0

我正在使用具有以下功能的缓存库。它试图从第 5 行的请求中获取 eTag,但从未设置 eTag。

REQUEST 什么时候会有 eTag?以及如何设置它们?

谢谢。

public function isNotModified(Request $request)
{
    $lastModified = $request->headers->get('If-Modified-Since');

    $notModified = false;
    if ($etags = $request->getEtags()) {
        $notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);
    } elseif ($lastModified) {
        $notModified = $lastModified == $this->headers->get('Last-Modified');
    }

    if ($notModified) {
        $this->setNotModified();
    }

    return $notModified;
}
4

1 回答 1

2

ETag头字段仅用于响应:

ETag响应头字段为请求的变体提供实体标签的当前值。

但该getEtags方法可能是来自If-None-Match标头字段的标签:

如果任何实体标签与该资源上的类似 GET 请求(没有If-None-Match标头)的响应中返回的实体标签匹配,或者如果*给出“”并且任何当前实体如果该资源存在,则服务器不得执行请求的方法,除非需要这样做,因为资源的修改日期与请求中的If-Modified-Since头字段中提供的日期不匹配。相反,如果请求方法是 GET 或 HEAD,服务器应该以 304(未修改)响应进行响应,包括与缓存相关的头字段(特别是ETag) 匹配的实体之一。对于所有其他请求方法,服务器必须以状态 412(Precondition Failed)响应。

这似乎与给定的代码完全匹配(我重新排列了第一句以适应代码):

//   the server MUST NOT perform the requested method
$notModified = (
    //   if any of the entity tags match the entity tag of the entity that
    //   would have been returned in the response to a similar GET request
    //   (without the If-None-Match header) on that resource
    in_array($this->getEtag(), $etags)
    //   or if "*" is given and any current entity exists for that resource
    || in_array('*', $etags))
    //   unless required to do so because the resource's modification
    //   date fails to match that supplied in an If-Modified-Since header
    //   field in the request.
    && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);

最后一个表达式(!$lastModified || $this->headers->get('Last-Modified') == $lastModified)相当于更!($lastModified && $this->headers->get('Last-Modified') != $lastModified)适合最后一个句子的部分。

于 2011-02-22T19:40:01.430 回答