From 028d2044e71d70b7bc21d339de29e2275c3be5c2 Mon Sep 17 00:00:00 2001 From: indexzero Date: Mon, 23 May 2011 02:18:12 -0400 Subject: [PATCH] [fix] Change sec-websocket-location header when proxying WSS --> WS. Added test coverage for this scenario --- lib/node-http-proxy.js | 11 +++++++++++ test/web-socket-proxy-test.js | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index ab836be..dc32f2e 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -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; @@ -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 { // diff --git a/test/web-socket-proxy-test.js b/test/web-socket-proxy-test.js index 07a9db7..20baa3b 100644 --- a/test/web-socket-proxy-test.js +++ b/test/web-socket-proxy-test.js @@ -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']); } }, @@ -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']); } }