From 3d301fc4ee3872e1e42a902a9fa718fccced6df2 Mon Sep 17 00:00:00 2001 From: LogMANOriginal Date: Wed, 28 Nov 2018 17:36:28 +0100 Subject: [PATCH] [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 --- lib/contents.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/contents.php b/lib/contents.php index 4693057e..9c0b1224 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -122,6 +122,20 @@ function getContents($url, $header = array(), $opts = array()){ case 200: // Contents received Debug::log('New contents received'); $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); return $data; case 304: // Not modified, use cached data