Skip to content

Commit 75cbb4d

Browse files
authored
allow only one host header (#507)
1 parent 2fc4c8f commit 75cbb4d

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

lib/httpRequestBuilder.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,21 @@ function requestBuilder (defaults) {
4545
const headers = reqData.headers
4646
const body = reqData.body
4747

48-
let host = getPropertyCaseInsensitive(reqData.headers, 'host') || reqData.host
48+
const headersDefinedHost = getPropertyCaseInsensitive(reqData.headers, 'host')
49+
let host = headersDefinedHost || reqData.host
4950

5051
if (!host) {
5152
const hostname = reqData.hostname
5253
const port = reqData.port
5354
host = hostname + ':' + port
5455
}
55-
56+
const baseReq = [
57+
`${method} ${path} HTTP/1.1`
58+
]
59+
if (!headersDefinedHost) {
60+
baseReq.push(`Host: ${host}`)
61+
}
62+
baseReq.push('Connection: keep-alive')
5663
if (reqData.auth) {
5764
const encodedAuth = Buffer.from(reqData.auth).toString('base64')
5865
headers.Authorization = `Basic ${encodedAuth}`
@@ -62,12 +69,6 @@ function requestBuilder (defaults) {
6269
throw new Error(`${method} HTTP method is not supported`)
6370
}
6471

65-
const baseReq = [
66-
`${method} ${path} HTTP/1.1`,
67-
`Host: ${host}`,
68-
'Connection: keep-alive'
69-
]
70-
7172
let bodyBuf
7273

7374
if (typeof body === 'string') {

test/httpRequestBuilder.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ test('request builder should add a Content-Length header when the body buffer ex
9999
'request is okay')
100100
})
101101

102+
test('request builder should add only one HOST header', (t) => {
103+
t.plan(1)
104+
105+
const opts = server.address()
106+
opts.method = 'POST'
107+
opts.headers = {
108+
Host: 'example.com'
109+
}
110+
111+
const build = RequestBuilder(opts)
112+
113+
const result = build({ body: 'body' })
114+
t.same(result,
115+
Buffer.from('POST / HTTP/1.1\r\nConnection: keep-alive\r\nHost: example.com\r\nContent-Length: 4\r\n\r\nbody'),
116+
'request is okay')
117+
})
118+
102119
test('request builder should add a Content-Length header with correct calculated value when the body buffer exists and idReplacement is enabled as a default override', (t) => {
103120
t.plan(1)
104121

0 commit comments

Comments
 (0)