Skip to content

Commit 8127575

Browse files
georgiyordanovjcrugzz
authored andcommitted
Fix overwriting of global options (#1074)
1 parent c979ba9 commit 8127575

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

lib/http-proxy/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ function createRightProxy(type) {
4141
cntr--;
4242
}
4343

44+
var requestOptions = options;
4445
if(
4546
!(args[cntr] instanceof Buffer) &&
4647
args[cntr] !== res
4748
) {
4849
//Copy global options
49-
options = extend({}, options);
50+
requestOptions = extend({}, options);
5051
//Overwrite with request options
51-
extend(options, args[cntr]);
52+
extend(requestOptions, args[cntr]);
5253

5354
cntr--;
5455
}
@@ -60,11 +61,11 @@ function createRightProxy(type) {
6061
/* optional args parse end */
6162

6263
['target', 'forward'].forEach(function(e) {
63-
if (typeof options[e] === 'string')
64-
options[e] = parse_url(options[e]);
64+
if (typeof requestOptions[e] === 'string')
65+
requestOptions[e] = parse_url(requestOptions[e]);
6566
});
6667

67-
if (!options.target && !options.forward) {
68+
if (!requestOptions.target && !requestOptions.forward) {
6869
return this.emit('error', new Error('Must provide a proper URL as target'));
6970
}
7071

@@ -77,7 +78,7 @@ function createRightProxy(type) {
7778
* refer to the connection socket
7879
* pass(req, socket, options, head)
7980
*/
80-
if(passes[i](req, res, options, head, this, cbl)) { // passes can return a truthy value to halt the loop
81+
if(passes[i](req, res, requestOptions, head, this, cbl)) { // passes can return a truthy value to halt the loop
8182
break;
8283
}
8384
}

test/lib-http-proxy-passes-web-incoming-test.js

+45
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,49 @@ describe('#createProxyServer.web() using own http server', function () {
367367

368368
http.request('http://127.0.0.1:8081', function() {}).end();
369369
});
370+
371+
it('should proxy requests to multiple servers with different options', function (done) {
372+
var proxy = httpProxy.createProxyServer();
373+
374+
// proxies to two servers depending on url, rewriting the url as well
375+
// http://127.0.0.1:8080/s1/ -> http://127.0.0.1:8081/
376+
// http://127.0.0.1:8080/ -> http://127.0.0.1:8082/
377+
function requestHandler(req, res) {
378+
if (req.url.indexOf('/s1/') === 0) {
379+
proxy.web(req, res, {
380+
ignorePath: true,
381+
target: 'http://127.0.0.1:8081' + req.url.substring(3)
382+
});
383+
} else {
384+
proxy.web(req, res, {
385+
target: 'http://127.0.0.1:8082'
386+
});
387+
}
388+
}
389+
390+
var proxyServer = http.createServer(requestHandler);
391+
392+
var source1 = http.createServer(function(req, res) {
393+
expect(req.method).to.eql('GET');
394+
expect(req.headers.host.split(':')[1]).to.eql('8080');
395+
expect(req.url).to.eql('/test1');
396+
});
397+
398+
var source2 = http.createServer(function(req, res) {
399+
source1.close();
400+
source2.close();
401+
proxyServer.close();
402+
expect(req.method).to.eql('GET');
403+
expect(req.headers.host.split(':')[1]).to.eql('8080');
404+
expect(req.url).to.eql('/test2');
405+
done();
406+
});
407+
408+
proxyServer.listen('8080');
409+
source1.listen('8081');
410+
source2.listen('8082');
411+
412+
http.request('http://127.0.0.1:8080/s1/test1', function() {}).end();
413+
http.request('http://127.0.0.1:8080/test2', function() {}).end();
414+
});
370415
});

0 commit comments

Comments
 (0)