Skip to content

Commit 8f3dbbc

Browse files
authored
HTTP/2 authority header missing port (#101)
1 parent e5d7007 commit 8f3dbbc

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

lib/http2/request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Http2Request extends EventEmitter {
7070
...headers,
7171
[http2.constants.HTTP2_HEADER_PATH]: options.path || '/',
7272
[http2.constants.HTTP2_HEADER_METHOD]: _options.method,
73-
[http2.constants.HTTP2_HEADER_AUTHORITY]: headers['host'] || _options.host
73+
[http2.constants.HTTP2_HEADER_AUTHORITY]: _options.host + (_options.port !== 443 ? ':' + options.port : '')
7474
}
7575

7676
if (options.uri.isUnix || headers['host'] === 'unix' || _options.host === 'unix') {

tests/test-body-http2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ tape('test HEAD Request', function (t) {
187187
},
188188
function (err, res, body) {
189189
t.error(err)
190-
t.equal(res.statusCode, 400)
190+
t.equal(res.statusCode, 200)
191191
t.end()
192192
}
193193
)

tests/test-headers-http2.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict'
2+
3+
var server = require('./server')
4+
var request = require('../index')
5+
var tape = require('tape')
6+
var destroyable = require('server-destroy')
7+
8+
var s = server.createHttp2Server()
9+
destroyable(s)
10+
11+
s.on('/redirect/from', function (req, res) {
12+
res.writeHead(301, {
13+
location: '/redirect/to'
14+
})
15+
res.end()
16+
})
17+
18+
s.on('/redirect/to', function (req, res) {
19+
res.end('ok')
20+
})
21+
22+
s.on('/headers.json', function (req, res) {
23+
res.writeHead(200, {
24+
'Content-Type': 'application/json'
25+
})
26+
27+
res.end(JSON.stringify(req.headers))
28+
})
29+
30+
tape('setup', function (t) {
31+
s.listen(0, function () {
32+
tape('cleanup', function (t) {
33+
s.destroy(function () {
34+
t.end()
35+
})
36+
})
37+
t.end()
38+
})
39+
})
40+
41+
tape('undefined headers', function (t) {
42+
request({
43+
url: s.url + '/headers.json',
44+
headers: {
45+
'X-TEST-1': 'test1',
46+
'X-TEST-2': undefined
47+
},
48+
json: true,
49+
strictSSL: false,
50+
protocolVersion: 'http2'
51+
}, function (err, res, body) {
52+
t.equal(err, null)
53+
t.equal(body['x-test-1'], 'test1')
54+
t.equal(typeof body['x-test-2'], 'undefined')
55+
t.end()
56+
})
57+
})
58+
59+
tape('preserve port in authority header if non-standard port', function (t) {
60+
request({
61+
url: s.url + '/headers.json',
62+
strictSSL: false,
63+
protocolVersion: 'http2'
64+
}, function (err, res, body, debug) {
65+
t.equal(err, null)
66+
console.log()
67+
t.equal(debug[0].request.headers.find(({key}) => key === ':authority').value, 'localhost:' + s.port)
68+
t.end()
69+
})
70+
})
71+
72+
tape('strip port in authority header if explicit standard port (:443) & protocol (HTTPS)', function (t) {
73+
request({
74+
url: 'https://localhost:443/headers.json',
75+
strictSSL: false,
76+
protocolVersion: 'http2'
77+
}, function (_err, res, body, debug) {
78+
t.equal(debug[0].request.headers.find(({key}) => key === ':authority').value, 'localhost')
79+
t.end()
80+
})
81+
})
82+
83+
tape('strip port in authority header if implicit standard port & protocol (HTTPS)', function (t) {
84+
request({
85+
url: 'https://localhost/headers.json',
86+
strictSSL: false,
87+
protocolVersion: 'http2'
88+
}, function (_err, res, body, debug) {
89+
t.equal(debug[0].request.headers.find(({key}) => key === ':authority').value, 'localhost')
90+
t.end()
91+
})
92+
})

0 commit comments

Comments
 (0)