Skip to content

Commit 5d2192e

Browse files
committed
[api minor] Small refactor to emit webSocketProxyError from a single helper function on any of the various error events in the proxy chain
1 parent 652cca3 commit 5d2192e

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

lib/node-http-proxy.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
541541
// This request is not WebSocket request
542542
return;
543543
}
544-
544+
545545
// Turn of all bufferings
546546
// For server set KeepAlive
547547
// For client set encoding
@@ -640,6 +640,15 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
640640
// Make the outgoing WebSocket request
641641
var reverseProxy = agent.appendMessage(outgoing);
642642

643+
function proxyError (err) {
644+
reverseProxy.end();
645+
if (self.emit('webSocketProxyError', req, socket, head)) {
646+
return;
647+
}
648+
649+
socket.end();
650+
}
651+
643652
//
644653
// Here we set the incoming `req`, `socket` and `head` data to the outgoing
645654
// 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
665674
agent.on('upgrade', function (_, remoteSocket, head) {
666675
//
667676
// Prepare the socket for the reverseProxy request and begin to
668-
// stream data between the two sockets
677+
// stream data between the two sockets. Here it is important to
678+
// note that `remoteSocket._httpMessage === reverseProxy`.
669679
//
670680
_socket(remoteSocket, true);
671681
onUpgrade(remoteSocket._httpMessage, remoteSocket);
@@ -705,26 +715,19 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
705715
socket.write(sdata);
706716
socket.write(data);
707717
}
708-
catch (e) {
709-
reverseProxy.end();
710-
socket.end();
718+
catch (ex) {
719+
proxyError(ex)
711720
}
712721

713722
// Catch socket errors
714-
socket.on('error', function() {
715-
reverseProxy.end();
716-
socket.end();
717-
});
723+
socket.on('error', proxyError);
718724

719725
// Remove data listener now that the 'handshake' is complete
720726
reverseProxy.socket.removeListener('data', handshake);
721727
});
722728
}
723729

724-
reverseProxy.on('error', function (err) {
725-
reverseProxy.end();
726-
socket.end();
727-
});
730+
reverseProxy.on('error', proxyError);
728731

729732
try {
730733
//
@@ -733,8 +736,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
733736
reverseProxy.write(head);
734737
}
735738
catch (ex) {
736-
reverseProxy.end();
737-
socket.end();
739+
proxyError(ex);
738740
}
739741

740742
//

0 commit comments

Comments
 (0)