Skip to content

Add support for auto host rewriting and protocol rewriting #1

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

Merged
merged 1 commit into from
Feb 5, 2015
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
2 changes: 2 additions & 0 deletions lib/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module.exports.createProxyServer =
* localAddress : <Local interface string to bind for outgoing connections>
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
* 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.
Expand Down
12 changes: 10 additions & 2 deletions lib/http-proxy/passes/web-outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ 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 u = url.parse(proxyRes.headers['location']);
u.host = options.hostRewrite;
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();
}
},
Expand Down
125 changes: 94 additions & 31 deletions test/lib-http-proxy-passes-web-outgoing-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,118 @@ 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.proxyRes = {
statusCode: 301,
headers: {
location: "http://f.com/"
}
};
beforeEach(function() {
this.req = {
headers: {
host: "x2.com"
}
};
this.proxyRes = {
statusCode: 301,
headers: {
location: "http://f.com/"
}
};
});

context('rewrites location host with hostRewrite', function() {
beforeEach(function() {
this.options = {
hostRewrite: "x.com"
};
});
[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://'+this.options.hostRewrite+'/');
});
});

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+'/');
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://f.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+'/');
it('not when hostRewrite is unset', function() {
delete this.options.hostRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/');
});

it('on 307', function() {
this.proxyRes.statusCode = 307;
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
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://'+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+'/');
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://'+this.req.headers.host+'/');
});
});

it('not on 200', function() {
this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/');
});

it('not when hostRewrite is unset', function() {
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, {});
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://f.com/');
});
});

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://f.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://f.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://f.com/');
});

it('works together with hostRewrite', function() {
this.options.hostRewrite = 'x.com'
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://x.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://x2.com/');
});
});
});

describe('#setConnection', function () {
Expand All @@ -64,7 +127,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
}
}, {}, proxyRes);

expect(proxyRes.headers.connection).to.eql('close');
expect(proxyRes.headers.connection).to.eql('close');
});

it('set the right connection with 1.0 - req.connection', function() {
Expand All @@ -76,7 +139,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
}
}, {}, proxyRes);

expect(proxyRes.headers.connection).to.eql('hey');
expect(proxyRes.headers.connection).to.eql('hey');
});

it('set the right connection - req.connection', function() {
Expand All @@ -88,7 +151,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
}
}, {}, proxyRes);

expect(proxyRes.headers.connection).to.eql('hola');
expect(proxyRes.headers.connection).to.eql('hola');
});

it('set the right connection - `keep-alive`', function() {
Expand All @@ -100,7 +163,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
}
}, {}, proxyRes);

expect(proxyRes.headers.connection).to.eql('keep-alive');
expect(proxyRes.headers.connection).to.eql('keep-alive');
});

});
Expand Down Expand Up @@ -153,4 +216,4 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
});

});