diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 070cefbb9..e7407fe44 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -106,7 +106,7 @@ var passes = exports; common.setupOutgoing(options.ssl || {}, options, req) ); // Error Handler - proxyReq.on('error', function(err){ + var onProxyError = function(err){ var ev = 'http-proxy:outgoing:ws:'; // If no error listeners, so throw the error. if (!options.ee.listeners(ev + 'error').length){ @@ -114,11 +114,18 @@ var passes = exports; } // Also emit the error events options.ee.emit(ev + 'error', err, req, socket, head); - }); + }; + + proxyReq.on('error', onProxyError); proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) { common.setupSocket(proxySocket); + // http client removes the error handler from proxySocket (which + // redirected 'error' events from proxySocket to proxyReq) before emitting + // 'upgrade'. So we need to ask for it again. + proxySocket.on('error', onProxyError); + if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead); socket.write('HTTP/1.1 101 Switching Protocols\r\n');