Home > Software design >  Wrong headers order and casing when performing an HTTP/2.0 request with Node JS
Wrong headers order and casing when performing an HTTP/2.0 request with Node JS

Time:03-12

What i need (TL;DR):

I need to the request headers to be exactly the same as i specify in the http2 request options, it should persist the name casing and the order of all headers.

How can i explicitly set this order or modify them before the request is sent? If is not possible with the default 'http2' library, please let me know any other lib that is capable of doing this.

If you need a little more context, keep reading!

What happens now:

When i perform a HTTP/2.0 request with 'http2' library, all the headers names seems to be normalized to lowerecase and it's order is changed before the request is sent.

Exemple code that i use to perform a HTTP/2.0 request:

const http2 = require('http2');

const client = http2.connect('http://localhost:8000');

const request = client.request({
  ':method': 'GET',
  ':authority': 'localhost:8000',
  ':scheme': 'https',
  ':path': '/',
  'sec-ch-ua-mobile': '?0',
  'sec-ch-ua': `" Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"`,
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36',
  'Cookie': 'foo=bar',
});

request.end()

The headers order i expect to recieve on the server:

:method: GET
:authority': localhost:8000
:scheme': https
:path: /
sec-ch-ua-mobile: ?0
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
Cookie: foo=bar

What i actually recieve on the server:

:path: /
:scheme: https
:authority: localhost:8000
:method: GET
sec-ch-ua-mobile: ?0
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
cookie: foo=bar

Note that the casing of all headers and the order of :path, :scheme, :authority and :method headers are different from what i expect them to be...

I already investigated the source code of http2, seems it is not possible to explicitly define the order of this 4 headers and disable this naming lowercase normalization. Many of the existing HTTP/2.0 libraries for node relly on the native lib, thus, carring this problem with them.

But here we goes nothing, how can i explicitly set this header order or modify them before the request is sent to match what i expect? Exists any library or library binding that allow a way to achive this in Node JS?

PS: already tried node-libcurl and it have the same problem.

Thanks!

CodePudding user response:

From the HTTP/2 spec:

Just as in HTTP/1.x, header field names are strings of ASCII characters that are compared in a case-insensitive fashion. However, header field names MUST be converted to lowercase prior to their encoding in HTTP/2. A request or response containing uppercase header field names MUST be treated as malformed

I’m not aware of any similar restrictions around ordering, however also would say that depending on the order is a bad idea as I’m not aware of it being explicitly preserved either.

  • Related