Commit eb6f9a63 authored by indexzero's avatar indexzero

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

parent 9d9509f7
......@@ -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) {
//
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment