使用 Node JS 执行 HTTP/2.0 请求时错误的标头顺序和大小写
我需要什么(TL;DR):
我需要请求标头与我在 http2 请求选项中指定的完全相同,它应该保留名称大小写和所有标头的顺序。
在发送请求之前,如何显式设置此顺序或修改它们?如果默认的“http2”库无法实现,请让我知道任何其他能够执行此操作的库。
如果您需要更多背景信息,请继续阅读!
现在发生了什么:
当我使用“http2”库执行 HTTP/2.0 请求时,所有标头名称似乎都已标准化为小写,并且在发送请求之前其顺序已更改。
我用来执行 HTTP/2.0 请求的示例代码:
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()
在服务器上我期望收到的标头顺序:
: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
在服务器上我实际收到的内容:
: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
请注意大小写所有标头的顺序以及 :path、:scheme、:authority 和 :method 标头的顺序与我期望的不同...
我已经调查了 http2 的源代码,似乎不可能显式定义顺序的这 4 个标头并禁用此命名小写规范化。许多现有的 Node 的 HTTP/2.0 库都依赖于本机库,因此存在这个问题。
但在这里我们什么也没做,如何在发送请求之前显式设置此标头顺序或修改它们以匹配我的期望?存在允许在 Node 中实现此目的的任何库或库绑定JS?
PS:已经尝试过node-libcurl,它也有同样的问题。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 HTTP/2 规范:
我不知道有关排序的任何类似限制,但是也会说取决于顺序是一个坏主意,因为我不知道它被显式保留任何一个。
From the HTTP/2 spec:
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.