Php 理解Etags HTTP头
我正在使用一个缓存库,它的功能如下所示。它试图从第5行的请求中获取ETag,但从未设置ETag 请求何时会有ETag?你会怎么设置它们 谢谢Php 理解Etags HTTP头,php,http,outputcache,cache-control,Php,Http,Outputcache,Cache Control,我正在使用一个缓存库,它的功能如下所示。它试图从第5行的请求中获取ETag,但从未设置ETag 请求何时会有ETag?你会怎么设置它们 谢谢 public function isNotModified(Request $request) { $lastModified = $request->headers->get('If-Modified-Since'); $notModified = false; if ($etags = $request->g
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;
}
以下内容仅适用于回复:
ETag响应标头字段提供所请求变量的实体标记的当前值
但是getEtags
方法可以是来自以下位置的标记:
如果任何实体标记与该资源上类似GET请求(不带If None match标头)响应中返回的实体的实体标记匹配,或者如果给定了“*
”且该资源存在任何当前实体,则服务器不得执行请求的方法,除非由于资源的修改日期与请求中If-Modified-Since标头字段中提供的日期不匹配而需要这样做。相反,如果请求方法是GET或HEAD,服务器应该以304(未修改)响应响应,包括匹配的实体之一的缓存相关头字段(特别是ETag)。对于所有其他请求方法,服务器的响应状态必须为412(前提条件失败)
这似乎与给定的代码完全匹配(我重新安排了第一句以适应代码):
最后一个表达式
(!$lastModified | |$this->headers->get('last-Modified')==$lastModified)
相当于!($lastModified&&$this->headers->get('Last-Modified')!=$lastModified)
更适合最后一句话的部分。如果只有响应头字段:“ETag响应头字段为请求的变量提供实体标记的当前值。”相关:实际上,ETag
仅出现在响应头中。请求中的contra头是如果不匹配
,如您在其他问题中所回答的。它正在检查If\u None\u Match头。这比我想象的要混乱得多。我只想缓存@迈克:如果你想强制浏览器缓存资源,只需在资源的响应中添加ETag
、Last Modified
和Expires
标题即可。然后,当后续请求传入时,如果自修改,则返回;如果不匹配,则返回,只需测试它们,并通过返回304或相关更改的资源进行相应处理。
// 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);