Skip to content

Commit

Permalink
Merge pull request #759 from nodejitsu/ignore-path
Browse files Browse the repository at this point in the history
[api] add an ignorePath option if you want to disregard the path of the ...
  • Loading branch information
jcrugzz committed Apr 20, 2015
2 parents 0bd446c + 9eefd46 commit 0db8f19
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
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>
* auth : Basic authentication i.e. 'user:password' to compute an Authorization header.
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 @@ -83,6 +83,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 @@ -175,9 +182,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 @@ -241,6 +241,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 0db8f19

Please sign in to comment.