Skip to content

Commit daf66a7

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

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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

+1-1
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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var passes = exports;
2-
1+
var url = require('url')
2+
var passes = exports;
3+
var redirectRegex = /^30(1|2|7|8)$/;
34
/*!
45
* Array of passes.
56
*
@@ -43,6 +44,13 @@ var passes = exports;
4344
}
4445
},
4546

47+
function setRedirectHostRewrite(req, res, proxyRes, options) {
48+
if (options.hostRewrite && redirectRegex.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.

0 commit comments

Comments
 (0)