我的问题是当我收到 HTTP 304“未修改”时如何回复bothif-none-match 和 if-modified-since 来自代理/客户端请求。
来自 RFC 2616 第 14.26 节(http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26 ):
如果没有实体标签匹配,那么
服务器可以执行请求的
方法就像 If-None-Match 标头
字段不存在,但也必须
忽略任何 If-Modified-Since 标头
请求中的字段。也就是说,如果
没有实体标签匹配,则服务器
不得返回 304(未修改)
回复。
我不确定是否理解这个说法......
- PHP 中的“如果没有实体标签匹配”
$_SERVER['HTTP_IF_NONE_MATCH']
与我之前发送的 ETag 相比?
- 如果我正确理解了这个声明,只要没有列出任何 ETag
$_SERVER['HTTP_IF_NONE_MATCH']
与我的 ETag 匹配,我停止所有验证并正常提供页面。
任何人都可以用伪代码(或 PHP 代码)翻译此 RFC 部分和/或回答我上面的 2 点?
EDIT 1:谢谢圣沃兰的回答。你(或其他人)能告诉我我在这 6 点上是否正确:
-
格式为$_SERVER['HTTP_IF_NONE_MATCH']
可以是:
a)如果无匹配:“xyzzy”、“r2d2xxxx”、“c3piozzzz”
b) 如果无匹配:“xyzzy”
and NOT:
c) 如果无匹配:“xyzzy,r2d2xxxx,c3piozzzz”
If !array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)
,anyTagMatched() 返回 NULL。
一旦 ETag 进入$_SERVER['HTTP_IF_NONE_MATCH']
匹配我的文档 ETag,anyTagMatched() 返回 TRUE。
如果没有 Etag$_SERVER['HTTP_IF_NONE_MATCH']
与我的文档 ETag 匹配,anyTagMatched()
返回 FALSE。
If $_SERVER['HTTP_IF_MODIFIED_SINCE']
已设置并匹配我的文档“上次修改”日期isExpired()
返回 FALSE,否则返回 TRUE。
立刻anyTagMatched()
返回 TRUE,我发出 304。如果 anyTagMatched() 返回 NULL 并且isExpired()
返回 FALSE 我可以发出 304。在任何其他情况下,我都会正常提供页面(我还发出最新的 Last-Modified 和 ETag 标头)。
这应该放在最后(移动是为了更好看)。
$anyTagMatched = anyTagMatched() ;
if( $anyTagMatched || ( ( null === $anyTagMatched ) && !isExpired() ) ) {
notModified() ;
}
// Output content
伪代码(需要审查):
<?php
/**
* Calculates eTag for the current resource.
*/
function calculateTag() {
}
/**
* Gets date of the most recent change.
*/
function lastChanged() {
}
/**
* TRUE if any tag matched
* FALSE if none matched
* NULL if header is not specified
*/
function anyTagMatched() {
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) :
false ;
if( false !== $if_none_match ) {
$tags = split( ", ", $if_none_match ) ;
$myTag = calculateTag() ;
foreach( $tags as $tag ) {
if( $tag == $myTag ) return true ;
}
return false ;
}
return null ;
}
function isExpired() {
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
false;
if( false !== $if_modified_since ) {
// Compare time here; pseudocode.
return ( $if_modified_since < lastChanged() ) ;
}
return true ;
}
function notModified() {
header('HTTP/1.0 304 Not Modified');
exit ;
}
主要回答here.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)