Skip to content

close websocket if proxyReq is closed before upgrade #708

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

Merged
merged 2 commits into from
Oct 1, 2014
Merged
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
4 changes: 4 additions & 0 deletions lib/http-proxy/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 40 additions & 0 deletions test/lib-http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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({
Expand Down