Skip to content

Commit 814fbd2

Browse files
committed
Merge pull request #693 from EndangeredMassa/fix-path
Allow proxy to maintain the original target path
2 parents 6b83ae4 + a65021d commit 814fbd2

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/http-proxy/common.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var common = exports,
2+
path = require('path'),
23
url = require('url'),
34
extend = require('util')._extend;
45

@@ -57,13 +58,22 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
5758
) { outgoing.headers.connection = 'close'; }
5859
}
5960

61+
62+
// the final path is target path + relative path requested by user:
63+
var target = options[forward || 'target'];
64+
var targetPath = target
65+
? (target.path || '')
66+
: '';
67+
6068
//
6169
// Remark: Can we somehow not use url.parse as a perf optimization?
6270
//
63-
outgoing.path = !options.toProxy
71+
var outgoingPath = !options.toProxy
6472
? url.parse(req.url).path
6573
: req.url;
6674

75+
outgoing.path = path.join(targetPath, outgoingPath);
76+
6777
return outgoing;
6878
};
6979

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ describe('lib/http-proxy/common.js', function () {
117117

118118
expect(outgoing.port).to.eql(443);
119119
});
120+
121+
it('should keep the original target path in the outgoing path', function(){
122+
var outgoing = {};
123+
common.setupOutgoing(outgoing, {target:
124+
{ path: 'some-path' }
125+
}, { url : 'am' });
126+
127+
expect(outgoing.path).to.eql('some-path/am');
128+
});
129+
130+
it('should keep the original forward path in the outgoing path', function(){
131+
var outgoing = {};
132+
common.setupOutgoing(outgoing, {
133+
target: {},
134+
forward: {
135+
path: 'some-path'
136+
}
137+
}, {
138+
url : 'am'
139+
}, 'forward');
140+
141+
expect(outgoing.path).to.eql('some-path/am');
142+
});
120143
});
121144

122145
describe('#setupSocket', function () {
@@ -144,4 +167,4 @@ describe('lib/http-proxy/common.js', function () {
144167
expect(socketConfig.keepalive).to.eql(true);
145168
});
146169
});
147-
});
170+
});

0 commit comments

Comments
 (0)