Skip to content

Commit 006eb24

Browse files
Fix overwriting of global options
1 parent 912cd3a commit 006eb24

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
@@ -338,4 +338,49 @@ describe('#createProxyServer.web() using own http server', function () {
338338

339339
http.request('http://127.0.0.1:8081', function() {}).end();
340340
});
341+
342+
it('should proxy requests to multiple servers with different options', function (done) {
343+
var proxy = httpProxy.createProxyServer();
344+
345+
// proxies to two servers depending on url, rewriting the url as well
346+
// http://127.0.0.1:8080/s1/ -> http://127.0.0.1:8081/
347+
// http://127.0.0.1:8080/ -> http://127.0.0.1:8082/
348+
function requestHandler(req, res) {
349+
if (req.url.indexOf('/s1/') === 0) {
350+
proxy.web(req, res, {
351+
ignorePath: true,
352+
target: 'http://127.0.0.1:8081' + req.url.substring(3)
353+
});
354+
} else {
355+
proxy.web(req, res, {
356+
target: 'http://127.0.0.1:8082'
357+
});
358+
}
359+
}
360+
361+
var proxyServer = http.createServer(requestHandler);
362+
363+
var source1 = http.createServer(function(req, res) {
364+
expect(req.method).to.eql('GET');
365+
expect(req.headers.host.split(':')[1]).to.eql('8080');
366+
expect(req.url).to.eql('/test1');
367+
});
368+
369+
var source2 = http.createServer(function(req, res) {
370+
source1.close();
371+
source2.close();
372+
proxyServer.close();
373+
expect(req.method).to.eql('GET');
374+
expect(req.headers.host.split(':')[1]).to.eql('8080');
375+
expect(req.url).to.eql('/test2');
376+
done();
377+
});
378+
379+
proxyServer.listen('8080');
380+
source1.listen('8081');
381+
source2.listen('8082');
382+
383+
http.request('http://127.0.0.1:8080/s1/test1', function() {}).end();
384+
http.request('http://127.0.0.1:8080/test2', function() {}).end();
385+
});
341386
});

0 commit comments

Comments
 (0)