From 5d2192e654f23e1b76e0b66554debe1590a3af64 Mon Sep 17 00:00:00 2001 From: indexzero Date: Thu, 19 May 2011 01:53:55 -0400 Subject: [PATCH] [api minor] Small refactor to emit `webSocketProxyError` from a single helper function on any of the various `error` events in the proxy chain --- lib/node-http-proxy.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 6711b1931..df3d6dc0f 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -541,7 +541,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options // This request is not WebSocket request return; } - + // Turn of all bufferings // For server set KeepAlive // For client set encoding @@ -640,6 +640,15 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options // Make the outgoing WebSocket request var reverseProxy = agent.appendMessage(outgoing); + function proxyError (err) { + reverseProxy.end(); + if (self.emit('webSocketProxyError', req, socket, head)) { + return; + } + + socket.end(); + } + // // Here we set the incoming `req`, `socket` and `head` data to the outgoing // request so that we can reuse this data later on in the closure scope @@ -665,7 +674,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options agent.on('upgrade', function (_, remoteSocket, head) { // // Prepare the socket for the reverseProxy request and begin to - // stream data between the two sockets + // stream data between the two sockets. Here it is important to + // note that `remoteSocket._httpMessage === reverseProxy`. // _socket(remoteSocket, true); onUpgrade(remoteSocket._httpMessage, remoteSocket); @@ -705,26 +715,19 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options socket.write(sdata); socket.write(data); } - catch (e) { - reverseProxy.end(); - socket.end(); + catch (ex) { + proxyError(ex) } // Catch socket errors - socket.on('error', function() { - reverseProxy.end(); - socket.end(); - }); + socket.on('error', proxyError); // Remove data listener now that the 'handshake' is complete reverseProxy.socket.removeListener('data', handshake); }); } - reverseProxy.on('error', function (err) { - reverseProxy.end(); - socket.end(); - }); + reverseProxy.on('error', proxyError); try { // @@ -733,8 +736,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options reverseProxy.write(head); } catch (ex) { - reverseProxy.end(); - socket.end(); + proxyError(ex); } //