Skip to content

Commit

Permalink
Accept host header case-insensitively (#463)
Browse files Browse the repository at this point in the history
* Accept host header case-insensitively

This makes CLI -H 'Host: foo' work where previously it did not identify
the header correctly because it was case-sensitive.

* try lowercase

* rename and remove second case-insensitive lookup

* restart ci

* disable fail-fast on ci

* restart ci

* restart ci
  • Loading branch information
silverwind authored Sep 23, 2022
1 parent 5533aaf commit edaef8d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
permissions:
contents: read
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [14, 16, 18]
Expand Down
4 changes: 3 additions & 1 deletion lib/httpRequestBuilder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const methods = require('./httpMethods')
const { getPropertyCaseInsensitive } = require('./util')

// this is a build request factory, that curries the build request function
// and sets the default for it
Expand Down Expand Up @@ -44,7 +45,8 @@ function requestBuilder (defaults) {
const headers = reqData.headers
const body = reqData.body

let host = reqData.headers.host || reqData.host
let host = getPropertyCaseInsensitive(reqData.headers, 'host') || reqData.host

if (!host) {
const hostname = reqData.hostname
const port = reqData.port
Expand Down
8 changes: 7 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
const semver = require('semver')
const hasWorkerSupport = semver.gte(process.versions.node, '11.7.0')

module.exports = { hasWorkerSupport }
function getPropertyCaseInsensitive (obj, key) {
for (const objKey of Object.keys(obj)) {
if (objKey.toLowerCase() === key.toLowerCase()) return obj[objKey]
}
}

module.exports = { hasWorkerSupport, getPropertyCaseInsensitive }
19 changes: 19 additions & 0 deletions test/httpClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,25 @@ test('client supports host custom header', (t) => {
})
})

test('client supports host custom header with mixed case', (t) => {
t.plan(2)

const opts = server.address()
opts.headers = {
Host: 'www.autocannon.com'
}
const client = new Client(opts)

server.once('request', (req, res) => {
t.equal(req.headers.host, 'www.autocannon.com', 'host header matches')
})

client.on('response', (statusCode, length) => {
t.equal(statusCode, 200, 'status code matches')
client.destroy()
})
})

test('client supports response trailers', (t) => {
t.plan(3)

Expand Down

0 comments on commit edaef8d

Please sign in to comment.