Skip to content

Commit

Permalink
pathnameOnly flag added. Ignores hostname and applies routing table t…
Browse files Browse the repository at this point in the history
…o the paths being requested.
  • Loading branch information
mikkel authored and indexzero committed Mar 9, 2013
1 parent 10f6b05 commit 46b078a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/node-http-proxy/proxy-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var util = require('util'),
// #### @router {Object} Object containing the host based routes
// #### @silent {Boolean} Value indicating whether we should suppress logs
// #### @hostnameOnly {Boolean} Value indicating if we should route based on __hostname string only__
// #### @pathnameOnly {Boolean} Value indicating if we should route based on only the pathname. __This causes hostnames to be ignored.__. Using this along with hostnameOnly wont work at all.
// Constructor function for the ProxyTable responsible for getting
// locations of proxy targets based on ServerRequest headers; specifically
// the HTTP host header.
Expand All @@ -43,6 +44,7 @@ var ProxyTable = exports.ProxyTable = function (options) {

this.silent = options.silent || options.silent !== true;
this.target = options.target || {};
this.pathnameOnly = options.pathnameOnly === true;
this.hostnameOnly = options.hostnameOnly === true;

if (typeof options.router === 'object') {
Expand Down Expand Up @@ -164,8 +166,9 @@ ProxyTable.prototype.getProxyLocation = function (req) {
return null;
}

var target = req.headers.host.split(':')[0];
var targetHost = req.headers.host.split(':')[0];
if (this.hostnameOnly === true) {
var target = targetHost;
if (this.router.hasOwnProperty(target)) {
var location = this.router[target].split(':'),
host = location[0],
Expand All @@ -177,8 +180,24 @@ ProxyTable.prototype.getProxyLocation = function (req) {
};
}
}
else if (this.pathnameOnly === true) {
var target = req.url;
for (var i in this.routes) {
var route = this.routes[i];
if (target.match(route.source.regexp)) {
req.url = url.format(target.replace(route.source.regexp, ''));
return {
protocol: route.target.url.protocol.replace(':', ''),
host: route.target.url.hostname,
port: route.target.url.port
|| (this.target.https ? 443 : 80)
};
}
}

}
else {
target += req.url;
var target = targetHost + req.url;
for (var i in this.routes) {
var route = this.routes[i];
if (target.match(route.source.regexp)) {
Expand Down
8 changes: 8 additions & 0 deletions test/http/routing-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ vows.describe(helpers.describe('routing-table')).addBatch({
"bar.com": "127.0.0.1:{PORT}"
}
}),
"using pathnameOnly": macros.http.assertProxiedToRoutes({
pathnameOnly: true,
routes: {
"/foo": "127.0.0.1:{PORT}",
"/bar": "127.0.0.1:{PORT}",
"/pizza": "127.0.0.1:{PORT}"
}
}),
"using a routing file": macros.http.assertProxiedToRoutes({
filename: routeFile,
routes: {
Expand Down
1 change: 1 addition & 0 deletions test/macros/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
//
proxy = {
hostnameOnly: options.hostnameOnly,
pathnameOnly: options.pathnameOnly,
router: options.routes
};
}
Expand Down

0 comments on commit 46b078a

Please sign in to comment.