Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Address missing ETags on 304 responses as mentioned in #1192

  • Loading branch information...
commit 8de89415f1cd535f1fa9ba1ce24396d00b764813 1 parent 05441ab
@DaSourcerer authored
Showing with 21 additions and 37 deletions.
  1. +21 −37 framework/web/filters/CHttpCacheFilter.php
View
58 framework/web/filters/CHttpCacheFilter.php
@@ -25,6 +25,7 @@ class CHttpCacheFilter extends CFilter
* @var string|integer
*/
public $lastModified;
+
/**
* Expression for the last modification date. If set, this takes precedence over {@link lastModified}.
* @var string|callback
@@ -35,41 +36,39 @@ class CHttpCacheFilter extends CFilter
* @var mixed
*/
public $etagSeed;
+
/**
* Expression for the ETag seed. If set, this takes precedence over {@link etag}.
* @var string|callback
*/
public $etagSeedExpression;
+
/**
- * Http cache control headers. Set this to an empty string in order to keep this
- * header from being sent entirely.
+ * Http cache control headers
* @var string
*/
public $cacheControl = 'max-age=3600, public';
-
- /**
- * Performs the pre-action filtering.
- * @param CFilterChain $filterChain the filter chain that the filter is on.
- * @return boolean whether the filtering process should continue and the action should be executed.
- */
+
public function preFilter($filterChain)
{
// Only cache GET and HEAD requests
if(!in_array(Yii::app()->getRequest()->getRequestType(), array('GET', 'HEAD')))
return true;
-
+
$lastModified=$this->getLastModifiedValue();
$etag=$this->getEtagValue();
-
+
if($etag===false&&$lastModified===false)
return true;
-
+
+ if($etag)
+ header('ETag: '.$etag);
+
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])&&isset($_SERVER['HTTP_IF_NONE_MATCH']))
{
if($this->checkLastModified($lastModified)&&$this->checkEtag($etag))
{
$this->send304Header();
- $this->sendCacheControlHeader();
return false;
}
}
@@ -78,7 +77,6 @@ public function preFilter($filterChain)
if($this->checkLastModified($lastModified))
{
$this->send304Header();
- $this->sendCacheControlHeader();
return false;
}
}
@@ -87,7 +85,6 @@ public function preFilter($filterChain)
if($this->checkEtag($etag))
{
$this->send304Header();
- $this->sendCacheControlHeader();
return false;
}
@@ -95,16 +92,13 @@ public function preFilter($filterChain)
if($lastModified)
header('Last-Modified: '.date('r', $lastModified));
-
- if($etag)
- header('ETag: '.$etag);
-
- $this->sendCacheControlHeader();
+
+ header('Cache-Control: ' . $this->cacheControl);
return true;
}
-
+
/**
- * Gets the last modified value from either {@link lastModifiedExpression} or {@link lastModified}
+ * Gets the last modified value from either {@link lastModifiedExpression} or {@lastModified}
* and converts it into a unix timestamp if necessary
* @throws CException
* @return integer|boolean A unix timestamp or false if neither lastModified nor
@@ -122,7 +116,7 @@ protected function getLastModifiedValue()
array('{value}'=>$value)));
return $lastModified;
}
-
+
if($this->lastModified)
{
if(is_numeric($this->lastModified)&&$this->lastModified==(int)$this->lastModified)
@@ -133,7 +127,7 @@ protected function getLastModifiedValue()
}
return false;
}
-
+
/**
* Gets the ETag out of either {@link etagSeedExpression} or {@link etagSeed}
* @return string|boolean Either a quoted string serving as ETag or false if neither etagSeed nor etagSeedExpression have been set
@@ -146,20 +140,20 @@ protected function getEtagValue()
return $this->generateEtag($this->etagSeed);
return false;
}
-
+
/**
* Check if the etag supplied by the client matches our generated one
- * @param string $etag the supplied etag
+ * @param string $etag
* @return boolean true if the supplied etag matches $etag
*/
protected function checkEtag($etag)
{
return isset($_SERVER['HTTP_IF_NONE_MATCH'])&&$_SERVER['HTTP_IF_NONE_MATCH']==$etag;
}
-
+
/**
* Checks if the last modified date supplied by the client is still up to date
- * @param integer $lastModified the last modified date
+ * @param integer $lastModified
* @return boolean true if the last modified date sent by the client is newer or equal to $lastModified
*/
protected function checkLastModified($lastModified)
@@ -176,16 +170,6 @@ protected function send304Header()
}
/**
- * Sends the cache control header to the client
- * @see cacheControl
- * @since 1.1.12
- */
- protected function sendCacheControlHeader()
- {
- header('Cache-Control: '.$this->cacheControl, true);
- }
-
- /**
* Generates a quoted string out of the seed
* @param mixed $seed Seed for the ETag
*/
Please sign in to comment.
Something went wrong with that request. Please try again.