Skip to content

Commit

Permalink
feat: use :authority header of http2 requests as host (#1262)
Browse files Browse the repository at this point in the history
close #1174
  • Loading branch information
mgjm authored and fengmk2 committed Oct 23, 2018
1 parent 9146024 commit 9c5c58b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ module.exports = {
get host() {
const proxy = this.app.proxy;
let host = proxy && this.get('X-Forwarded-Host');
if (this.req.httpVersionMajor >= 2) host = this.get(':authority');
host = host || this.get('Host');
if (!host) return '';
return host.split(/\s*,\s*/)[0];
Expand Down
33 changes: 33 additions & 0 deletions test/request/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('and proxy is not trusted', () => {
it('should be ignored', () => {
Expand Down

0 comments on commit 9c5c58b

Please sign in to comment.