From 0a54e31f5282b09ef136c538653cbc94def94362 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 21 Jul 2019 12:41:41 +0200 Subject: [PATCH 1/3] use OutgoingMessage#flushHeaders() instead of OutgoingMessage#_send() --- proxy.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/proxy.js b/proxy.js index 38d7363..8286394 100644 --- a/proxy.js +++ b/proxy.js @@ -339,9 +339,7 @@ function onconnect (req, socket, head) { res.removeListener('finish', onfinish); res.writeHead(200, 'Connection established'); - - // HACK: force a flush of the HTTP header - res._send(''); + res.flushHeaders(); // relinquish control of the `socket` from the ServerResponse instance res.detachSocket(socket); From 548fc432b33d13c44657b109666319e065b283f8 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 21 Jul 2019 12:31:54 +0200 Subject: [PATCH 2/3] resume the client socket when it is unpiped Flush any buffered data to ensure that the socket is closed and no connection is leaked on Node.js >= 10.0.0. --- proxy.js | 12 ++++++++++++ test/test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/proxy.js b/proxy.js index 8286394..3cf7ca5 100644 --- a/proxy.js +++ b/proxy.js @@ -349,6 +349,7 @@ function onconnect (req, socket, head) { res = null; socket.pipe(target); + target.once('unpipe', resume); target.pipe(socket); } @@ -414,6 +415,17 @@ function onconnect (req, socket, head) { }); } +/** + * Resumes a socket. + * + * @param {(net.Socket|tls.Socket)} socket The socket to resume + * @api private + */ + +function resume (socket) { + socket.resume(); +} + /** * Checks `Proxy-Authorization` request headers. Same logic applied to CONNECT * requests as well as regular HTTP requests. diff --git a/test/test.js b/test/test.js index ff919e3..b105276 100644 --- a/test/test.js +++ b/test/test.js @@ -108,6 +108,48 @@ describe('proxy', function () { }); }); + it('should resume the client socket when it is unpiped', function (done) { + server.once('request', function (req, res) { + res.end(); + }); + + var gotData = false; + var host = '127.0.0.1:' + serverPort; + var socket = net.connect({ port: proxyPort }); + + socket.on('connect', function () { + socket.write( + 'CONNECT ' + host + ' HTTP/1.1\r\n' + + 'Host: ' + host + '\r\n' + + '\r\n' + ); + }); + + socket.on('close', function () { + assert(gotData); + done(); + }); + + socket.setEncoding('utf8'); + socket.once('data', function (data) { + assert(0 == data.indexOf('HTTP/1.1 200 Connection established\r\n')); + + socket.write( + 'POST / HTTP/1.1\r\n' + + 'Host: ' + host + '\r\n' + + 'Connection: close\r\n' + + 'Transfer-Encoding: chunked\r\n' + + '\r\n' + ); + + socket.once('data', function (data) { + assert(0 == data.indexOf('HTTP/1.1 200 OK\r\n')); + gotData = true; + socket.write('10\r\n{ "foo": "bar",\r\n'); + }); + }); + }); + describe('authentication', function () { function clearAuth () { From 958f27a744cc8e6b2c326e2f5ccb096848d1ac85 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 21 Jul 2019 12:40:02 +0200 Subject: [PATCH 3/3] test on node 10 and 12 --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 805d3d5..5783ad7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,3 @@ -sudo: false - language: node_js node_js: @@ -8,6 +6,8 @@ node_js: - "6" - "7" - "8" + - "10" + - "12" install: - PATH="`npm bin`:`npm bin -g`:$PATH"