Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[contents] Skip caching if Cache-Control defines no-cache #945

Merged
merged 2 commits into from
Nov 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down