Skip to content

Fix for issue #387 websocket proxy not work in node.js version 0.10.0 #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,15 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
socket: socket,
head: head
};

//
// Here we set the handshake `headers` and `statusCode` data to the outgoing
// request so that we can reuse this data later.
//
reverseProxy.handshake = {
headers: {},
statusCode: null,
}

//
// If the agent for this particular `host` and `port` combination
Expand All @@ -703,7 +712,16 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// In addition, it's important to note the closure scope here. Since
// there is no mapping of the socket to the request bound to it.
//
reverseProxy.on('upgrade', function (_, remoteSocket, head) {
reverseProxy.on('upgrade', function ( res, remoteSocket, head) {

//
// Prepare handshake response 'headers' and 'statusCode'.
//
reverseProxy.handshake = {
headers: res.headers,
statusCode: res.statusCode,
}

//
// Prepare the socket for the reverseProxy request and begin to
// stream data between the two sockets. Here it is important to
Expand All @@ -719,6 +737,28 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
//
reverseProxy.once('socket', function (revSocket) {
revSocket.on('data', function handshake (data) {

// Set empty headers
var headers = '';

//
// If the handshake statusCode 101, concat headers.
//
if(reverseProxy.handshake.statusCode && reverseProxy.handshake.statusCode == 101){

headers = [
'HTTP/1.1 101 Switching Protocols'
, 'Upgrade: websocket'
, 'Connection: Upgrade'
, 'Sec-WebSocket-Accept: ' + reverseProxy.handshake.headers['sec-websocket-accept']
];

headers = headers.concat('', '').join('\r\n');

}



//
// Ok, kind of harmfull part of code. Socket.IO sends a hash
// at the end of handshake if protocol === 76, but we need
Expand Down Expand Up @@ -748,7 +788,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// from the original incoming request.
//
self.emit('websocket:handshake', req, socket, head, sdata, data);
socket.write(sdata);
// add headers to the socket
socket.write(headers+sdata);
var flushed = socket.write(data);
if (!flushed) {
revSocket.pause();
Expand Down