From 9eefd4678ef95d878a9e5cdd25dd45b0c07311b7 Mon Sep 17 00:00:00 2001 From: Jarrett Cruger Date: Tue, 23 Dec 2014 13:17:58 -0500 Subject: [PATCH] [api] add an ignorePath option if you want to disregard the path of the incoming request when proxying to the target server fixes #758 --- lib/http-proxy.js | 1 + lib/http-proxy/common.js | 11 ++++++++--- test/lib-http-proxy-common-test.js | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/http-proxy.js b/lib/http-proxy.js index b1ad646f4..983d587b7 100644 --- a/lib/http-proxy.js +++ b/lib/http-proxy.js @@ -39,6 +39,7 @@ module.exports.createProxyServer = * secure : * toProxy: * prependPath: + * ignorePath: * localAddress : * changeOrigin: * hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null. diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index 66e80f9a1..e3dd87ecc 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -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) { @@ -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 '?' diff --git a/test/lib-http-proxy-common-test.js b/test/lib-http-proxy-common-test.js index 5cf36c232..a455ad510 100644 --- a/test/lib-http-proxy-common-test.js +++ b/test/lib-http-proxy-common-test.js @@ -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 = {};