Skip to content

Commit

Permalink
[api minor] Small refactor to emit webSocketProxyError from a singl…
Browse files Browse the repository at this point in the history
…e helper function on any of the various `error` events in the proxy chain
  • Loading branch information
indexzero committed May 19, 2011
1 parent 652cca3 commit 5d2192e
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
//
Expand All @@ -733,8 +736,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
reverseProxy.write(head);
}
catch (ex) {
reverseProxy.end();
socket.end();
proxyError(ex);
}

//
Expand Down

0 comments on commit 5d2192e

Please sign in to comment.