HTTP请求中是否允许多个Cookie标头?

Ben*_*min 87 cookies http

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

Cookie: a=1; b=2
Run Code Online (Sandbox Code Playgroud)

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

Cookie: a=1
Cookie: b=2
Run Code Online (Sandbox Code Playgroud)

或者他们总是必须在同一条线上?

Jam*_*ong 118

在查找此主题的详细信息时,可以查看此页面.从A报价HTTP State Management Mechanism,RFC 6265应该让事情更清晰:

5.4.Cookie标头

当用户代理生成HTTP请求时,用户代理不得附加多个Cookie头字段.

它看起来像使用多个Cookie,事实上,禁止!

  • 请注意,_server_可以使用多个`Set-Cookie`标题进行响应:http://tools.ietf.org/html/rfc6265#page-7 (12认同)
  • 这些疯狂的新奇RFC.:) (8认同)
  • 为什么投反对票,因为 OP 在 HTTP 请求方面特别询问,而不是 HTTP 响应。@杰夫达文波特 (2认同)
  • 对于那些像杰夫一样从谷歌来到这里的人来说,OP显示你可以通过用";"分隔它们来拥有多个变量.(不需要空间) (2认同)
  • @HawkeyeParker - *can*并不意味着*正确*.我认为`Set-Cookie:a = b; c = d;`比`Set-Cookie更正确:a = b; Set-Cookie:c = d;`如果值是由单个服务器设置的.规范说服务器不应该将多个Set-Cookie头字段折叠成一个**字段**,但它可以将多个Set-Cookie头字段添加到一个**响应**中.在现实世界中,这意味着当代理服务器传递响应时,如果该代理设置了cookie,它应该使用单独的Set-Cookie头. (2认同)

小智 18

它现在允许在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
Run Code Online (Sandbox Code Playgroud)