@@ -263,18 +263,20 @@ 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+ socket . write ( 'Connection: keep-alive\r\n' ) ;
271+ }
272+ return socket . write ( '\r\n' ) ;
271273 }
272274
273275 socket . pause ( ) ;
274276
275277 /*
276278 * Detect TLS from first bytes of data
277- * Inspered from https://gist.github.com/tg-x/835636
279+ * Inspired from https://gist.github.com/tg-x/835636
278280 * used heuristic:
279281 * - an incoming connection using SSLv3/TLSv1 records should start with 0x16
280282 * - an incoming connection using SSLv2 records should start with the record size
@@ -659,12 +661,19 @@ Proxy.prototype._onHttpServerRequest = function(isSSL, clientToProxyRequest, pro
659661 ctx . proxyToClientResponse . on ( 'error' , self . _onError . bind ( self , 'PROXY_TO_CLIENT_RESPONSE_ERROR' , ctx ) ) ;
660662 ctx . clientToProxyRequest . pause ( ) ;
661663 var hostPort = Proxy . parseHostAndPort ( ctx . clientToProxyRequest , ctx . isSSL ? 443 : 80 ) ;
664+ var headers = { } ;
665+ for ( var h in ctx . clientToProxyRequest . headers ) {
666+ // don't forward proxy- headers
667+ if ( / ^ p r o x y \- / i. test ( h ) ) {
668+ headers [ h ] = ctx . clientToProxyRequest . headers [ h ] ;
669+ }
670+ }
662671 ctx . proxyToServerRequestOptions = {
663672 method : ctx . clientToProxyRequest . method ,
664673 path : ctx . clientToProxyRequest . url ,
665674 host : hostPort . host ,
666675 port : hostPort . port ,
667- headers : ctx . clientToProxyRequest . headers ,
676+ headers : headers ,
668677 agent : ctx . isSSL ? self . httpsAgent : self . httpAgent
669678 } ;
670679 return self . _onRequest ( ctx , function ( err ) {
@@ -702,9 +711,9 @@ Proxy.prototype._onHttpServerRequest = function(isSSL, clientToProxyRequest, pro
702711 ctx . serverToProxyResponse . headers [ 'transfer-encoding' ] = 'chunked' ;
703712 delete ctx . serverToProxyResponse . headers [ 'content-length' ] ;
704713 if ( self . keepAlive ) {
705- if ( ! ctx . serverToProxyResponse . headers [ 'connection' ] ) {
706- var keepAlive = ctx . clientToProxyRequest . headers && ctx . clientToProxyRequest . headers [ ' connection'] && ctx . clientToProxyRequest . headers [ 'connection' ] . trim ( ) . toLowerCase ( ) == = 'keep-alive' ;
707- ctx . serverToProxyResponse . headers [ 'connection' ] = keepAlive ? 'keep-alive' : 'close ';
714+ if ( ctx . clientToProxyRequest . headers [ 'proxy- connection' ] ) {
715+ ctx . serverToProxyResponse . headers [ 'proxy- connection'] = 'keep-alive' ;
716+ ctx . serverToProxyResponse . headers [ 'connection' ] = 'keep-alive' ;
708717 }
709718 } else {
710719 ctx . serverToProxyResponse . headers [ 'connection' ] = 'close' ;
0 commit comments