Description
- Version: v10.15.3 & v12.9.1, I suspect many others
- Platform:
Linux zapp 4.15.0-58-generic #64-Ubuntu SMP Tue Aug 6 11:12:41 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
- Subsystem: HTTP
Passing the host header as an array to http.get
results in hostHeader.startsWith is not a function
.
This happens for arrays with multiple items, which is invalid of course, or a single item, which the docs & rest of the API seems to imply is valid; other header APIs treat setting header values of [x]
and x
as equivalent everywhere afaict.
Repro:
> http.get('http://example.com', { headers: { host: ['example.com'] } });
Thrown:
TypeError: hostHeader.startsWith is not a function
at calculateServerName (_http_agent.js:247:20)
at Agent.addRequest (_http_agent.js:162:26)
at new ClientRequest (_http_client.js:276:16)
at request (http.js:44:10)
at Object.get (http.js:48:15)
It's arguable that this shouldn't be allowed at all, but it is convenient. In my case, I'm reducing to build headers from an array of inputs, and storing all values as arrays during that process, since it's easy & consistent. To avoid this bug I have to special case the host header and undo that. Other libraries including node-fetch do the same.
If we do want to consider this invalid, it would be nice if there was at least a clearer error. For example, setting the host header to undefined:
> http.get('http://example.com', { headers: { host: undefined } });
Thrown:
TypeError [ERR_HTTP_INVALID_HEADER_VALUE]: Invalid value "undefined" for header "host"
at ClientRequest.setHeader (_http_outgoing.js:488:3)
at new ClientRequest (_http_client.js:221:14)
at request (http.js:44:10)
at Object.get (http.js:48:15)
The error itself is thrown in _http_agent
here: https://github.com/nodejs/node/blob/master/lib/_http_agent.js#L247