HTTP中的if-none-match和if-modified-since以及304澄清 [英] HTTP if-none-match and if-modified-since and 304 clarification in PHP

查看:398
本文介绍了HTTP中的if-none-match和if-modified-since以及304澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是当我从代理/客户端请求收到两者 if-none-match和if-modified-since时,如何回复HTTP 304Not Modified。

My question is about how to reply a HTTP 304 "Not Modified" when I receive both if-none-match and if-modified-since from a proxy/client request.

来自RFC 2616第14.26节( http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26 ):

From RFC 2616 secttion 14.26 ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26 ):


如果没有实体标签匹配,那么
服务器可以执行请求的
方法,就像If-None-Match标头
字段不存在一样,但也必须
忽略请求中的任何If-Modified-Since标头
字段。也就是说,如果
没有实体标签匹配,那么服务器
不得返回304(未修改)
响应。

If none of the entity tags match, then the server MAY perform the requested method as if the If-None-Match header field did not exist, but MUST also ignore any If-Modified-Since header field(s) in the request. That is, if no entity tags match, then the server MUST NOT return a 304 (Not Modified) response.

我不确定理解这句话...

I am not sure to understand this statement...


  1. 如果没有实体标签匹配在PHP中,他们是否谈到 $ _ SERVER ['HTTP_IF_NONE_MATCH'] 与我之前发送的ETag相比?

  2. 如果我理解正确的话这句话,只要 $ _ SERVER ['HTTP_IF_NONE_MATCH'] 中没有列出的ETag与我的ETag匹配,我就会停止所有验证并正常提供页面。

  1. "If none of the entity tags match" in PHP do they speak of $_SERVER['HTTP_IF_NONE_MATCH'] vs. my ETags that I sent earlier?
  2. If I understand correctly this statement, as soon as none of the ETags listed in $_SERVER['HTTP_IF_NONE_MATCH'] match my ETags, I stop all verifications and serve the page normally.

任何人都可以用伪代码(或PHP代码)翻译这个RFC部分和/或回答我的2分以上?

Anyone can translate this RFC part in pseudo-code (or PHP code) and/or answer my 2 points above?

编辑1:
感谢St.Woland的回答。您(或其他任何人)可以告诉我这些6分是否正确:

EDIT 1: Thank you St.Woland for your answer. Can you (or anyone else) tell me if I'm correct on these 6 points:


  1. <$ c的格式$ c> $ _ SERVER ['HTTP_IF_NONE_MATCH'] 可以是:

a)If-None-Match:xyzzy,r2d2xxxx ,c3piozzzz

a)If-None-Match: "xyzzy", "r2d2xxxx", "c3piozzzz"

b)如果 - 无匹配:xyzzy

b) If-None-Match: "xyzzy"

和NOT:

c)如果 - 无匹配:xyzzy,r2d2xxxx,c3piozzzz

c) If-None-Match: "xyzzy, r2d2xxxx, c3piozzzz"

如果!array_key_exists('HTTP_IF_NONE_MATCH',$ _SERVER),anyTagMatched()将返回NULL。

If !array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER), anyTagMatched() returns NULL.

只要 $ _ SERVER ['HTTP_IF_NONE_MATCH'] 中的ETag与我的文档ETag匹配,anyTagMatched()就会返回TRUE。

As soon as an ETag in $_SERVER['HTTP_IF_NONE_MATCH'] match my document ETag, anyTagMatched() returns TRUE.

如果 $ _ SERVER ['HTTP_IF_NONE_MATCH'] 中没有Etags与我的文档ETag匹配, anyTagMatched()返回FALSE。

If none of the Etags in $_SERVER['HTTP_IF_NONE_MATCH'] are matching my document ETag, anyTagMatched() returns FALSE.

如果 $ _ SERVER ['HTTP_IF_MODIFIED_SINCE'] 已设置并匹配我的文档上次修改日期 isExpired()返回FALSE否则返回TRUE。

If $_SERVER['HTTP_IF_MODIFIED_SINCE'] is set and matches my document "last modified" date isExpired() returns FALSE, otherwise return TRUE.

只要 anyTagMatched()返回TRUE,我就发出304。如果anyTagMatched()返回NULL并且 isExpired()返回FALSE我可以发出304.在任何其他情况下,我正常服务我的页面(我也发布了最新版本) date Last-Modified和ETag标题)。

As soon as anyTagMatched() returns TRUE, I issue a 304. If anyTagMatched() returned NULL and isExpired() returned FALSE I can issue a 304. In any other situation I serve my page as normal (I also issue the up-to-date Last-Modified and ETag headers).


推荐答案

这应该放最后(移动以获得更好的外观)。

This should be put in the end (moved for better look).

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

主要答案这里

这篇关于HTTP中的if-none-match和if-modified-since以及304澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