Skip to content
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

[api] add an ignorePath option if you want to disregard the path of the ... #759

Merged
merged 1 commit into from
Apr 20, 2015
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
1 change: 1 addition & 0 deletions lib/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports.createProxyServer =
* secure : <true/false, verify SSL certificate>
* toProxy: <true/false, explicitly specify if we are proxying to another proxy>
* prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
* ignorePath: <true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
* localAddress : <Local interface string to bind for outgoing connections>
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
* hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null.
Expand Down
11 changes: 8 additions & 3 deletions lib/http-proxy/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
? url.parse(req.url).path
: req.url;

//
// Remark: ignorePath will just straight up ignore whatever the request's
// path is. This can be labeled as FOOT-GUN material if you do not know what
// you are doing and are using conflicting options.
//
outgoingPath = !options.ignorePath ? outgoingPath : '/';

outgoing.path = common.urlJoin(targetPath, outgoingPath);

if (options.changeOrigin) {
Expand Down Expand Up @@ -158,9 +165,7 @@ common.urlJoin = function() {
// joining e.g. ['', 'am']
//
retSegs = [
args.filter(function filter(a) {
return !!a;
}).join('/').replace(/\/+/g, '/').replace(/:\//g, '://')
args.filter(Boolean).join('/').replace(/\/+/g, '/').replace(/:\//g, '://')
];

// Only join the query string if it exists so we don't have trailing a '?'
Expand Down
25 changes: 25 additions & 0 deletions test/lib-http-proxy-common-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,31 @@ describe('lib/http-proxy/common.js', function () {
expect(outgoing.path).to.eql('/' + google);
});

describe('when using ignorePath', function () {
it('should ignore the path of the `req.url` passed in but use the target path', function () {
var outgoing = {};
var myEndpoint = 'https://whatever.com/some/crazy/path/whoooo';
common.setupOutgoing(outgoing, {
target: url.parse(myEndpoint),
ignorePath: true
}, { url: '/more/crazy/pathness' });

expect(outgoing.path).to.eql('/some/crazy/path/whoooo/');
});

it('and prependPath: false, it should ignore path of target and incoming request', function () {
var outgoing = {};
var myEndpoint = 'https://whatever.com/some/crazy/path/whoooo';
common.setupOutgoing(outgoing, {
target: url.parse(myEndpoint),
ignorePath: true,
prependPath: false
}, { url: '/more/crazy/pathness' });

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

describe('when using changeOrigin', function () {
it('should correctly set the port to the host when it is a non-standard port using url.parse', function () {
var outgoing = {};
Expand Down