Skip to content

Commit 028d204

Browse files
committed
[fix] Change sec-websocket-location header when proxying WSS --> WS. Added test coverage for this scenario
1 parent 76ecb51 commit 028d204

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Diff for: lib/node-http-proxy.js

+11
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ var HttpProxy = exports.HttpProxy = function (options) {
222222
options = options || {};
223223
options.target = options.target || {};
224224

225+
this.https = options.https;
225226
this.forward = options.forward;
226227
this.target = options.target;
227228
this.changeOrigin = options.changeOrigin || false;
@@ -706,9 +707,19 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
706707
// Get the Non-Printable data
707708
data = data.slice(Buffer.byteLength(sdata), data.length);
708709

710+
//
709711
// Replace the host and origin headers in the Printable data
712+
//
710713
sdata = sdata.replace(remoteHost, options.host)
711714
.replace(remoteHost, options.host);
715+
716+
if (self.https && !self.target.https) {
717+
//
718+
// If the proxy server is running HTTPS but the client is running
719+
// HTTP then replace `ws` with `wss` in the data sent back to the client.
720+
//
721+
sdata = sdata.replace('ws:', 'wss:');
722+
}
712723

713724
try {
714725
//

Diff for: test/web-socket-proxy-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
8787
assert.equal(msg, 'from client');
8888
},
8989
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
90+
assert.isString(headers.response['sec-websocket-location']);
91+
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
9092
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
9193
}
9294
},
@@ -128,6 +130,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
128130
assert.equal(msg, 'from server');
129131
},
130132
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
133+
assert.isString(headers.response['sec-websocket-location']);
134+
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
131135
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
132136
}
133137
}

0 commit comments

Comments
 (0)