Skip to content

Commit

Permalink
Address missing ETags on 304 responses as mentioned in #1192
Browse files Browse the repository at this point in the history
  • Loading branch information
DaSourcerer committed Aug 13, 2012
1 parent 05441ab commit 8de8941
Showing 1 changed file with 21 additions and 37 deletions.
58 changes: 21 additions & 37 deletions framework/web/filters/CHttpCacheFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
Expand All @@ -78,7 +77,6 @@ public function preFilter($filterChain)
if($this->checkLastModified($lastModified))
{
$this->send304Header();
$this->sendCacheControlHeader();
return false;
}
}
Expand All @@ -87,24 +85,20 @@ public function preFilter($filterChain)
if($this->checkEtag($etag))
{
$this->send304Header();
$this->sendCacheControlHeader();
return false;
}

}

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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -175,16 +169,6 @@ protected function send304Header()
header('HTTP/1.1 304 Not Modified');
}

/**
* 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
Expand Down

0 comments on commit 8de8941

Please sign in to comment.