Skip to content

Commit 46b078a

Browse files
mikkelindexzero
authored andcommitted
pathnameOnly flag added. Ignores hostname and applies routing table to the paths being requested.
1 parent 10f6b05 commit 46b078a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lib/node-http-proxy/proxy-table.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var util = require('util'),
3434
// #### @router {Object} Object containing the host based routes
3535
// #### @silent {Boolean} Value indicating whether we should suppress logs
3636
// #### @hostnameOnly {Boolean} Value indicating if we should route based on __hostname string only__
37+
// #### @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.
3738
// Constructor function for the ProxyTable responsible for getting
3839
// locations of proxy targets based on ServerRequest headers; specifically
3940
// the HTTP host header.
@@ -43,6 +44,7 @@ var ProxyTable = exports.ProxyTable = function (options) {
4344

4445
this.silent = options.silent || options.silent !== true;
4546
this.target = options.target || {};
47+
this.pathnameOnly = options.pathnameOnly === true;
4648
this.hostnameOnly = options.hostnameOnly === true;
4749

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

167-
var target = req.headers.host.split(':')[0];
169+
var targetHost = req.headers.host.split(':')[0];
168170
if (this.hostnameOnly === true) {
171+
var target = targetHost;
169172
if (this.router.hasOwnProperty(target)) {
170173
var location = this.router[target].split(':'),
171174
host = location[0],
@@ -177,8 +180,24 @@ ProxyTable.prototype.getProxyLocation = function (req) {
177180
};
178181
}
179182
}
183+
else if (this.pathnameOnly === true) {
184+
var target = req.url;
185+
for (var i in this.routes) {
186+
var route = this.routes[i];
187+
if (target.match(route.source.regexp)) {
188+
req.url = url.format(target.replace(route.source.regexp, ''));
189+
return {
190+
protocol: route.target.url.protocol.replace(':', ''),
191+
host: route.target.url.hostname,
192+
port: route.target.url.port
193+
|| (this.target.https ? 443 : 80)
194+
};
195+
}
196+
}
197+
198+
}
180199
else {
181-
target += req.url;
200+
var target = targetHost + req.url;
182201
for (var i in this.routes) {
183202
var route = this.routes[i];
184203
if (target.match(route.source.regexp)) {

test/http/routing-table-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ vows.describe(helpers.describe('routing-table')).addBatch({
4343
"bar.com": "127.0.0.1:{PORT}"
4444
}
4545
}),
46+
"using pathnameOnly": macros.http.assertProxiedToRoutes({
47+
pathnameOnly: true,
48+
routes: {
49+
"/foo": "127.0.0.1:{PORT}",
50+
"/bar": "127.0.0.1:{PORT}",
51+
"/pizza": "127.0.0.1:{PORT}"
52+
}
53+
}),
4654
"using a routing file": macros.http.assertProxiedToRoutes({
4755
filename: routeFile,
4856
routes: {

test/macros/http.js

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
262262
//
263263
proxy = {
264264
hostnameOnly: options.hostnameOnly,
265+
pathnameOnly: options.pathnameOnly,
265266
router: options.routes
266267
};
267268
}

0 commit comments

Comments
 (0)