Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: http-party/node-http-proxy
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.7.0
Choose a base ref
...
head repository: http-party/node-http-proxy
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.7.1
Choose a head ref
  • 11 commits
  • 10 files changed
  • 4 contributors

Commits on Sep 17, 2011

  1. Make sure the target port is an integer

    This fixes a bug that caused cli to fail when --target was specified with both hostname and port
    jnordberg committed Sep 17, 2011
    Copy the full SHA
    5ba25aa View commit details

Commits on Sep 19, 2011

  1. Merge pull request #109 from jnordberg/master

    command line tool - make sure targetPort is an integer
    indexzero committed Sep 19, 2011
    Copy the full SHA
    787370e View commit details

Commits on Sep 20, 2011

  1. Copy the full SHA
    2677bb6 View commit details
  2. Copy the full SHA
    66e9820 View commit details
  3. Copy the full SHA
    1f33943 View commit details

Commits on Sep 21, 2011

  1. Copy the full SHA
    24ef919 View commit details
  2. Merge pull request #110 from nodejitsu/gh-107

    #107: Set x-forwarded-for header (amongst others)
    AvianFlu committed Sep 21, 2011
    Copy the full SHA
    e5693d2 View commit details
  3. Copy the full SHA
    8fc8d96 View commit details
  4. Copy the full SHA
    549360a View commit details
  5. Merge pull request #114 from nodejitsu/readme-fixes

    Readme fixes
    AvianFlu committed Sep 21, 2011
    Copy the full SHA
    2b2c4ab View commit details
  6. Copy the full SHA
    5ad7739 View commit details
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -145,7 +145,7 @@ var http = require('http'),
//
// Create a new instance of HttProxy to use in your server
//
var proxy = new httpProxy.HttpProxy();
var proxy = new httpProxy.RoutingProxy();

