Skip to content

Commit 9eefd46

Browse files
committed
[api] add an ignorePath option if you want to disregard the path of the incoming request when proxying to the target server fixes #758
1 parent 9ece52f commit 9eefd46

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

lib/http-proxy.js

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports.createProxyServer =
3939
* secure : <true/false, verify SSL certificate>
4040
* toProxy: <true/false, explicitly specify if we are proxying to another proxy>
4141
* prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
42+
* ignorePath: <true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
4243
* localAddress : <Local interface string to bind for outgoing connections>
4344
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
4445
* hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null.

lib/http-proxy/common.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
7979
? url.parse(req.url).path
8080
: req.url;
8181

82+
//
83+
// Remark: ignorePath will just straight up ignore whatever the request's
84+
// path is. This can be labeled as FOOT-GUN material if you do not know what
85+
// you are doing and are using conflicting options.
86+
//
87+
outgoingPath = !options.ignorePath ? outgoingPath : '/';
88+
8289
outgoing.path = common.urlJoin(targetPath, outgoingPath);
8390

8491
if (options.changeOrigin) {
@@ -158,9 +165,7 @@ common.urlJoin = function() {
158165
// joining e.g. ['', 'am']
159166
//
160167
retSegs = [
161-
args.filter(function filter(a) {
162-
return !!a;
163-
}).join('/').replace(/\/+/g, '/').replace(/:\//g, '://')
168+
args.filter(Boolean).join('/').replace(/\/+/g, '/').replace(/:\//g, '://')
164169
];
165170

166171
// Only join the query string if it exists so we don't have trailing a '?'

test/lib-http-proxy-common-test.js

+25
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,31 @@ describe('lib/http-proxy/common.js', function () {
239239
expect(outgoing.path).to.eql('/' + google);
240240
});
241241

242+
describe('when using ignorePath', function () {
243+
it('should ignore the path of the `req.url` passed in but use the target path', function () {
244+
var outgoing = {};
245+
var myEndpoint = 'https://whatever.com/some/crazy/path/whoooo';
246+
common.setupOutgoing(outgoing, {
247+
target: url.parse(myEndpoint),
248+
ignorePath: true
249+
}, { url: '/more/crazy/pathness' });
250+
251+
expect(outgoing.path).to.eql('/some/crazy/path/whoooo/');
252+
});
253+
254+
it('and prependPath: false, it should ignore path of target and incoming request', function () {
255+
var outgoing = {};
256+
var myEndpoint = 'https://whatever.com/some/crazy/path/whoooo';
257+
common.setupOutgoing(outgoing, {
258+
target: url.parse(myEndpoint),
259+
ignorePath: true,
260+
prependPath: false
261+
}, { url: '/more/crazy/pathness' });
262+
263+
expect(outgoing.path).to.eql('/');
264+
});
265+
});
266+
242267
describe('when using changeOrigin', function () {
243268
it('should correctly set the port to the host when it is a non-standard port using url.parse', function () {
244269
var outgoing = {};

0 commit comments

Comments
 (0)