Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current setHeadersFromArray is inconsistent with NodeJS Http docs #15

Open
Zen-cronic opened this issue May 11, 2024 · 0 comments
Open

Comments

@Zen-cronic
Copy link

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:

  1. 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.
  2. 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant