带有host-only-flag的cookie是否应该替换没有它的cookie?

Ben*_*min 6 cookies http

RFC 6265声明用户代理在接收Set-Cookie标头时应以下列方式继续:

如果设置了Domain属性:

  • 将cookie的域设置为domain-attribute.
  • 将cookie的host-only-flag设置为false.

如果设置Domain属性:

  • 将cookie的域设置为规范化的请求主机.
  • 将cookie的host-only-flag设置为true.

这一点都很清楚.这段话引起了混乱:

如果用户代理接收到具有相同cookie名称,域值和路径值的新cookie作为其已存储的cookie,则将现有cookie逐出并替换为新cookie.

我们举一个例子,在域上收到两个cookie www.example.com:

Set-cookie: name=value
Set-Cookie: name=value; Domain=www.example.com
Run Code Online (Sandbox Code Playgroud)

两个cookie的域(和路径)将是相同的,但第一个将设置为host-only-flag true,第二个设置为false.

阅读RFC时,看起来比较两个cookie并不重要,无论如何它们应该被视为等效,但我不确定我的解释是否正确.

用户代理应该用第二个cookie替换第一个cookie,还是应该存储它们?

aki*_*irk 5

困扰您的段落是关于为cookie 分配新值能力(以及更改/刷新cookie过期日期).如果没有这样编写,HTTP客户端将需要存储多个具有相同名称的cookie,并且需要决定在下一个请求时发送到HTTP服务器的另一个标准.

关于你问题的第二部分:

如果在同一请求中指定了这两个cookie,则第二个cookie"获胜",因此host-only-flag = false将存储具有该cookie的cookie .

如果这两个cookie来自单独的请求,则第二个会覆盖第一个,因为它们匹配cookie-name(指定),domain-value(一旦指定,一旦派生)和path-value(派生).存储它们时,浏览器的cookie数据库中的条目仅在host-only-flag中有所不同.

当客户端向服务器发出新请求时,此host-only-flag生效(来自RFC6265的代码段):

The user agent MUST use an algorithm equivalent to the following
algorithm to compute the "cookie-string" from a cookie store and a
request-uri:

1.  Let cookie-list be the set of cookies from the cookie store that
    meets all of the following requirements:

    *  Either:

           The cookie's host-only-flag is true and the canonicalized
           request-host is identical to the cookie's domain.

        Or:

           The cookie's host-only-flag is false and the canonicalized
           request-host domain-matches the cookie's domain.
Run Code Online (Sandbox Code Playgroud)

详细的细节是如何比较域.匹配算法在5.1.3节中规定.

基本上,如果使用前导"."指定域,则可以使cookie对所有子域都有效.

但是,当省略域时(因此服务器从请求中暗示),情况永远不会是这种情况,因为域中始终需要相同的匹配.

进一步研究确定:

在实践中,浏览器存储已在cookie中指定的域,其前缀为.(因为www.example.com它将存储.www.example.com),以便请求也subdomain.www.example.com将返回该cookie.如果未指定域,.则将存储没有前置的普通域,因此对子域的请求将不包含该cookie.