mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[api minor] Small refactor to emit webSocketProxyError from a single helper function on any of the various error events in the proxy chain
This commit is contained in:
parent
652cca37ea
commit
5d2192e654
@ -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);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user