Skip to content

Commit

Permalink
fix common.urlJoin when target has query string
Browse files Browse the repository at this point in the history
  • Loading branch information
potoo0 committed Jul 3, 2024
1 parent 7a6ca08 commit 307fa0a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
42 changes: 21 additions & 21 deletions lib/http-proxy/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
if (options.auth) {
outgoing.auth = options.auth;
}

if (options.ca) {
outgoing.ca = options.ca;
}
Expand Down Expand Up @@ -170,35 +170,35 @@ common.hasEncryptedConnection = function(req) {
*/

common.urlJoin = function() {
//
// We do not want to mess with the query string. All we want to touch is the path.
//
//
// join url and merge all query string.
//
var args = Array.prototype.slice.call(arguments),
lastIndex = args.length - 1,
last = args[lastIndex],
lastSegs = last.split('?'),
retSegs;

args[lastIndex] = lastSegs.shift();
queryParams = [],
queryParamRaw = '',
retSegs;

args.forEach((url, index) => {
var qpStart = url.indexOf('?')
if (qpStart !== -1) {
queryParams.push(url.substring(qpStart + 1))
args[index] = url.substring(0, qpStart)
}
});
queryParamRaw = queryParams.filter(Boolean).join('&');

//
// Join all strings, but remove empty strings so we don't get extra slashes from
// joining e.g. ['', 'am']
//
retSegs = [
args.filter(Boolean).join('/')
.replace(/\/+/g, '/')
.replace('http:/', 'http://')
.replace('https:/', 'https://')
];
retSegs = args.filter(Boolean).join('/')
.replace(/\/+/g, '/')
.replace('http:/', 'http://')
.replace('https:/', 'https://');

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

// Handle case where there could be multiple ? in the URL.
retSegs.push.apply(retSegs, lastSegs);

return retSegs.join('?')
return queryParamRaw ? retSegs + '?' + queryParamRaw : retSegs
};

/**
Expand Down
9 changes: 9 additions & 0 deletions test/lib-http-proxy-common-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ describe('lib/http-proxy/common.js', function () {
expect(outgoing.path).to.eql('/forward/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2');
})

it('target path has query string', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
target: { path: '/forward?f=1' },
}, { url: '/src?s=1' });

expect(outgoing.path).to.eql('/forward/src?f=1&s=1');
})

//
// This is the proper failing test case for the common.join problem
//
Expand Down

0 comments on commit 307fa0a

Please sign in to comment.