Issue: In the setHeadersFromArray function the headers are set in a 2D array format, which is not as described in the NodeJS Http docs.
The res/req object composed of the class http.IncomingMessage has the rawHeaders properties. It contains the key and value pairs of each header in either object or array form:
//object
{ "content-length": "123",
"content-type": "text/plain",
"connection": "keep-alive",
"host": "example.com",
"accept": "*/*" }
//array
[ 'ConTent-Length', '123456',
'content-LENGTH', '123',
'content-type', 'text/plain',
'CONNECTION', 'keep-alive',
'Host', 'example.com',
'accepT', '*/*' ]
Note that the array is in the format of key, value, key2, value2, ...], not a 2D array [[key, value], [key, value], [...]]
However, setHeadersFromArray treats the array as 2D
function setHeadersFromArray (res, headers) {
for (var i = 0; i < headers.length; i++) {
res.setHeader(headers[i][0], headers[i][1])
}
}
This results in an error when res.writeHead is used to set the headers in their correct form:
function addPoweredBy() {
if (!this.getHeader("X-Powered-By")) {
this.setHeader("X-Powered-By", "Node.js");
}
}
function handleRequest(req, res) {
onHeaders(res, addPoweredBy);
//ERR_HTTP_INVALID_HEADER_VALUE
res.writeHead(200, [
["X-A", "A"],
["X-B", "B"],
]);
res.end();
}
Error thrown:
TypeError [ERR_INVALID_HTTP_TOKEN]: Header name must be a valid HTTP token ["[ 'X-A', 'A' ]"]
No error is thrown when the 2D array format is used (because that's how the current setHeadersFromArray is written:
//works
res.writeHead(200, ["X-A", "A","X-B","B" ])
Also, no error is thrown when res.writeHead is used with object form (the current implementation is correct):
function setHeadersFromObject (res, headers) {
var keys = Object.keys(headers)
for (var i = 0; i < keys.length; i++) {
var k = keys[i]
if (k) res.setHeader(k, headers[k])
}
}
...
//works
res.writeHead(200, {"X-A": "A", "X-B": "B"})
Note: This issue pops up only under these circumstances:
- The headers argument to
res.writeHead() is in the 2D array format. Other format works: object, and 1D array. This 1D array structure is what's inconsistent with the docs.
res.writeHead() is used with onHeaders. Without res.writeHead(), the headers are not explicitly set. Most dependent libraries use only onHeaders (e.g., morgan), which is probably why this issue doesn't show up.
Any thoughts?
Issue: In the
setHeadersFromArrayfunction the headers are set in a 2D array format, which is not as described in the NodeJS Http docs.The res/req object composed of the class http.IncomingMessage has the
rawHeadersproperties. It contains the key and value pairs of each header in either object or array form:Note that the array is in the format of
key, value, key2, value2, ...], not a 2D array[[key, value], [key, value], [...]]However,
setHeadersFromArraytreats the array as 2DThis results in an error when
res.writeHeadis used to set the headers in their correct form:No error is thrown when the 2D array format is used (because that's how the current
setHeadersFromArrayis written:Also, no error is thrown when
res.writeHeadis used with object form (the current implementation is correct):Note: This issue pops up only under these circumstances:
res.writeHead()is in the 2D array format. Other format works: object, and 1D array. This 1D array structure is what's inconsistent with the docs.res.writeHead()is used withonHeaders. Withoutres.writeHead(), the headers are not explicitly set. Most dependent libraries use onlyonHeaders(e.g., morgan), which is probably why this issue doesn't show up.Any thoughts?