Skip to content

Commit 985025c

Browse files
Ivan Jaramilloindexzero
Ivan Jaramillo
authored andcommitted
Add headers on 'handshake'
The headers in the 'handshake' event were not written to the socket, the client received data but not the headers.
1 parent 98f5c46 commit 985025c

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

lib/node-http-proxy/http-proxy.js

+43-2
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,15 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
698698
socket: socket,
699699
head: head
700700
};
701+
702+
//
703+
// Here we set the handshake `headers` and `statusCode` data to the outgoing
704+
// request so that we can reuse this data later.
705+
//
706+
reverseProxy.handshake = {
707+
headers: {},
708+
statusCode: null,
709+
}
701710

702711
//
703712
// If the agent for this particular `host` and `port` combination
@@ -707,7 +716,16 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
707716
// In addition, it's important to note the closure scope here. Since
708717
// there is no mapping of the socket to the request bound to it.
709718
//
710-
reverseProxy.on('upgrade', function (_, remoteSocket, head) {
719+
reverseProxy.on('upgrade', function ( res, remoteSocket, head) {
720+
721+
//
722+
// Prepare handshake response 'headers' and 'statusCode'.
723+
//
724+
reverseProxy.handshake = {
725+
headers: res.headers,
726+
statusCode: res.statusCode,
727+
}
728+
711729
//
712730
// Prepare the socket for the reverseProxy request and begin to
713731
// stream data between the two sockets. Here it is important to
@@ -723,6 +741,28 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
723741
//
724742
reverseProxy.once('socket', function (revSocket) {
725743
revSocket.on('data', function handshake (data) {
744+
745+
// Set empty headers
746+
var headers = '';
747+
748+
//
749+
// If the handshake statusCode 101, concat headers.
750+
//
751+
if(reverseProxy.handshake.statusCode && reverseProxy.handshake.statusCode == 101){
752+
753+
headers = [
754+
'HTTP/1.1 101 Switching Protocols'
755+
, 'Upgrade: websocket'
756+
, 'Connection: Upgrade'
757+
, 'Sec-WebSocket-Accept: ' + reverseProxy.handshake.headers['sec-websocket-accept']
758+
];
759+
760+
headers = headers.concat('', '').join('\r\n');
761+
762+
}
763+
764+
765+
726766
//
727767
// Ok, kind of harmfull part of code. Socket.IO sends a hash
728768
// at the end of handshake if protocol === 76, but we need
@@ -752,7 +792,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
752792
// from the original incoming request.
753793
//
754794
self.emit('websocket:handshake', req, socket, head, sdata, data);
755-
socket.write(sdata);
795+
// add headers to the socket
796+
socket.write(headers+sdata);
756797
var flushed = socket.write(data);
757798
if (!flushed) {
758799
revSocket.pause();

0 commit comments

Comments
 (0)