File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -252,6 +252,7 @@ module.exports = {
252
252
get host ( ) {
253
253
const proxy = this . app . proxy ;
254
254
let host = proxy && this . get ( 'X-Forwarded-Host' ) ;
255
+ if ( this . req . httpVersionMajor >= 2 ) host = this . get ( ':authority' ) ;
255
256
host = host || this . get ( 'Host' ) ;
256
257
if ( ! host ) return '' ;
257
258
return host . split ( / \s * , \s * / ) [ 0 ] ;
Original file line number Diff line number Diff line change @@ -18,6 +18,39 @@ describe('req.host', () => {
18
18
} ) ;
19
19
} ) ;
20
20
21
+ describe ( 'when less then HTTP/2' , ( ) => {
22
+ it ( 'should not use :authority header' , ( ) => {
23
+ const req = request ( {
24
+ 'httpVersionMajor' : 1 ,
25
+ 'httpVersion' : '1.1'
26
+ } ) ;
27
+ req . header [ ':authority' ] = 'foo.com:3000' ;
28
+ req . header . host = 'bar.com:8000' ;
29
+ assert . equal ( req . host , 'bar.com:8000' ) ;
30
+ } ) ;
31
+ } ) ;
32
+
33
+ describe ( 'when HTTP/2' , ( ) => {
34
+ it ( 'should use :authority header' , ( ) => {
35
+ const req = request ( {
36
+ 'httpVersionMajor' : 2 ,
37
+ 'httpVersion' : '2.0'
38
+ } ) ;
39
+ req . header [ ':authority' ] = 'foo.com:3000' ;
40
+ req . header . host = 'bar.com:8000' ;
41
+ assert . equal ( req . host , 'foo.com:3000' ) ;
42
+ } ) ;
43
+
44
+ it ( 'should use host header as fallback' , ( ) => {
45
+ const req = request ( {
46
+ 'httpVersionMajor' : 2 ,
47
+ 'httpVersion' : '2.0'
48
+ } ) ;
49
+ req . header . host = 'bar.com:8000' ;
50
+ assert . equal ( req . host , 'bar.com:8000' ) ;
51
+ } ) ;
52
+ } ) ;
53
+
21
54
describe ( 'when X-Forwarded-Host is present' , ( ) => {
22
55
describe ( 'and proxy is not trusted' , ( ) => {
23
56
it ( 'should be ignored' , ( ) => {
You can’t perform that action at this time.
0 commit comments