@@ -263,11 +263,12 @@ Proxy.prototype._onHttpServerConnect = function(req, socket, head) {
263263
264264 // we need first byte of data to detect if request is SSL encrypted
265265 if ( ! head || head . length === 0 ) {
266- socket . once ( 'data' , function ( data ) {
267- self . _onHttpServerConnect ( req , socket , data ) ;
268- } ) ;
269- // respond to the client that the connection was made so it can send us data
270- return socket . write ( 'HTTP/1.1 200 OK\r\n\r\n' ) ;
266+ socket . once ( 'data' , self . _onHttpServerConnect . bind ( self , req , socket ) ) ;
267+ socket . write ( 'HTTP/1.1 200 OK\r\n' ) ;
268+ if ( self . keepAlive && req . headers [ 'proxy-connection' ] === 'keep-alive' ) {
269+ socket . write ( 'Proxy-Connection: Keep-Alive\r\n' ) ;
270+ }
271+ return socket . write ( '\r\n' ) ;
271272 }
272273
273274 socket . pause ( ) ;
@@ -709,9 +710,8 @@ Proxy.prototype._onHttpServerRequest = function(isSSL, clientToProxyRequest, pro
709710 ctx . serverToProxyResponse . headers [ 'transfer-encoding' ] = 'chunked' ;
710711 delete ctx . serverToProxyResponse . headers [ 'content-length' ] ;
711712 if ( self . keepAlive ) {
712- if ( ! ctx . serverToProxyResponse . headers [ 'connection' ] ) {
713- var keepAlive = ctx . clientToProxyRequest . headers && ctx . clientToProxyRequest . headers [ 'connection' ] && ctx . clientToProxyRequest . headers [ 'connection' ] . trim ( ) . toLowerCase ( ) === 'keep-alive' ;
714- ctx . serverToProxyResponse . headers [ 'connection' ] = keepAlive ? 'keep-alive' : 'close' ;
713+ if ( ctx . clientToProxyRequest . headers [ 'proxy-connection' ] ) {
714+ ctx . serverToProxyResponse . headers [ 'proxy-connection' ] = 'keep-alive' ;
715715 }
716716 } else {
717717 ctx . serverToProxyResponse . headers [ 'connection' ] = 'close' ;
0 commit comments