From 511b7b3d4743636de9d9fbe8ff409730d221d273 Mon Sep 17 00:00:00 2001 From: Domi Date: Sat, 31 May 2014 17:35:30 +0800 Subject: [PATCH 1/2] Fix proxy path This fix considers the actual target path again (which has been ignored). --- lib/http-proxy/common.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index 5d10647..9de052e 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -57,10 +57,14 @@ common.setupOutgoing = function(outgoing, options, req, forward) { ) { outgoing.headers.connection = 'close'; } } + + // the final path is target path + relative path requested by user: + outgoing.path = options.target.path; + // // Remark: Can we somehow not use url.parse as a perf optimization? // - outgoing.path = !options.toProxy + outgoing.path += !options.toProxy ? url.parse(req.url).path : req.url; From a65021d52b0ee039486819b5a95f442229458776 Mon Sep 17 00:00:00 2001 From: Sean Massa Date: Mon, 8 Sep 2014 16:23:11 -0500 Subject: [PATCH 2/2] fix tests for maintaining proxy path --- lib/http-proxy/common.js | 12 +++++++++--- test/lib-http-proxy-common-test.js | 25 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index 9de052e..196a890 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -1,4 +1,5 @@ var common = exports, + path = require('path'), url = require('url'), extend = require('util')._extend; @@ -57,17 +58,22 @@ common.setupOutgoing = function(outgoing, options, req, forward) { ) { outgoing.headers.connection = 'close'; } } - + // the final path is target path + relative path requested by user: - outgoing.path = options.target.path; + var target = options[forward || 'target']; + var targetPath = target + ? (target.path || '') + : ''; // // Remark: Can we somehow not use url.parse as a perf optimization? // - outgoing.path += !options.toProxy + var outgoingPath = !options.toProxy ? url.parse(req.url).path : req.url; + outgoing.path = path.join(targetPath, outgoingPath); + return outgoing; }; diff --git a/test/lib-http-proxy-common-test.js b/test/lib-http-proxy-common-test.js index dab2d26..86fee33 100644 --- a/test/lib-http-proxy-common-test.js +++ b/test/lib-http-proxy-common-test.js @@ -117,6 +117,29 @@ describe('lib/http-proxy/common.js', function () { expect(outgoing.port).to.eql(443); }); + + it('should keep the original target path in the outgoing path', function(){ + var outgoing = {}; + common.setupOutgoing(outgoing, {target: + { path: 'some-path' } + }, { url : 'am' }); + + expect(outgoing.path).to.eql('some-path/am'); + }); + + it('should keep the original forward path in the outgoing path', function(){ + var outgoing = {}; + common.setupOutgoing(outgoing, { + target: {}, + forward: { + path: 'some-path' + } + }, { + url : 'am' + }, 'forward'); + + expect(outgoing.path).to.eql('some-path/am'); + }); }); describe('#setupSocket', function () { @@ -144,4 +167,4 @@ describe('lib/http-proxy/common.js', function () { expect(socketConfig.keepalive).to.eql(true); }); }); -}); \ No newline at end of file +});