mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Fixing issues with HTTP cache control
This commit is contained in:
parent
96f87b4140
commit
33d93c2a31
@ -308,7 +308,7 @@ class HTTP {
|
|||||||
/**
|
/**
|
||||||
* Add the appropriate caching headers to the response, including If-Modified-Since / 304 handling.
|
* Add the appropriate caching headers to the response, including If-Modified-Since / 304 handling.
|
||||||
*
|
*
|
||||||
* @param SS_HTTPResponse The SS_HTTPResponse object to augment. Omitted the argument or passing a string is
|
* @param SS_HTTPResponse $body The SS_HTTPResponse object to augment. Omitted the argument or passing a string is
|
||||||
* deprecated; in these cases, the headers are output directly.
|
* deprecated; in these cases, the headers are output directly.
|
||||||
*/
|
*/
|
||||||
public static function add_cache_headers($body = null) {
|
public static function add_cache_headers($body = null) {
|
||||||
@ -328,21 +328,17 @@ class HTTP {
|
|||||||
// us trying.
|
// us trying.
|
||||||
if(headers_sent() && !$body) return;
|
if(headers_sent() && !$body) return;
|
||||||
|
|
||||||
// Popuplate $responseHeaders with all the headers that we want to build
|
// Populate $responseHeaders with all the headers that we want to build
|
||||||
$responseHeaders = array();
|
$responseHeaders = array();
|
||||||
|
|
||||||
$config = Config::inst();
|
$config = Config::inst();
|
||||||
$cacheControlHeaders = Config::inst()->get('HTTP', 'cache_control');
|
$cacheControlHeaders = Config::inst()->get('HTTP', 'cache_control');
|
||||||
|
|
||||||
|
|
||||||
// currently using a config setting to cancel this, seems to be so taht the CMS caches ajax requests
|
// currently using a config setting to cancel this, seems to be so that the CMS caches ajax requests
|
||||||
if(function_exists('apache_request_headers') && $config->get(get_called_class(), 'cache_ajax_requests')) {
|
if(function_exists('apache_request_headers') && $config->get(get_called_class(), 'cache_ajax_requests')) {
|
||||||
$requestHeaders = apache_request_headers();
|
$requestHeaders = array_change_key_case(apache_request_headers(), CASE_LOWER);
|
||||||
|
|
||||||
if(isset($requestHeaders['X-Requested-With']) && $requestHeaders['X-Requested-With']=='XMLHttpRequest') {
|
|
||||||
$cacheAge = 0;
|
|
||||||
}
|
|
||||||
// bdc: now we must check for DUMB IE6:
|
|
||||||
if(isset($requestHeaders['x-requested-with']) && $requestHeaders['x-requested-with']=='XMLHttpRequest') {
|
if(isset($requestHeaders['x-requested-with']) && $requestHeaders['x-requested-with']=='XMLHttpRequest') {
|
||||||
$cacheAge = 0;
|
$cacheAge = 0;
|
||||||
}
|
}
|
||||||
@ -383,13 +379,16 @@ class HTTP {
|
|||||||
foreach($cacheControlHeaders as $header => $value) {
|
foreach($cacheControlHeaders as $header => $value) {
|
||||||
if(is_null($value)) {
|
if(is_null($value)) {
|
||||||
unset($cacheControlHeaders[$header]);
|
unset($cacheControlHeaders[$header]);
|
||||||
} elseif(is_bool($value) || $value === "true") {
|
} elseif((is_bool($value) && $value) || $value === "true") {
|
||||||
$cacheControlHeaders[$header] = $header;
|
$cacheControlHeaders[$header] = $header;
|
||||||
} else {
|
} else {
|
||||||
$cacheControlHeaders[$header] = $header."=".$value;
|
$cacheControlHeaders[$header] = $header."=".$value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$responseHeaders['Cache-Control'] = implode(', ', $cacheControlHeaders);
|
||||||
|
unset($cacheControlHeaders, $header, $value);
|
||||||
|
|
||||||
if(self::$modification_date && $cacheAge > 0) {
|
if(self::$modification_date && $cacheAge > 0) {
|
||||||
$responseHeaders["Last-Modified"] = self::gmt_date(self::$modification_date);
|
$responseHeaders["Last-Modified"] = self::gmt_date(self::$modification_date);
|
||||||
|
|
||||||
|
@ -7,6 +7,26 @@
|
|||||||
*/
|
*/
|
||||||
class HTTPTest extends FunctionalTest {
|
class HTTPTest extends FunctionalTest {
|
||||||
|
|
||||||
|
public function testAddCacheHeaders() {
|
||||||
|
$body = "<html><head></head><body><h1>Mysite</h1></body></html>";
|
||||||
|
$response = new SS_HTTPResponse($body, 200);
|
||||||
|
$this->assertEmpty($response->getHeader('Cache-Control'));
|
||||||
|
|
||||||
|
HTTP::set_cache_age(30);
|
||||||
|
HTTP::add_cache_headers($response);
|
||||||
|
|
||||||
|
$this->assertNotEmpty($response->getHeader('Cache-Control'));
|
||||||
|
|
||||||
|
Config::inst()->update('Director', 'environment_type', 'dev');
|
||||||
|
HTTP::add_cache_headers($response);
|
||||||
|
$this->assertContains('max-age=0', $response->getHeader('Cache-Control'));
|
||||||
|
|
||||||
|
Config::inst()->update('Director', 'environment_type', 'live');
|
||||||
|
HTTP::add_cache_headers($response);
|
||||||
|
$this->assertContains('max-age=30', explode(', ', $response->getHeader('Cache-Control')));
|
||||||
|
$this->assertNotContains('max-age=0', $response->getHeader('Cache-Control'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests {@link HTTP::getLinksIn()}
|
* Tests {@link HTTP::getLinksIn()}
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user