Skip to content

Commit

Permalink
[fix] Change sec-websocket-location header when proxying WSS --> WS. …
Browse files Browse the repository at this point in the history
…Added test coverage for this scenario
  • Loading branch information
indexzero committed May 23, 2011
1 parent 76ecb51 commit 028d204
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ var HttpProxy = exports.HttpProxy = function (options) {
options = options || {};
options.target = options.target || {};

this.https = options.https;
this.forward = options.forward;
this.target = options.target;
this.changeOrigin = options.changeOrigin || false;
Expand Down Expand Up @@ -706,9 +707,19 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// Get the Non-Printable data
data = data.slice(Buffer.byteLength(sdata), data.length);

//
// Replace the host and origin headers in the Printable data
//
sdata = sdata.replace(remoteHost, options.host)
.replace(remoteHost, options.host);

if (self.https && !self.target.https) {
//
// If the proxy server is running HTTPS but the client is running
// HTTP then replace `ws` with `wss` in the data sent back to the client.
//
sdata = sdata.replace('ws:', 'wss:');
}

try {
//
Expand Down
4 changes: 4 additions & 0 deletions test/web-socket-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
assert.equal(msg, 'from client');
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.isString(headers.response['sec-websocket-location']);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
},
Expand Down Expand Up @@ -128,6 +130,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
assert.equal(msg, 'from server');
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.isString(headers.response['sec-websocket-location']);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
Expand Down

0 comments on commit 028d204

Please sign in to comment.