Skip to content

Commit 70fa8ad

Browse files
committed
Merge pull request #723 from whitecolor/change-origin-option
Added changeOrigin option with test and docs
2 parents f70015f + 796ab0b commit 70fa8ad

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

lib/http-proxy.js

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports.createProxyServer =
4040
* toProxy: <true/false, explicitly specify if we are proxying to another proxy>
4141
* prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
4242
* localAddress : <Local interface string to bind for outgoing connections>
43+
* changeOrigin: true/false, Default: false - changes the origin of the host header to the target URL>
4344
* }
4445
*
4546
* NOTE: `options.ws` and `options.ssl` are optional.

lib/http-proxy/common.js

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
7474

7575
outgoing.path = common.urlJoin(targetPath, outgoingPath);
7676

77+
if (options.changeOrigin) {
78+
outgoing.headers.host = outgoing.host;
79+
}
80+
7781
return outgoing;
7882
};
7983

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

+26
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,30 @@ describe('#createProxyServer.web() using own http server', function () {
264264
source.listen('8080');
265265
http.request('http://127.0.0.1:8086', function() {}).end();
266266
});
267+
268+
it('should proxy the request and handle changeOrigin option', function (done) {
269+
var proxy = httpProxy.createProxyServer({
270+
target: 'http://127.0.0.1:8080',
271+
changeOrigin: true
272+
});
273+
274+
function requestHandler(req, res) {
275+
proxy.web(req, res);
276+
}
277+
278+
var proxyServer = http.createServer(requestHandler);
279+
280+
var source = http.createServer(function(req, res) {
281+
source.close();
282+
proxyServer.close();
283+
expect(req.method).to.eql('GET');
284+
expect(req.headers.host.split(':')[1]).to.eql('8080');
285+
done();
286+
});
287+
288+
proxyServer.listen('8081');
289+
source.listen('8080');
290+
291+
http.request('http://127.0.0.1:8081', function() {}).end();
292+
});
267293
});

0 commit comments

Comments
 (0)