Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close outgoing socket (fixes #559) #561

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/http-proxy/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also call onOutgoingError here since ending the proxySocket won't trigger its error event in anyway.

});

common.setupSocket(proxySocket);

Expand All @@ -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 {
Expand Down
24 changes: 22 additions & 2 deletions test/lib-http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down