API: Allow HTTP Cache Headers to be customized

This commit is contained in:
Jeremy Shipman 2015-06-13 12:12:36 +12:00 committed by Will Rossiter
parent 65fce1cb07
commit e766658ee3
2 changed files with 24 additions and 4 deletions

View File

@ -5,4 +5,9 @@ Upload:
# Replace an existing file rather than renaming the new one.
replaceFile: false
MySQLDatabase:
connection_charset: utf8
connection_charset: utf8
HTTP:
cache_control:
max-age: 0
must-revalidate: "true"
no-transform: "true"

View File

@ -330,10 +330,15 @@ class HTTP {
// Popuplate $responseHeaders with all the headers that we want to build
$responseHeaders = array();
$config = Config::inst();
$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
if(function_exists('apache_request_headers') && $config->get(get_called_class(), 'cache_ajax_requests')) {
$requestHeaders = apache_request_headers();
if(isset($requestHeaders['X-Requested-With']) && $requestHeaders['X-Requested-With']=='XMLHttpRequest') {
$cacheAge = 0;
}
@ -344,7 +349,7 @@ class HTTP {
}
if($cacheAge > 0) {
$responseHeaders["Cache-Control"] = "max-age={$cacheAge}, must-revalidate, no-transform";
$cacheControlHeaders['max-age'] = self::$cache_age;
$responseHeaders["Pragma"] = "";
// To do: User-Agent should only be added in situations where you *are* actually
@ -368,10 +373,20 @@ class HTTP {
// IE6-IE8 have problems saving files when https and no-cache are used
// (http://support.microsoft.com/kb/323308)
// Note: this is also fixable by ticking "Do not save encrypted pages to disk" in advanced options.
$responseHeaders["Cache-Control"] = "max-age=3, must-revalidate, no-transform";
$cacheControlHeaders['max-age'] = 3;
$responseHeaders["Pragma"] = "";
} else {
$responseHeaders["Cache-Control"] = "no-cache, max-age=0, must-revalidate, no-transform";
$cacheControlHeaders['no-cache'] = "true";
}
}
foreach($cacheControlHeaders as $header => $value) {
if(is_null($value)) {
unset($cacheControlHeaders[$header]);
} elseif(is_bool($value) || $value === "true") {
$cacheControlHeaders[$header] = $header;
} else {
$cacheControlHeaders[$header] = $header."=".$value;
}
}