Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overwriting of global options #1074

Merged
merged 1 commit into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/http-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ function createRightProxy(type) {
cntr--;
}

var requestOptions = options;
if(
!(args[cntr] instanceof Buffer) &&
args[cntr] !== res
) {
//Copy global options
options = extend({}, options);
requestOptions = extend({}, options);
//Overwrite with request options
extend(options, args[cntr]);
extend(requestOptions, args[cntr]);

cntr--;
}
Expand All @@ -60,11 +61,11 @@ function createRightProxy(type) {
/* optional args parse end */

['target', 'forward'].forEach(function(e) {
if (typeof options[e] === 'string')
options[e] = parse_url(options[e]);
if (typeof requestOptions[e] === 'string')
requestOptions[e] = parse_url(requestOptions[e]);
});

if (!options.target && !options.forward) {
if (!requestOptions.target && !requestOptions.forward) {
return this.emit('error', new Error('Must provide a proper URL as target'));
}

Expand All @@ -77,7 +78,7 @@ function createRightProxy(type) {
* refer to the connection socket
* pass(req, socket, options, head)
*/
if(passes[i](req, res, options, head, this, cbl)) { // passes can return a truthy value to halt the loop
if(passes[i](req, res, requestOptions, head, this, cbl)) { // passes can return a truthy value to halt the loop
break;
}
}
Expand Down
45 changes: 45 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,49 @@ describe('#createProxyServer.web() using own http server', function () {

http.request('http://127.0.0.1:8081', function() {}).end();
});

it('should proxy requests to multiple servers with different options', function (done) {
var proxy = httpProxy.createProxyServer();

// proxies to two servers depending on url, rewriting the url as well
// http://127.0.0.1:8080/s1/ -> http://127.0.0.1:8081/
// http://127.0.0.1:8080/ -> http://127.0.0.1:8082/
function requestHandler(req, res) {
if (req.url.indexOf('/s1/') === 0) {
proxy.web(req, res, {
ignorePath: true,
target: 'http://127.0.0.1:8081' + req.url.substring(3)
});
} else {
proxy.web(req, res, {
target: 'http://127.0.0.1:8082'
});
}
}

var proxyServer = http.createServer(requestHandler);

var source1 = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8080');
expect(req.url).to.eql('/test1');
});

var source2 = http.createServer(function(req, res) {
source1.close();
source2.close();
proxyServer.close();
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8080');
expect(req.url).to.eql('/test2');
done();
});

proxyServer.listen('8080');
source1.listen('8081');
source2.listen('8082');

http.request('http://127.0.0.1:8080/s1/test1', function() {}).end();
http.request('http://127.0.0.1:8080/test2', function() {}).end();
});
});