Skip to content

Commit

Permalink
[api] add an ignorePath option if you want to disregard the path of t…
Browse files Browse the repository at this point in the history
…he incoming request when proxying to the target server
  • Loading branch information
jcrugzz committed Dec 23, 2014
1 parent 9ece52f commit 59d6dd3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/http-proxy.js
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
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
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

0 comments on commit 59d6dd3

Please sign in to comment.