了解 Etags HTTP 标头

发布于 2024-10-18 09:48:26 字数 717 浏览 1 评论 0原文

我正在使用一个具有如下功能的缓存库。它尝试从第 5 行的请求中获取 eTags,但 eTags 从未设置。

请求什么时候会有电子标签?你会如何设置它们?

谢谢。

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;
}

I am using a caching library that has the function seen below. It is attempting to grab eTags from the Request on the 5th line but the eTags are never set.

When would a REQUEST have eTags? and how might you set them?

Thanks.

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;
}

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

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

发布评论

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

评论(1

恰似旧人归 2024-10-25 09:48:26

ETag 标头字段仅用于回复:

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

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

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

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

//   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) 适合最后一句话部分更好。

The ETag header field is for responses only:

The ETag response-header field provides the current value of the entity tag for the requested variant.

But the getEtags method could be the tags from the If-None-Match header field:

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, or if "*" is given and any current entity exists for that resource, then the server MUST NOT perform the requested method, 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. Instead, if the request method was GET or HEAD, the server SHOULD respond with a 304 (Not Modified) response, including the cache- related header fields (particularly ETag) of one of the entities that matched. For all other request methods, the server MUST respond with a status of 412 (Precondition Failed).

This seems to match the given code exactly (I rearranged the first sentence to fit the code):

//   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);

The last expression (!$lastModified || $this->headers->get('Last-Modified') == $lastModified) is equivalent to !($lastModified && $this->headers->get('Last-Modified') != $lastModified) that fits the last sentence part better.

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