//
// Create a regular http server and proxy its handler
@@ -247,12 +247,14 @@ httpProxy.createServer(8000, 'localhost', options).listen(8001);
//
// Create an instance of HttpProxy to use with another HTTPS server
//
var proxy = new httpProxy.HttpProxy();
https.createServer(options.https, function (req, res) {
proxy.proxyRequest(req, res, {
var proxy = new httpProxy.HttpProxy({
target: {
host: 'localhost',
port: 8000
})
}
});
https.createServer(options.https, function (req, res) {
proxy.proxyRequest(req, res)
}).listen(8002);

//
@@ -266,7 +268,7 @@ http.createServer(function (req, res) {
```

### Proxying to HTTPS from HTTPS
Proxying from HTTPS to HTTPS is essentially the same as proxying from HTTPS to HTTP, but you must include `target` option in when calling `httpProxy.createServer` or instantiating a new instance of `HttpProxy`.
Proxying from HTTPS to HTTPS is essentially the same as proxying from HTTPS to HTTP, but you must include the `target` option in when calling `httpProxy.createServer` or instantiating a new instance of `HttpProxy`.

``` js
var fs = require('fs'),
@@ -293,15 +295,14 @@ httpProxy.createServer(8000, 'localhost', options).listen(8001);
//
var proxy = new httpProxy.HttpProxy({
target: {
host: 'localhost',
port: 8000,
https: true
}
});

https.createServer(options.https, function (req, res) {
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 8000
})
proxy.proxyRequest(req, res);
}).listen(8002);

//
@@ -325,7 +326,7 @@ httpProxy.createServer(
```

## Proxying WebSockets
Websockets are handled automatically when using the `httpProxy.createServer()`, but if you want to use it in conjunction with a stand-alone HTTP + WebSocket (such as [socket.io][5]) server here's how:
Websockets are handled automatically when using `httpProxy.createServer()`, but if you want to use it in conjunction with a stand-alone HTTP + WebSocket (such as [socket.io][5]) server here's how:

``` js
var http = require('http'),
@@ -334,26 +335,24 @@ var http = require('http'),
//
// Create an instance of node-http-proxy
//
var proxy = new httpProxy.HttpProxy();
var proxy = new httpProxy.HttpProxy(
target: {
host: 'localhost',
port: 8000
});

var server = http.createServer(function (req, res) {
//
// Proxy normal HTTP requests
//
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 8000
})
proxy.proxyRequest(req, res);
});

server.on('upgrade', function(req, socket, head) {
//
// Proxy websocket requests too
//
proxy.proxyWebSocketRequest(req, socket, head, {
host: 'localhost',
port: 8000
});
proxy.proxyWebSocketRequest(req, socket, head);
});
```

2 changes: 1 addition & 1 deletion bin/node-http-proxy
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ if (typeof target === 'string') location = target.split(':');
//
var server;
if (location) {
var targetPort = location.length === 1 ? 80 : location[1];
var targetPort = location.length === 1 ? 80 : parseInt(location[1]);
server = httpProxy.createServer(targetPort, location[0], config);
}
else if (config.router) {
2 changes: 1 addition & 1 deletion examples/http/standalone-proxy.js
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ var util = require('util'),
//
// Http Server with proxyRequest Handler and Latency
//
var proxy = new httpProxy.HttpProxy();
var proxy = new httpProxy.RoutingProxy();
http.createServer(function (req, res) {
var buffer = httpProxy.buffer(req);
setTimeout(function() {
18 changes: 8 additions & 10 deletions examples/websocket/latent-websocket-proxy.js
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
var sys = require('sys'),
http = require('http'),
colors = require('colors'),
websocket = require('./../vendor/websocket'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy');

try {
@@ -67,12 +67,14 @@ socket.on('connection', function (client) {
//
// Setup our server to proxy standard HTTP requests
//
var proxy = new httpProxy.HttpProxy();
var proxyServer = http.createServer(function (req, res) {
proxy.proxyRequest(req, res, {
var proxy = new httpProxy.HttpProxy({
target: {
host: 'localhost',
port: 8080
})
}
});
var proxyServer = http.createServer(function (req, res) {
proxy.proxyRequest(req, res);
});

//
@@ -83,11 +85,7 @@ proxyServer.on('upgrade', function (req, socket, head) {
var buffer = httpProxy.buffer(socket);

setTimeout(function () {
proxy.proxyWebSocketRequest(req, socket, head, {
port: 8080,
host: 'localhost',
buffer: buffer
});
proxy.proxyWebSocketRequest(req, socket, head, buffer);
}, 1000);
});

17 changes: 8 additions & 9 deletions examples/websocket/standalone-websocket-proxy.js
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
var sys = require('sys'),
http = require('http'),
colors = require('colors'),
websocket = require('./../vendor/websocket'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy');

try {
@@ -67,23 +67,22 @@ socket.on('connection', function (client) {
//
// Setup our server to proxy standard HTTP requests
//
var proxy = new httpProxy.HttpProxy();
var proxyServer = http.createServer(function (req, res) {
proxy.proxyRequest(req, res, {
var proxy = new httpProxy.HttpProxy({
target: {
host: 'localhost',
port: 8080
})
}
});
var proxyServer = http.createServer(function (req, res) {
proxy.proxyRequest(req, res);
});

//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {
proxy.proxyWebSocketRequest(req, socket, head, {
port: 8080,
host: 'localhost'
});
proxy.proxyWebSocketRequest(req, socket, head);
});

proxyServer.listen(8081);
2 changes: 1 addition & 1 deletion examples/websocket/websocket-proxy.js
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
var sys = require('sys'),
http = require('http'),
colors = require('colors'),
websocket = require('./../vendor/websocket'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy');

try {
9 changes: 5 additions & 4 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
@@ -128,9 +128,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// * `x-forwarded-proto`: Protocol of the original request
// * `x-forwarded-port`: Port of the original request.
//
if (this.enable.xforward && req.connection && req.connection.socket) {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress;
req.headers['x-forwarded-port'] = req.connection.remotePort || req.connection.socket.remotePort;

if (this.enable.xforward && req.connection && req.socket) {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort;
req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http';
}

@@ -763,4 +764,4 @@ HttpProxy.prototype._forwardRequest = function (req) {
req.on('end', function () {
forwardProxy.end();
});
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-proxy",
"version": "0.7.0",
"version": "0.7.1",
"description": "A full-featured http reverse proxy for node.js",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"contributors": [
36 changes: 35 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
@@ -128,6 +128,40 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
return test;
};

// A test helper to check and see if the http headers were set properly.
TestRunner.prototype.assertHeaders = function (proxyPort, headerName, createProxy) {
var assertion = "should receive http header \"" + headerName + "\"",
protocol = this.source.protocols.http;

var test = {
topic: function () {
var that = this, options = {
method: 'GET',
uri: protocol + '://localhost:' + proxyPort,
headers: {
host: 'unknown.com'
}
};

if (createProxy) {
return createProxy(function () {
request(options, that.callback);
});
}

request(options, this.callback);
}
};

test[assertion] = function (err, res, body) {
assert.isNull(err);
assert.isNotNull(res.headers[headerName]);
};

return test;
};


//
// WebSocketTest
//
@@ -368,4 +402,4 @@ function merge (target) {
});
});
return target;
}
}
7 changes: 5 additions & 2 deletions test/http/http-proxy-test.js
Original file line number Diff line number Diff line change
@@ -74,8 +74,11 @@ vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
"and a valid target server": runner.assertProxied('localhost', 8120, 8121, function (callback) {
runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback);
}),
"and without a valid forward server": runner.assertProxied('localhost', 8122, 8123, function (callback) {
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', badForwardOptions, callback);
"and also a valid target server": runner.assertHeaders(8122, "x-forwarded-for", function (callback) {
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', forwardOptions, callback);
}),
"and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, function (callback) {
runner.startProxyServerWithForwarding(8124, 8125, 'localhost', badForwardOptions, callback);
})
}
}