From 3ddc831f2077f9fdb398c0cb25feefc2864c2fe7 Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Tue, 22 Jul 2008 23:05:06 +0000 Subject: [PATCH] Completed HTTP_ConditionalGet change to accept 'maxAge' not prevent conditional GETs. Also 304 responses now have the full Cache-Control/Last-Modified/ETag headers so max-age will be honored after each 304 response. --- lib/HTTP/ConditionalGet.php | 5 +---- web/test/HTTP_ConditionalGet/2.php | 1 - web/test/HTTP_ConditionalGet/3.php | 3 +-- web/test/HTTP_ConditionalGet/4.php | 4 +--- web/test/HTTP_ConditionalGet/5.php | 15 +++++++-------- web/test/HTTP_ConditionalGet/_include.php | 7 +++---- web/test/HTTP_ConditionalGet/index.php | 3 +-- web/test/test_HTTP_ConditionalGet.php | 15 ++++++++++++--- 8 files changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/HTTP/ConditionalGet.php b/lib/HTTP/ConditionalGet.php index 4c68435..d95120f 100644 --- a/lib/HTTP/ConditionalGet.php +++ b/lib/HTTP/ConditionalGet.php @@ -225,10 +225,7 @@ class HTTP_ConditionalGet { } $isValid = ($this->resourceMatchedEtag() || $this->resourceNotModified()); if ($isValid) { - // overwrite headers, only need 304 - $this->_headers = array( - '_responseCode' => 'HTTP/1.0 304 Not Modified' - ); + $this->_headers['_responseCode'] = 'HTTP/1.0 304 Not Modified'; } return $isValid; } diff --git a/web/test/HTTP_ConditionalGet/2.php b/web/test/HTTP_ConditionalGet/2.php index df7b650..e258db3 100644 --- a/web/test/HTTP_ConditionalGet/2.php +++ b/web/test/HTTP_ConditionalGet/2.php @@ -42,4 +42,3 @@ $cg->setContentLength(strlen($content)); $cg->sendHeaders(); send_slowly($content); -?> \ No newline at end of file diff --git a/web/test/HTTP_ConditionalGet/3.php b/web/test/HTTP_ConditionalGet/3.php index 1d14aa4..bb0ebdf 100644 --- a/web/test/HTTP_ConditionalGet/3.php +++ b/web/test/HTTP_ConditionalGet/3.php @@ -37,5 +37,4 @@ $cg->setContentLength(strlen($content)); $cg->sendHeaders(); send_slowly($content); - -?> \ No newline at end of file + diff --git a/web/test/HTTP_ConditionalGet/4.php b/web/test/HTTP_ConditionalGet/4.php index 6120e9a..74faeac 100644 --- a/web/test/HTTP_ConditionalGet/4.php +++ b/web/test/HTTP_ConditionalGet/4.php @@ -31,7 +31,7 @@ $content = get_content(array( ,'explain' => $explain )); -require '../../Encoder.php'; +require 'HTTP/Encoder.php'; $he = new HTTP_Encoder(array( 'content' => get_content(array( 'title' => $title @@ -44,5 +44,3 @@ $he->encode(); // connection $he->sendHeaders(); send_slowly($he->getContent()); - -?> \ No newline at end of file diff --git a/web/test/HTTP_ConditionalGet/5.php b/web/test/HTTP_ConditionalGet/5.php index 41abfc6..315e3ce 100644 --- a/web/test/HTTP_ConditionalGet/5.php +++ b/web/test/HTTP_ConditionalGet/5.php @@ -5,18 +5,18 @@ require 'HTTP/ConditionalGet.php'; // far expires $cg = new HTTP_ConditionalGet(array( - 'setExpires' => (time() + 86400 * 365) // 1 yr + 'maxAge' => 20 + ,'lastModifiedTime' => filemtime(__FILE__) )); $cg->sendHeaders(); // generate, send content -$title = 'Expires date is known'; +$title = 'Last-Modified + Expires'; $explain = ' -

Here we set "setExpires" to a timestamp or GMT date string. This results in -$cacheIsValid always being false, so content is always served, but -with an Expires header. -

Note: This isn\'t a conditional GET, but is useful if you\'re -used to the HTTP_ConditionalGet workflow already.

+

Here we set a static "lastModifiedTime" and "maxAge" to 20. The browser +will consider this document fresh for 20 seconds, then revalidate its cache. After +the 304 response, the cache will be good for another 20 seconds. Unless you force +a reload, there will only be 304 responses for this page after the initial download. '; require '_include.php'; @@ -25,4 +25,3 @@ echo get_content(array( ,'explain' => $explain )); -?> \ No newline at end of file diff --git a/web/test/HTTP_ConditionalGet/_include.php b/web/test/HTTP_ConditionalGet/_include.php index 6bc28b9..9b65ae1 100644 --- a/web/test/HTTP_ConditionalGet/_include.php +++ b/web/test/HTTP_ConditionalGet/_include.php @@ -7,7 +7,7 @@ function send_slowly($content) while ($chunk = array_shift($content)) { sleep(1); echo $chunk; - ob_flush(); + ob_get_level() && ob_flush(); flush(); } } @@ -31,7 +31,7 @@ function get_content($data)

  • Last-Modified is known : add Content-Length
  • Last-Modified is unknown : use hash of content for ETag
  • ConditionalGet + Encoder
  • -
  • Expires date is known
  • +
  • Last-Modified + Expires
  • Notes

    How to distinguish 200 and 304 responses

    @@ -61,5 +61,4 @@ to verify headers and content being sent.

    ob_end_clean(); return $content; } - -?> \ No newline at end of file + diff --git a/web/test/HTTP_ConditionalGet/index.php b/web/test/HTTP_ConditionalGet/index.php index ef81b5f..33c4af6 100644 --- a/web/test/HTTP_ConditionalGet/index.php +++ b/web/test/HTTP_ConditionalGet/index.php @@ -33,5 +33,4 @@ echo send_slowly(get_content(array( 'title' => $title ,'explain' => $explain ))); - -?> \ No newline at end of file + diff --git a/web/test/test_HTTP_ConditionalGet.php b/web/test/test_HTTP_ConditionalGet.php index 162eb58..5a73b8a 100644 --- a/web/test/test_HTTP_ConditionalGet.php +++ b/web/test/test_HTTP_ConditionalGet.php @@ -16,7 +16,10 @@ function test_HTTP_ConditionalGet() ,'inm' => null ,'ims' => $gmtTime ,'exp' => array( - '_responseCode' => 'HTTP/1.0 304 Not Modified' + 'Last-Modified' => $gmtTime + ,'ETag' => "\"{$lmTime}pri\"" + ,'Cache-Control' => 'max-age=0, private, must-revalidate' + ,'_responseCode' => 'HTTP/1.0 304 Not Modified' ,'isValid' => true ) ) @@ -25,7 +28,10 @@ function test_HTTP_ConditionalGet() ,'inm' => null ,'ims' => $gmtTime . ';' ,'exp' => array( - '_responseCode' => 'HTTP/1.0 304 Not Modified' + 'Last-Modified' => $gmtTime + ,'ETag' => "\"{$lmTime}pri\"" + ,'Cache-Control' => 'max-age=0, private, must-revalidate' + ,'_responseCode' => 'HTTP/1.0 304 Not Modified' ,'isValid' => true ) ) @@ -34,7 +40,10 @@ function test_HTTP_ConditionalGet() ,'inm' => "\"badEtagFoo\", \"{$lmTime}pri\"" ,'ims' => null ,'exp' => array( - '_responseCode' => 'HTTP/1.0 304 Not Modified' + 'Last-Modified' => $gmtTime + ,'ETag' => "\"{$lmTime}pri\"" + ,'Cache-Control' => 'max-age=0, private, must-revalidate' + ,'_responseCode' => 'HTTP/1.0 304 Not Modified' ,'isValid' => true ) )