From e766658ee3b9e70988b79d99d75124857f2a7ccc Mon Sep 17 00:00:00 2001 From: Jeremy Shipman Date: Sat, 13 Jun 2015 12:12:36 +1200 Subject: [PATCH] API: Allow HTTP Cache Headers to be customized --- _config/config.yml | 7 ++++++- control/HTTP.php | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/_config/config.yml b/_config/config.yml index a6fa72c8d..31dbd1682 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -5,4 +5,9 @@ Upload: # Replace an existing file rather than renaming the new one. replaceFile: false MySQLDatabase: - connection_charset: utf8 \ No newline at end of file + connection_charset: utf8 +HTTP: + cache_control: + max-age: 0 + must-revalidate: "true" + no-transform: "true" \ No newline at end of file diff --git a/control/HTTP.php b/control/HTTP.php index 83b5e6310..9993c8e52 100644 --- a/control/HTTP.php +++ b/control/HTTP.php @@ -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; } }