@@ -40,6 +40,10 @@ function HttpsProxyAgent (opts) {
4040 proxy . host = proxy . hostname || proxy . host ;
4141 proxy . port = + proxy . port || ( this . secureProxy ? 443 : 80 ) ;
4242
43+ if ( opts . timeout ) {
44+ this . timeout = opts . timeout ;
45+ }
46+
4347 if ( proxy . host && proxy . path ) {
4448 // if both a `host` and `path` are specified then it's most likely the
4549 // result of a `url.parse()` call... we need to remove the `path` portion so
@@ -70,6 +74,10 @@ function connect (req, opts, fn) {
7074 socket = net . connect ( proxy ) ;
7175 }
7276
77+ if ( this . timeout ) {
78+ socket . setTimeout ( this . timeout ) ;
79+ }
80+
7381 // we need to buffer any HTTP traffic that happens with the proxy before we get
7482 // the CONNECT response, so that if the response is anything other than an "200"
7583 // response code, then we can re-play the "data" events on the socket once the
@@ -89,12 +97,22 @@ function connect (req, opts, fn) {
8997 socket . removeListener ( 'error' , onerror ) ;
9098 socket . removeListener ( 'close' , onclose ) ;
9199 socket . removeListener ( 'readable' , read ) ;
100+ socket . removeListener ( 'timeout' , ontimeout ) ;
92101 }
93102
94103 function onclose ( err ) {
95104 debug ( 'onclose had error %o' , err ) ;
96105 }
97106
107+ function ontimeout ( ) {
108+ var err = new Error ( 'Proxy connection timed out' ) ;
109+ err . code = 'ETIMEOUT' ;
110+ cleanup ( ) ;
111+ // prevent unhandled `error` events
112+ socket . destroy ( ) ;
113+ fn ( err ) ;
114+ }
115+
98116 function onend ( ) {
99117 debug ( 'onend' ) ;
100118 }
@@ -182,6 +200,7 @@ function connect (req, opts, fn) {
182200 socket . on ( 'error' , onerror ) ;
183201 socket . on ( 'close' , onclose ) ;
184202 socket . on ( 'end' , onend ) ;
203+ socket . on ( 'timeout' , ontimeout ) ;
185204
186205 if ( socket . read ) {
187206 read ( ) ;
0 commit comments