Merge pull request #787 from mokafive/master

Fixes / additions to URL rewriting
This commit is contained in:
Jarrett Cruger 2015-04-01 12:20:37 -04:00
commit 5a969d077b
3 changed files with 150 additions and 45 deletions

View File

@ -43,6 +43,8 @@ module.exports.createProxyServer =
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
* auth : Basic authentication i.e. 'user:password' to compute an Authorization header.
* hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null.
* autoRewrite: rewrites the location host/port on (301/302/307/308) redirects based on requested host/port. Default: false.
* protocolRewrite: rewrites the location protocol on (301/302/307/308) redirects to 'http' or 'https'. Default: null.
* }
*
* NOTE: `options.ws` and `options.ssl` are optional.

View File

@ -47,11 +47,26 @@ var redirectRegex = /^30(1|2|7|8)$/;
},
function setRedirectHostRewrite(req, res, proxyRes, options) {
if (options.hostRewrite
if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite)
&& proxyRes.headers['location']
&& redirectRegex.test(proxyRes.statusCode)) {
var target = url.parse(options.target);
var u = url.parse(proxyRes.headers['location']);
// make sure the redirected host matches the target host before rewriting
if (target.host != u.host) {
return;
}
if (options.hostRewrite) {
u.host = options.hostRewrite;
} else if (options.autoRewrite) {
u.host = req.headers['host'];
}
if (options.protocolRewrite) {
u.protocol = options.protocolRewrite;
}
proxyRes.headers['location'] = u.format();
}
},

View File

@ -3,53 +3,141 @@ var httpProxy = require('../lib/http-proxy/passes/web-outgoing'),
describe('lib/http-proxy/passes/web-outgoing.js', function () {
describe('#setRedirectHostRewrite', function () {
context('rewrites location host to option', function() {
beforeEach(function() {
this.req = {
headers: {
host: "ext-auto.com"
}
};
this.proxyRes = {
statusCode: 301,
headers: {
location: "http://f.com/"
location: "http://backend.com/"
}
};
this.options = {
hostRewrite: "x.com"
target: "http://backend.com"
};
});
it('on 301', function() {
this.proxyRes.statusCode = 301;
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/');
context('rewrites location host with hostRewrite', function() {
beforeEach(function() {
this.options.hostRewrite = "ext-manual.com";
});
it('on 302', function() {
this.proxyRes.statusCode = 302;
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/');
[301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() {
this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://ext-manual.com/');
});
it('on 307', function() {
this.proxyRes.statusCode = 307;
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/');
});
it('on 308', function() {
this.proxyRes.statusCode = 308;
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/');
});
it('not on 200', function() {
this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/');
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('not when hostRewrite is unset', function() {
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, {});
expect(this.proxyRes.headers.location).to.eql('http://f.com/');
delete this.options.hostRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('takes precedence over autoRewrite', function() {
this.options.autoRewrite = true;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://ext-manual.com/');
});
it('not when the redirected location does not match target host', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = "http://some-other/";
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://some-other/');
});
it('not when the redirected location does not match target port', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = "http://backend.com:8080/";
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com:8080/');
});
});
context('rewrites location host with autoRewrite', function() {
beforeEach(function() {
this.options.autoRewrite = true;
});
[301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() {
this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://ext-auto.com/');
});
});
it('not on 200', function() {
this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('not when autoRewrite is unset', function() {
delete this.options.autoRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('not when the redirected location does not match target host', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = "http://some-other/";
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://some-other/');
});
it('not when the redirected location does not match target port', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = "http://backend.com:8080/";
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com:8080/');
});
});
context('rewrites location protocol with protocolRewrite', function() {
beforeEach(function() {
this.options.protocolRewrite = 'https';
});
[301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() {
this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://backend.com/');
});
});
it('not on 200', function() {
this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('not when protocolRewrite is unset', function() {
delete this.options.protocolRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('works together with hostRewrite', function() {
this.options.hostRewrite = 'ext-manual.com'
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://ext-manual.com/');
});
it('works together with autoRewrite', function() {
this.options.autoRewrite = true
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://ext-auto.com/');
});
});
});