Skip to content

Commit 107b9da

Browse files
committed
Merge pull request #699 from STRML/master
Urgent: Fix breaking bug on url joining resulting in paths like `///path`.
2 parents df12aeb + 73d865b commit 107b9da

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
7171
? url.parse(req.url).path
7272
: req.url;
7373

74-
outgoing.path = targetPath
75-
? targetPath + '/' + outgoingPath
76-
: outgoingPath;
74+
outgoing.path = common.urlJoin(targetPath, outgoingPath);
7775

7876
return outgoing;
7977
};
@@ -109,4 +107,12 @@ common.getPort = function(req) {
109107
return res ?
110108
res[1] :
111109
req.connection.pair ? '443' : '80' ;
112-
}
110+
};
111+
112+
// OS-agnostic join (doesn't break on URLs like path.join does on Windows)
113+
common.urlJoin = function() {
114+
var args = Array.prototype.slice.call(arguments);
115+
// Join all strings, but remove empty strings so we don't get extra slashes from
116+
// joining e.g. ['', 'am']
117+
return args.filter(function(a) { return !!a; }).join('/').replace(/\/+/g, '/');
118+
};

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

+9
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ describe('lib/http-proxy/common.js', function () {
150150

151151
expect(outgoing.path).to.eql('hi');
152152
})
153+
154+
it('should properly join paths', function () {
155+
var outgoing = {};
156+
common.setupOutgoing(outgoing, {
157+
target: { path: '/forward' },
158+
}, { url: '/static/path' });
159+
160+
expect(outgoing.path).to.eql('/forward/static/path');
161+
})
153162
});
154163

155164
describe('#setupSocket', function () {

0 commit comments

Comments
 (0)