【问题标题】:Are multiple Cookie headers allowed in an HTTP request?HTTP 请求中是否允许多个 Cookie 标头?
【发布时间】:2013-04-24 17:20:59
【问题描述】:

通常,浏览器将 cookie 分组到单个 Cookie 标头中,例如:

Cookie: a=1; b=2

标准是否允许将这些作为单独的标头发送,例如:

Cookie: a=1
Cookie: b=2

或者他们总是必须在同一行?

【问题讨论】:

    标签: http cookies


    【解决方案1】:

    在查找有关该主题的详细信息时偶然访问了此页面。来自HTTP State Management MechanismRFC 6265 的引用应该会让事情更清楚:

    5.4。 Cookie 标头

    当用户代理生成一个 HTTP 请求时,用户代理必须 不要附加多个 Cookie 标头字段。

    貌似使用多个Cookie headers,其实是禁止的!

    【讨论】:

    • 这些疯狂的新奇 RFC。 :)
    • 请注意,server 可以使用多个 Set-Cookie 标头进行响应:tools.ietf.org/html/rfc6265#page-7
    • 为什么要投反对票,正如 OP 在 HTTP 请求而非 HTTP 响应方面特别询问的那样。 @JeffDavenport
    • 对于像 Jeff 这样来自 Google 的人,OP 显示您可以通过用“;”分隔多个 vars (不需要空格)
    • @HawkeyeParker - 可以并不意味着正确。如果值由单个服务器设置,我认为Set-Cookie:a=b;c=d;Set-Cookie:a=b; Set-Cookie:c=d; 更正确。规范说服务器不应该将多个 Set-Cookie 标头字段折叠到一个 field 中,但它可以将多个 Set-Cookie 标头字段添加到一个 response 中。这在现实世界中意味着,当代理服务器传递响应时,如果该代理设置了 cookie,它应该使用单独的 Set-Cookie 标头。
    【解决方案2】:

    它现在在 HTTP/2 (RFC 7540) 中被允许,它指定:

        8.1.2.5.  Compressing the Cookie Header Field
    
       The Cookie header field [COOKIE] uses a semi-colon (";") to delimit
       cookie-pairs (or "crumbs").  This header field doesn't follow the
       list construction rules in HTTP (see [RFC7230], Section 3.2.2), which
       prevents cookie-pairs from being separated into different name-value
       pairs.  This can significantly reduce compression efficiency as
       individual cookie-pairs are updated.
    
       To allow for better compression efficiency, the Cookie header field
       MAY be split into separate header fields, each with one or more
       cookie-pairs.  If there are multiple Cookie header fields after
       decompression, these MUST be concatenated into a single octet string
       using the two-octet delimiter of 0x3B, 0x20 (the ASCII string "; ")
       before being passed into a non-HTTP/2 context, such as an HTTP/1.1
       connection, or a generic HTTP server application.
    
       Therefore, the following two lists of Cookie header fields are
       semantically equivalent.
    
         cookie: a=b; c=d; e=f
    
         cookie: a=b
         cookie: c=d
         cookie: e=f
    

    【讨论】:

      猜你喜欢
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 2019-04-11
      • 2011-06-24
      • 2015-10-16
      • 1970-01-01
      相关资源
      最近更新 更多