@@ -4,8 +4,6 @@ var net = require('net')
44 , tls = require ( 'tls' )
55 , http = require ( 'http' )
66 , https = require ( 'https' )
7- , events = require ( 'events' )
8- , assert = require ( 'assert' )
97 , util = require ( 'util' )
108 , Buffer = require ( 'safe-buffer' ) . Buffer
119 ;
@@ -68,7 +66,7 @@ function TunnelingAgent(options) {
6866 self . removeSocket ( socket )
6967 } )
7068}
71- util . inherits ( TunnelingAgent , events . EventEmitter )
69+ util . inherits ( TunnelingAgent , http . Agent )
7270
7371TunnelingAgent . prototype . addRequest = function addRequest ( req , options ) {
7472 var self = this
@@ -123,6 +121,10 @@ TunnelingAgent.prototype.createSocket = function createSocket(options, cb) {
123121 { method : 'CONNECT'
124122 , path : options . host + ':' + options . port
125123 , agent : false
124+ , headers : {
125+ host : options . host + ':' + options . port
126+ }
127+ , servername : self . proxyOptions . host
126128 }
127129 )
128130 if ( connectOptions . proxyAuth ) {
@@ -138,6 +140,9 @@ TunnelingAgent.prototype.createSocket = function createSocket(options, cb) {
138140 connectReq . once ( 'upgrade' , onUpgrade ) // for v0.6
139141 connectReq . once ( 'connect' , onConnect ) // for v0.7 or later
140142 connectReq . once ( 'error' , onError )
143+ connectReq . setTimeout ( options . timeout || 15000 , function ( ) {
144+ connectReq . abort ( ) ;
145+ } ) ;
141146 connectReq . end ( )
142147
143148 function onResponse ( res ) {
@@ -155,18 +160,17 @@ TunnelingAgent.prototype.createSocket = function createSocket(options, cb) {
155160 function onConnect ( res , socket , head ) {
156161 connectReq . removeAllListeners ( )
157162 socket . removeAllListeners ( )
163+ self . sockets [ self . sockets . indexOf ( placeholder ) ] = socket
158164
159- if ( res . statusCode === 200 ) {
160- assert . equal ( head . length , 0 )
165+ if ( res . statusCode === 200 && head . length == 0 ) {
161166 debug ( 'tunneling connection has established' )
162- self . sockets [ self . sockets . indexOf ( placeholder ) ] = socket
163167 cb ( socket )
164168 } else {
165169 debug ( 'tunneling socket could not be established, statusCode=%d' , res . statusCode )
166170 var error = new Error ( 'tunneling socket could not be established, ' + 'statusCode=' + res . statusCode )
167171 error . code = 'ECONNRESET'
168- options . request . emit ( 'error' , error )
169- self . removeSocket ( placeholder )
172+ cb ( socket )
173+ options . request . emit ( 'error' , error ) // fire up error on ClientRequest.
170174 }
171175 }
172176
@@ -200,7 +204,7 @@ function createSecureSocket(options, cb) {
200204 TunnelingAgent . prototype . createSocket . call ( self , options , function ( socket ) {
201205 // 0 is dummy port for v0.6
202206 var secureSocket = tls . connect ( 0 , mergeOptions ( { } , self . options ,
203- { servername : options . host
207+ { servername : self . options . servername || options . host
204208 , socket : socket
205209 }
206210 ) )
0 commit comments