From 77305489d9b88d283802477e155340e5dacfcc2c Mon Sep 17 00:00:00 2001 From: MinRK Date: Tue, 30 Sep 2014 16:11:49 -0700 Subject: [PATCH 1/2] test closing upstream socket prior to upgrade should close the client socket with ECONNRESET, but currently is left hanging. --- test/lib-http-proxy-test.js | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/lib-http-proxy-test.js b/test/lib-http-proxy-test.js index e0b4eca92..1110d29f1 100644 --- a/test/lib-http-proxy-test.js +++ b/test/lib-http-proxy-test.js @@ -281,6 +281,11 @@ describe('lib/http-proxy.js', function() { client.send('hello there'); }); + client.on('error', function (err) { + expect(err).to.be.an(Error); + expect(err.code).to.be('ECONNRESET'); + }); + proxy.on('error', function (err) { expect(err).to.be.an(Error); expect(err.code).to.be('ECONNREFUSED'); @@ -289,6 +294,41 @@ describe('lib/http-proxy.js', function() { }); }); + it('should close client socket if upstream is closed before upgrade', function (done) { + var ports = { source: gen.port, proxy: gen.port }; + var server = http.createServer(); + server.on('upgrade', function (req, socket, head) { + var response = [ + 'HTTP/1.1 404 Not Found', + 'Content-type: text/html', + '', + '' + ]; + socket.write(response.join('\r\n')); + socket.end(); + }); + server.listen(ports.source); + + var proxy = httpProxy.createProxyServer({ + // note: we don't ever listen on this port + target: 'ws://127.0.0.1:' + ports.source, + ws: true + }), + proxyServer = proxy.listen(ports.proxy), + client = new ws('ws://127.0.0.1:' + ports.proxy); + + client.on('open', function () { + client.send('hello there'); + }); + + client.on('error', function (err) { + expect(err).to.be.an(Error); + expect(err.code).to.be('ECONNRESET'); + proxyServer.close(); + done(); + }); + }); + it('should proxy a socket.io stream', function (done) { var ports = { source: gen.port, proxy: gen.port }; var proxy = httpProxy.createProxyServer({ From bcd8a564a802512c90df20353ca341a1d8c84501 Mon Sep 17 00:00:00 2001 From: MinRK Date: Tue, 30 Sep 2014 16:13:17 -0700 Subject: [PATCH 2/2] close websocket if proxyReq is closed before upgrade avoids leaving client sockets open when upstream connections are rejected. --- lib/http-proxy/passes/ws-incoming.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 1bc786980..9d3970366 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -89,6 +89,10 @@ var passes = exports; ); // Error Handler proxyReq.on('error', onOutgoingError); + proxyReq.on('response', function (res) { + // if upgrade event isn't going to happen, close the socket + if (!res.upgrade) socket.end(); + }); proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) { proxySocket.on('error', onOutgoingError);