Skip to content
Browse files

[api] Simplify the usage for the `.changeHeaders` option. Fixes #34

  • Loading branch information...
1 parent 9d9509f commit eb6f9a6354765881104f2e103fd648aa7b941721 @indexzero indexzero committed Jun 26, 2011
Showing with 20 additions and 9 deletions.
  1. +20 −9 lib/node-http-proxy.js
View
29 lib/node-http-proxy.js
@@ -235,11 +235,22 @@ var HttpProxy = exports.HttpProxy = function (options) {
var self = this;
options = options || {};
- options.target = options.target || {};
+ //
+ // Setup basic proxying options
+ //
this.https = options.https;
this.forward = options.forward;
- this.target = options.target;
+ this.target = options.target || {};
+
+ //
+ // Setup additional options for WebSocket proxying. When forcing
+ // the WebSocket handshake to change the `sec-websocket-location`
+ // and `sec-websocket-origin` headers `options.source` **MUST**
+ // be provided or the operation will fail with an `origin mismatch`
+ // by definition.
+ //
+ this.source = options.source || { host: 'localhost', port: 8000 };
this.changeOrigin = options.changeOrigin || false;
if (options.router) {
@@ -684,12 +695,18 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// Setup the incoming client socket.
_socket(socket);
+ function getPort (port) {
+ port = port || 80;
+ return port - 80 === 0 ? '' : ':' + port
+ }
+
//
// Get the protocol, and host for this request and create an instance
// of `http.Agent` or `https.Agent` from the pool managed by `node-http-proxy`.
//
var protocolName = options.https || this.target.https ? 'https' : 'http',
- remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port),
+ portUri = getPort(this.source.port),
+ remoteHost = options.host + portUri,
agent = _getAgent(options.host, options.port, options.https || this.target.https);
// Change headers (if requested).
@@ -777,12 +794,6 @@ 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) {
//

0 comments on commit eb6f9a6

Please sign in to comment.
Something went wrong with that request. Please try again.