Permalink
Browse files

Merge pull request #218 from CodeRarity/proxy-table-fix

Fix problem with req.url not being not properly replaced.
  • Loading branch information...
2 parents ca37ad7 + 5d839dd commit 64c974755d767d79531065625580546d46236d36 @coderarity coderarity committed Jun 5, 2012
Showing with 20 additions and 13 deletions.
  1. +13 −12 lib/node-http-proxy/proxy-table.js
  2. +7 −1 test/http/routing-proxy-test.js
@@ -26,7 +26,8 @@
var util = require('util'),
events = require('events'),
- fs = require('fs');
+ fs = require('fs'),
+ url = require('url');
//
// ### function ProxyTable (router, silent)
@@ -137,17 +138,17 @@ ProxyTable.prototype.getProxyLocation = function (req) {
for (var i in this.routes) {
var route = this.routes[i];
if (target.match(route.route)) {
- var pathSegments = route.path.split('/');
-
- if (pathSegments.length > 1) {
- // don't include the proxytable path segments in the proxied request url
- pathSegments = new RegExp("/" + pathSegments.slice(1).join('/'));
- req.url = req.url.replace(pathSegments, '');
- }
-
- var location = route.target.split(':'),
- host = location[0],
- port = location.length === 1 ? 80 : location[1];
+ var requrl = url.parse(req.url);
+ //add the 'http://'' to get around a url.parse bug, it won't actually be used.
+ var targeturl = url.parse('http://'+route.target);
+ var pathurl = url.parse('http://'+route.path);
+
+ //This replaces the path's part of the URL to the target's part of the URL.
+ requrl.pathname = requrl.pathname.replace(pathurl.pathname, targeturl.pathname);
+ req.url = url.format(requrl);
+
+ var host = targeturl.hostname,
+ port = targeturl.port || 80;
return {
port: port,
@@ -32,6 +32,9 @@ var defaultOptions = {
"bar.com": "127.0.0.1:8092",
"baz.com/taco": "127.0.0.1:8098",
"pizza.com/taco/muffins": "127.0.0.1:8099",
+ "blah.com/me": "127.0.0.1:8088/remapped",
+ "bleh.com/remap/this": "127.0.0.1:8087/remap/remapped",
+ "test.com/double/tap": "127.0.0.1:8086/remap",
}
};
@@ -53,7 +56,10 @@ vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"an incoming request to foo.com": runner.assertProxied('foo.com', 8090, 8091),
"an incoming request to bar.com": runner.assertProxied('bar.com', 8090, 8092),
"an incoming request to baz.com/taco": runner.assertProxied('baz.com', 8090, 8098, "/taco", "/"),
- "an incoming request to pizza.com/taco/muffins": runner.assertProxied('pizza.com', 8090, 8099, "/taco/muffins", "/taco"),
+ "an incoming request to pizza.com/taco/muffins": runner.assertProxied('pizza.com', 8090, 8099, "/taco/muffins", "/"),
+ "an incoming request to blah.com/me/fun": runner.assertProxied('blah.com', 8090, 8088, "/me/fun", "/remapped/fun"),
+ "an incoming request to bleh.com/remap/this": runner.assertProxied('bleh.com', 8090, 8087, "/remap/this", "/remap/remapped"),
+ "an incoming request to test.com/double/tap/double/tap": runner.assertProxied('test.com', 8090, 8086, "/double/tap/double/tap", "/remap/double/tap"),
"an incoming request to unknown.com": runner.assertResponseCode(8090, 404)
},
"and routing by Hostname": {

1 comment on commit 64c9747

This commit broke my proxy server. Not sure why yet.

[EDIT]
On further investigation, two problem emerge: wildcards in the hostname, and trailing slashes. I was using wildcards in the hostname so that it would work for both www.risacher.org and "risacher.org". I was also following the examples in not using trailing slashes in the routes, which didn't work as expected with the new code.

My old routes table looked something like this:

{
".*risacher.org/ajaxterm":         "127.0.0.1:8022",
".*risacher.org/term":             "127.0.0.1:8001",
".*risacher.org/express":          "127.0.0.1:3000",
".*risacher.org/otherapp":              "127.0.0.1:9001",
".*":                            "127.0.0.1:81"
}

My modified routes looks like this:

{
"risacher.org/ajaxterm/":        "127.0.0.1:8022",
"risacher.org/term/":            "127.0.0.1:8001",
"risacher.org/express/":         "127.0.0.1:3000",
"risacher.org/otherapp/":             "127.0.0.1:9001",
"www.risacher.org/otherapp/":         "127.0.0.1:9001",
".*":                            "127.0.0.1:81"
}
Please sign in to comment.