Skip to content

Commit 37fd6d8

Browse files
Improve keepAlive: handles proxy-connection header & don't force
connection header
1 parent cce888a commit 37fd6d8

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/proxy.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)