Skip to content

Allow proxy to maintain the original target path #693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/http-proxy/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var common = exports,
path = require('path'),
url = require('url'),
extend = require('util')._extend;

Expand Down Expand Up @@ -57,13 +58,22 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
) { outgoing.headers.connection = 'close'; }
}


// the final path is target path + relative path requested by user:
var target = options[forward || 'target'];
var targetPath = target
? (target.path || '')
: '';

//
// Remark: Can we somehow not use url.parse as a perf optimization?
//
outgoing.path = !options.toProxy
var outgoingPath = !options.toProxy
? url.parse(req.url).path
: req.url;

outgoing.path = path.join(targetPath, outgoingPath);

return outgoing;
};

Expand Down
25 changes: 24 additions & 1 deletion test/lib-http-proxy-common-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ describe('lib/http-proxy/common.js', function () {

expect(outgoing.port).to.eql(443);
});

it('should keep the original target path in the outgoing path', function(){
var outgoing = {};
common.setupOutgoing(outgoing, {target:
{ path: 'some-path' }
}, { url : 'am' });

expect(outgoing.path).to.eql('some-path/am');
});

it('should keep the original forward path in the outgoing path', function(){
var outgoing = {};
common.setupOutgoing(outgoing, {
target: {},
forward: {
path: 'some-path'
}
}, {
url : 'am'
}, 'forward');

expect(outgoing.path).to.eql('some-path/am');
});
});

describe('#setupSocket', function () {
Expand Down Expand Up @@ -144,4 +167,4 @@ describe('lib/http-proxy/common.js', function () {
expect(socketConfig.keepalive).to.eql(true);
});
});
});
});