Skip to content

Commit eb6f9a6

Browse files
committed
[api] Simplify the usage for the .changeHeaders option. Fixes #34
1 parent 9d9509f commit eb6f9a6

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

lib/node-http-proxy.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,22 @@ var HttpProxy = exports.HttpProxy = function (options) {
235235

236236
var self = this;
237237
options = options || {};
238-
options.target = options.target || {};
239238

239+
//
240+
// Setup basic proxying options
241+
//
240242
this.https = options.https;
241243
this.forward = options.forward;
242-
this.target = options.target;
244+
this.target = options.target || {};
245+
246+
//
247+
// Setup additional options for WebSocket proxying. When forcing
248+
// the WebSocket handshake to change the `sec-websocket-location`
249+
// and `sec-websocket-origin` headers `options.source` **MUST**
250+
// be provided or the operation will fail with an `origin mismatch`
251+
// by definition.
252+
//
253+
this.source = options.source || { host: 'localhost', port: 8000 };
243254
this.changeOrigin = options.changeOrigin || false;
244255

245256
if (options.router) {
@@ -684,12 +695,18 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
684695
// Setup the incoming client socket.
685696
_socket(socket);
686697

698+
function getPort (port) {
699+
port = port || 80;
700+
return port - 80 === 0 ? '' : ':' + port
701+
}
702+
687703
//
688704
// Get the protocol, and host for this request and create an instance
689705
// of `http.Agent` or `https.Agent` from the pool managed by `node-http-proxy`.
690706
//
691707
var protocolName = options.https || this.target.https ? 'https' : 'http',
692-
remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port),
708+
portUri = getPort(this.source.port),
709+
remoteHost = options.host + portUri,
693710
agent = _getAgent(options.host, options.port, options.https || this.target.https);
694711

695712
// Change headers (if requested).
@@ -777,12 +794,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
777794

778795
// Get the Non-Printable data
779796
data = data.slice(Buffer.byteLength(sdata), data.length);
780-
781-
//
782-
// Replace the host and origin headers in the Printable data
783-
//
784-
sdata = sdata.replace(remoteHost, options.host)
785-
.replace(remoteHost, options.host);
786797

787798
if (self.https && !self.target.https) {
788799
//

0 commit comments

Comments
 (0)