Commit 5d2192e6 authored by indexzero's avatar indexzero

[api minor] Small refactor to emit `webSocketProxyError` from a single helper...

[api minor] Small refactor to emit `webSocketProxyError` from a single helper function on any of the various `error` events in the proxy chain
parent 652cca37
...@@ -541,7 +541,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -541,7 +541,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// This request is not WebSocket request // This request is not WebSocket request
return; return;
} }
// Turn of all bufferings // Turn of all bufferings
// For server set KeepAlive // For server set KeepAlive
// For client set encoding // For client set encoding
...@@ -640,6 +640,15 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -640,6 +640,15 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// Make the outgoing WebSocket request // Make the outgoing WebSocket request
var reverseProxy = agent.appendMessage(outgoing); 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 // 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 // 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 ...@@ -665,7 +674,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
agent.on('upgrade', function (_, remoteSocket, head) { agent.on('upgrade', function (_, remoteSocket, head) {
// //
// Prepare the socket for the reverseProxy request and begin to // 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); _socket(remoteSocket, true);
onUpgrade(remoteSocket._httpMessage, remoteSocket); onUpgrade(remoteSocket._httpMessage, remoteSocket);
...@@ -705,26 +715,19 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -705,26 +715,19 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
socket.write(sdata); socket.write(sdata);
socket.write(data); socket.write(data);
} }
catch (e) { catch (ex) {
reverseProxy.end(); proxyError(ex)
socket.end();
} }
// Catch socket errors // Catch socket errors
socket.on('error', function() { socket.on('error', proxyError);
reverseProxy.end();
socket.end();
});
// Remove data listener now that the 'handshake' is complete // Remove data listener now that the 'handshake' is complete
reverseProxy.socket.removeListener('data', handshake); reverseProxy.socket.removeListener('data', handshake);
}); });
} }
reverseProxy.on('error', function (err) { reverseProxy.on('error', proxyError);
reverseProxy.end();
socket.end();
});
try { try {
// //
...@@ -733,8 +736,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -733,8 +736,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
reverseProxy.write(head); reverseProxy.write(head);
} }
catch (ex) { catch (ex) {
reverseProxy.end(); proxyError(ex);
socket.end();
} }
// //
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment