Skip to content

Commit 0db8f19

Browse files
committed
Merge pull request #759 from nodejitsu/ignore-path
[api] add an ignorePath option if you want to disregard the path of the ...
2 parents 0bd446c + 9eefd46 commit 0db8f19

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Diff for: 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
* auth : Basic authentication i.e. 'user:password' to compute an Authorization header.

Diff for: lib/http-proxy/common.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
8383
? (url.parse(req.url).path || '/')
8484
: req.url;
8585

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

8895
if (options.changeOrigin) {
@@ -175,9 +182,7 @@ common.urlJoin = function() {
175182
// joining e.g. ['', 'am']
176183
//
177184
retSegs = [
178-
args.filter(function filter(a) {
179-
return !!a;
180-
}).join('/').replace(/\/+/g, '/').replace(/:\//g, '://')
185+
args.filter(Boolean).join('/').replace(/\/+/g, '/').replace(/:\//g, '://')
181186
];
182187

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

Diff for: test/lib-http-proxy-common-test.js

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

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

0 commit comments

Comments
 (0)