feat: use :authority header of http2 requests as host (#1262)

close #1174
This commit is contained in:
Martin Michaelis 2018-10-23 07:21:20 +02:00 committed by fengmk2
parent 9146024e10
commit 9c5c58b183
2 changed files with 34 additions and 0 deletions

View file

@ -252,6 +252,7 @@ module.exports = {
get host() { get host() {
const proxy = this.app.proxy; const proxy = this.app.proxy;
let host = proxy && this.get('X-Forwarded-Host'); let host = proxy && this.get('X-Forwarded-Host');
if (this.req.httpVersionMajor >= 2) host = this.get(':authority');
host = host || this.get('Host'); host = host || this.get('Host');
if (!host) return ''; if (!host) return '';
return host.split(/\s*,\s*/)[0]; return host.split(/\s*,\s*/)[0];

View file

@ -18,6 +18,39 @@ describe('req.host', () => {
}); });
}); });
describe('when less then HTTP/2', () => {
it('should not use :authority header', () => {
const req = request({
'httpVersionMajor': 1,
'httpVersion': '1.1'
});
req.header[':authority'] = 'foo.com:3000';
req.header.host = 'bar.com:8000';
assert.equal(req.host, 'bar.com:8000');
});
});
describe('when HTTP/2', () => {
it('should use :authority header', () => {
const req = request({
'httpVersionMajor': 2,
'httpVersion': '2.0'
});
req.header[':authority'] = 'foo.com:3000';
req.header.host = 'bar.com:8000';
assert.equal(req.host, 'foo.com:3000');
});
it('should use host header as fallback', () => {
const req = request({
'httpVersionMajor': 2,
'httpVersion': '2.0'
});
req.header.host = 'bar.com:8000';
assert.equal(req.host, 'bar.com:8000');
});
});
describe('when X-Forwarded-Host is present', () => { describe('when X-Forwarded-Host is present', () => {
describe('and proxy is not trusted', () => { describe('and proxy is not trusted', () => {
it('should be ignored', () => { it('should be ignored', () => {