将单个 HTTP 标头的多个值添加到请求或响应的标准

发布于 2024-09-06 19:06:01 字数 159 浏览 1 评论 0原文

如果我想添加值列表作为 HTTP 标头,是否有标准方法可以做到这一点?我在 RFC 822 中找不到任何内容(我可以轻松理解)。例如,是 逗号分隔值标准或分号分隔值。有标准吗?

例子:

Key: value1;value2;value3

If I want to add a list of values as an HTTP Header, is there a standard way to do this? I couldn't find anything (that I could easily understand) in RFC 822. For example, is
comma separated values standard or semi-colon separated values. Is there a standard at all?

Example:

Key: value1;value2;value3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

优雅的叶子 2024-09-13 19:06:01

您需要查看 HTTP 规范 RFC 2616< /a> 上面写着:

多个消息头字段
相同的字段名称可能出现在
一条消息当且仅当整个
该标头字段的字段值为
定义为逗号分隔的列表
[即#(值)]。这一定是可能的
组合多个标头字段
分成一个“字段名称:字段值”
配对,不改变语义
消息的,通过附加每个
第一个字段的后续字段值,
每个都用逗号分隔。订单
其中头字段具有相同的
因此收到的字段名称是
对解释具有重要意义
组合字段值,因此
代理不得更改顺序
当消息被发送时这些字段值
已转发。

这意味着您可以在具有不同值的响应中多次发送相同的标头,只要这些值可以使用逗号相互附加即可。这也意味着您可以通过用逗号连接多个值来在单个标头中发送多个值。

所以在你的情况下它将是:

Key: value1,value2,value3

You'll want to take a look at the HTTP spec RFC 2616 where it says:

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.

What this means is that you can send the same header multiple times in a response with different values, as long as those values can be appended to each other using a comma. This also means that you can send multiple values in a single header by concatenating them with commas.

So in your case it will be:

Key: value1,value2,value3
请叫√我孤独 2024-09-13 19:06:01

无论如何@marc-novakowski你缩小了“问题”的范围:)

通常(根据 HTTP 规范)我们使用逗号“,”将每个值与其他值分隔开

但我们将检查一个简单的情况:

Cookie-set: language=pl; expires=Sat, 15-Jul-2017 23:58:22 GMT; path=/; domain=x.com   
Cookie-set: id=123 expires=Sat, 15-Jul-2017 23:58:22 GMT; path=/; domain=x.com; httponly   

当彼此之间的值是用逗号分隔 - 出现昏迷的情况???

那么“客户”的责任是选择并决定策略,例如删除、合并 (如果合并如何)?

请查看 nsHttpHeaderArray 的 Mozilla 实现

https://github.com/bnoordhuis/mozilla-central/blob/master/netwerk/protocol/http/nsHttpHeaderArray.h#L185

在这种情况下,mozilla 选择使用换行符“\n”(对于某些标头字段名称)

我鼓励您在遇到这种情况时在常见的现有解决方案中进行搜索 - 因为它们提供了熟悉的方案

标志说明:

Cookie 不是 HTTP 标准的一部分。 Cookie 的定义是
自己的 RFC,6265(正式版本为 2965 和 2109)。甚至仅 HTTP 2 RFC
提到了cookie,但没有将它们定义为标准的一部分。 –
@mecki 8 月 25 日 18:56

请再看一遍句子:

根据 HTTP 规范,我们使用逗号 ',' 将每个值与其他值分隔开 - 这里没有 cookie 一词:)

也许我们需要精确地讨论 HEADER字段(s - 重复时)“Cookie-set”是一个标头字段并且它具有值..我们认为这些值是“COOKIE/S” - 因此客户端/服务器实现应该处理这样的“COOKIE/S”

查看值或名称对:)在HTTP 1/1 SPEC

https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2

by all means @marc-novakowski you narrowing the "problem" :)

normally (per HTTP spec) we delimit each value from the other using a comma ','

but we will examine a simple case:

Cookie-set: language=pl; expires=Sat, 15-Jul-2017 23:58:22 GMT; path=/; domain=x.com   
Cookie-set: id=123 expires=Sat, 15-Jul-2017 23:58:22 GMT; path=/; domain=x.com; httponly   

how do you join such headers when the values one from another are delimited with commas - case when coma can appear ???

then the "client" responsibility is to choose and decide the strategy eg drop, merg (if merg how)?

pleas take look at Mozilla implementation of nsHttpHeaderArray

https://github.com/bnoordhuis/mozilla-central/blob/master/netwerk/protocol/http/nsHttpHeaderArray.h#L185

mozilla choose to use a newline delimiter '\n' in this case (for certain header fields names)

I encourage when you face a such situation to search in common existing solutions - as they providing familiar scheme

flags explanations:

Cookies are no part of the HTTP standard. Cookies are defined in an
own RFC, 6265 (formally 2965 and 2109). Even the HTTP 2 RFC only
mentions cookies but does not define them as part of the standard. –
@mecki Aug 25 at 18:56

please look one more time for sentence:

per HTTP spec we delimit each value from other using a comma ',' - there is no word cookie here :)

maybe we need to precise we talk here about HEADER FIELD(s - when repeating them) "Cookie-set" is a header field and it has value .. those value we consider to be a "COOKIE/S" - thus client/server implementation should handle such "COOKIE/S"

SEE VALUES OR NAME PAIRS :) IN HTTP 1/1 SPEC

https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2

怎樣才叫好 2024-09-13 19:06:01

然而,并非所有具有相同字段名称的值都可以组合到字段值列表中。例如,在 RFC 7230 中,我们可以阅读

注意:在实践中,“Set-Cookie”头字段([RFC6265])经常
在响应消息中多次出现并且不使用
list语法,违反了上述对多个header的要求
具有相同名称的字段。因为它不能组合成一个
单字段值,接收者应该将“Set-Cookie”作为
处理标头字段时的特殊情况。 (见附录A.2.3
[Kri2001] 的详细信息。)

However not all values with the same field name may be combined into field values list. For example, in RFC 7230 we may read

Note: In practice, the "Set-Cookie" header field ([RFC6265]) often
appears multiple times in a response message and does not use the
list syntax, violating the above requirements on multiple header
fields with the same name. Since it cannot be combined into a
single field-value, recipients ought to handle "Set-Cookie" as a
special case while processing header fields. (See Appendix A.2.3
of [Kri2001] for details.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文