From 0b223abb65286e0844627c2791e98dc884dadf10 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Wed, 22 Jan 2014 15:28:23 -0800 Subject: [PATCH 1/2] Fix argument order for ws stream pass head and error handling was broken before this commit. --- lib/http-proxy/passes/ws-incoming.js | 2 +- test/lib-http-proxy-test.js | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 057c54066..a9e879785 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -78,7 +78,7 @@ var passes = exports; * * @api private */ - function stream(req, socket, options, server, head, clb) { + function stream(req, socket, options, head, server, clb) { common.setupSocket(socket); if (head && head.length) socket.unshift(head); diff --git a/test/lib-http-proxy-test.js b/test/lib-http-proxy-test.js index 6b2ce035e..d6231751b 100644 --- a/test/lib-http-proxy-test.js +++ b/test/lib-http-proxy-test.js @@ -261,9 +261,29 @@ describe('lib/http-proxy.js', function() { }); }); }); - }); - describe('#createProxyServer using the ws-incoming passes', function () { + it('should emit error on proxy error', function (done) { + var ports = { source: gen.port, proxy: gen.port }; + 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'); + }); + + proxy.on('error', function (err) { + expect(err).to.be.an(Error); + expect(err.code).to.be('ECONNREFUSED'); + proxyServer._server.close(); + done(); + }); + }); + it('should proxy a socket.io stream', function (done) { var ports = { source: gen.port, proxy: gen.port }; var proxy = httpProxy.createProxyServer({ From d8ea687936d6bed0f3e99849695cab2dcdccd6f4 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Wed, 22 Jan 2014 15:40:40 -0800 Subject: [PATCH 2/2] Close outgoing ws if incoming ws emits error Fixes #559, which contains a full reproduction recipe. --- lib/http-proxy/passes/ws-incoming.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index a9e879785..7673fe067 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -88,10 +88,16 @@ var passes = exports; common.setupOutgoing(options.ssl || {}, options, req) ); // Error Handler - proxyReq.on('error', onError); + proxyReq.on('error', onOutgoingError); proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) { - proxySocket.on('error', onError); + proxySocket.on('error', onOutgoingError); + // The pipe below will end proxySocket if socket closes cleanly, but not + // if it errors (eg, vanishes from the net and starts returning + // EHOSTUNREACH). We need to do that explicitly. + socket.on('error', function () { + proxySocket.end(); + }); common.setupSocket(proxySocket); @@ -106,7 +112,7 @@ var passes = exports; return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT - function onError(err) { + function onOutgoingError(err) { if (clb) { clb(err, req, socket); } else {