[contents] Skip caching if the remote server requests no caching (#945)

* Skip caching if Cache-Control defines no-cache
* Skip caching if Cache-Control defines no-store
This commit is contained in:
LogMANOriginal 2018-11-28 17:36:28 +01:00 committed by GitHub
parent 263e8872ea
commit 3d301fc4ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -122,6 +122,20 @@ function getContents($url, $header = array(), $opts = array()){
case 200: // Contents received case 200: // Contents received
Debug::log('New contents received'); Debug::log('New contents received');
$data = substr($data, $headerSize); $data = substr($data, $headerSize);
// Disable caching if the server responds with "Cache-Control: no-cache"
// or "Cache-Control: no-store"
$finalHeader = array_change_key_case($finalHeader, CASE_LOWER);
if(array_key_exists('cache-control', $finalHeader)) {
Debug::log('Server responded with "Cache-Control" header');
$directives = explode(',', $finalHeader['cache-control']);
$directives = array_map('trim', $directives);
if(in_array('no-cache', $directives)
|| in_array('no-store', $directives)) { // Skip caching
Debug::log('Skip server side caching');
return $data;
}
}
Debug::log('Store response to cache');
$cache->saveData($data); $cache->saveData($data);
return $data; return $data;
case 304: // Not modified, use cached data case 304: // Not modified, use cached data