Skip to content

Commit a1fc98e

Browse files
committed
Allow optional redirect host rewriting.
1 parent aba505d commit a1fc98e

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ proxyServer.listen(8015);
325325
* **secure**: true/false, if you want to verify the SSL Certs
326326
* **xfwd**: true/false, adds x-forward headers
327327
* **toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies)
328+
* **hostRewrite**: rewrites the location hostname on (301/302/307/308) redirects.
328329

329330
If you are using the `proxyServer.listen` method, the following options are also applicable:
330331

lib/http-proxy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module.exports.createProxyServer =
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>
4343
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
44+
* hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null.
4445
* }
4546
*
4647
* NOTE: `options.ws` and `options.ssl` are optional.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ web_o = Object.keys(web_o).map(function(pass) {
144144
proxyReq.on('response', function(proxyRes) {
145145
if(server) { server.emit('proxyRes', proxyRes, req, res); }
146146
for(var i=0; i < web_o.length; i++) {
147-
if(web_o[i](req, res, proxyRes)) { break; }
147+
if(web_o[i](req, res, proxyRes, options)) { break; }
148148
}
149149

150150
// Allow us to listen when the proxy has completed

lib/http-proxy/passes/web-outgoing.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var url = require('url')
12
var passes = exports;
23

34
/*!
@@ -43,6 +44,13 @@ var passes = exports;
4344
}
4445
},
4546

47+
function setRedirectHostRewrite(req, res, proxyRes, options) {
48+
if (options.hostRewrite && /^30(1|2|7|8)$/.test(proxyRes.statusCode)) {
49+
var u = url.parse(proxyRes.headers['location']);
50+
u.host = options.hostRewrite;
51+
proxyRes.headers['location'] = u.format();
52+
}
53+
},
4654
/**
4755
* Copy headers from proxyResponse to response
4856
* set each header in response object.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
1212
}
1313
}, {}, proxyRes);
1414

15-
expect(proxyRes.headers.connection).to.eql('close');
15+
expect(proxyRes.headers.connection).to.eql('close');
1616
});
1717

1818
it('set the right connection with 1.0 - req.connection', function() {
@@ -24,7 +24,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
2424
}
2525
}, {}, proxyRes);
2626

27-
expect(proxyRes.headers.connection).to.eql('hey');
27+
expect(proxyRes.headers.connection).to.eql('hey');
2828
});
2929

3030
it('set the right connection - req.connection', function() {
@@ -36,7 +36,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
3636
}
3737
}, {}, proxyRes);
3838

39-
expect(proxyRes.headers.connection).to.eql('hola');
39+
expect(proxyRes.headers.connection).to.eql('hola');
4040
});
4141

4242
it('set the right connection - `keep-alive`', function() {
@@ -48,7 +48,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
4848
}
4949
}, {}, proxyRes);
5050

51-
expect(proxyRes.headers.connection).to.eql('keep-alive');
51+
expect(proxyRes.headers.connection).to.eql('keep-alive');
5252
});
5353

5454
});
@@ -101,4 +101,4 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
101101
});
102102

103103
});
104-
104+

0 commit comments

Comments
 (0)