From 4a2b870cc92300e9c12865081a5272a4c02c3d47 Mon Sep 17 00:00:00 2001 From: Ricky Miller Date: Sun, 9 Nov 2014 08:44:22 +0900 Subject: [PATCH] do not modify the query string --- lib/http-proxy/common.js | 23 +++++++++++++++++++---- test/lib-http-proxy-common-test.js | 9 +++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index eda6b17e1..9dd30822a 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -77,7 +77,7 @@ common.setupOutgoing = function(outgoing, options, req, forward) { if (options.changeOrigin) { outgoing.headers.host = outgoing.host; } - + return outgoing; }; @@ -134,9 +134,24 @@ common.getPort = function(req) { common.urlJoin = function() { var args = Array.prototype.slice.call(arguments); + + // We do not want to mess with the query string. All we want to touch is the path. + var lastIndex = args.length-1; + var last = args[lastIndex] + var lastSegs = last.split('?') + args[lastIndex] = lastSegs[0] + // Join all strings, but remove empty strings so we don't get extra slashes from // joining e.g. ['', 'am'] - return args.filter(function filter(a) { - return !!a; - }).join('/').replace(/\/+/g, '/'); + var retSegs = [ + args.filter(function filter(a) { + return !!a; + }).join('/').replace(/\/+/g, '/') + ]; + + // Only join the query string if it exists so we don't have trailing a '?' + // on every request + lastSegs[1] && retSegs.push(lastSegs[1]); + + return retSegs.join('?') }; diff --git a/test/lib-http-proxy-common-test.js b/test/lib-http-proxy-common-test.js index 01af2f7ef..b74fd021f 100644 --- a/test/lib-http-proxy-common-test.js +++ b/test/lib-http-proxy-common-test.js @@ -202,6 +202,15 @@ describe('lib/http-proxy/common.js', function () { expect(outgoing.path).to.eql('/forward/static/path'); }) + + it('should not modify the query string', function () { + var outgoing = {}; + common.setupOutgoing(outgoing, { + target: { path: '/forward' }, + }, { url: '/?foo=bar//&target=http://foobar.com/' }); + + expect(outgoing.path).to.eql('/forward/?foo=bar//&target=http://foobar.com/'); + }) }); describe('#setupSocket', function () {