Skip to content

Commit

Permalink
Fix problem with req.url not being not properly replaced.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderarity committed Mar 29, 2012
1 parent a088efd commit f1611ec
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/node-http-proxy/proxy-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

var util = require('util'),
events = require('events'),
fs = require('fs');
fs = require('fs'),
url = require('url');

//
// ### function ProxyTable (router, silent)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f1611ec

Please sign in to comment.