gpt4 book ai didi

http - 如何拆分 header 值?

转载 作者:可可西里 更新时间:2023-11-01 16:07:46 24 4
gpt4 key购买 nike

我正在解析 HTTP header 。我想将 header 值拆分为有意义的数组。

例如,Cache-Control: no-cache, no-store 应该返回 ['no-cache','no-store']

HTTP RFC2616 说:

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded

但我不确定反过来是否正确——用逗号拆分安全吗?

我已经找到了一个导致问题的例子。例如,我的用户代理字符串是

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36

即,它在“KHTML”之后包含一个逗号。显然我没有超过一个用户代理,所以拆分这个 header 没有意义。

User-Agent 字符串是唯一的异常(exception),还是还有更多?

最佳答案

不,根据逗号拆分标题是不安全的。例如,Accept: foo/bar;p="A,B,C", bob/dole;x="apples,oranges" 是一个有效的 header ,但如果您尝试拆分逗号以获取 MIME 类型列表,您会得到无效的结果。

正确的答案是每个 header 都是使用 ABNF 指定的,其中大部分在各种 RFC 中,例如接受:defined in RFC7231 Section 5.3.2 .

我有这个具体问题并且 wrote a parsertested it on edge cases .不仅是parsing the header non-trivial , 解释它并给出 correct result is also non-trivial .

有些 header 比其他 header 更复杂,但基本上每个 header 都有自己的语法,正确(和安全)处理应该遵守这些语法。

关于http - 如何拆分 header 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29549299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com