了解 Etags HTTP 标头
我正在使用一个具有如下功能的缓存库。它尝试从第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ETag 标头字段仅用于回复:
但是
getEtags
方法可能是来自 If-None-Match 标头字段:这似乎与给定的代码完全匹配(我重新排列了第一句话以适合代码):
最后一个表达式
(!$lastModified || $this->headers->get('Last-Modified') = = $lastModified)
相当于!($lastModified && $this->headers->get('Last-Modified') != $lastModified)
适合最后一句话部分更好。The ETag header field is for responses only:
But the
getEtags
method could be the tags from the If-None-Match header field:This seems to match the given code exactly (I rearranged the first sentence to fit the code):
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.