Skip to content

Commit f1611ec

Browse files
committed
Fix problem with req.url not being not properly replaced.
1 parent a088efd commit f1611ec

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

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

+13-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
var util = require('util'),
2828
events = require('events'),
29-
fs = require('fs');
29+
fs = require('fs'),
30+
url = require('url');
3031

3132
//
3233
// ### function ProxyTable (router, silent)
@@ -137,17 +138,17 @@ ProxyTable.prototype.getProxyLocation = function (req) {
137138
for (var i in this.routes) {
138139
var route = this.routes[i];
139140
if (target.match(route.route)) {
140-
var pathSegments = route.path.split('/');
141-
142-
if (pathSegments.length > 1) {
143-
// don't include the proxytable path segments in the proxied request url
144-
pathSegments = new RegExp("/" + pathSegments.slice(1).join('/'));
145-
req.url = req.url.replace(pathSegments, '');
146-
}
147-
148-
var location = route.target.split(':'),
149-
host = location[0],
150-
port = location.length === 1 ? 80 : location[1];
141+
var requrl = url.parse(req.url);
142+
//add the 'http://'' to get around a url.parse bug, it won't actually be used.
143+
var targeturl = url.parse('http://'+route.target);
144+
var pathurl = url.parse('http://'+route.path);
145+
146+
//This replaces the path's part of the URL to the target's part of the URL.
147+
requrl.pathname = requrl.pathname.replace(pathurl.pathname, targeturl.pathname);
148+
req.url = url.format(requrl);
149+
150+
var host = targeturl.hostname,
151+
port = targeturl.port || 80;
151152

152153
return {
153154
port: port,

0 commit comments

Comments
 (0)