Skip to content

Commit

Permalink
Workaround for buggy servers returning wrong cache-control headers fo…
Browse files Browse the repository at this point in the history
…r 304 responses
  • Loading branch information
Alkarex committed Sep 20, 2024
1 parent b18c7d3 commit ecee8d4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/SimplePie.php
Original file line number Diff line number Diff line change
Expand Up @@ -2018,10 +2018,23 @@ protected function fetch_data(&$cache)
// is still valid.
$this->raw_data = false;
if (isset($file)) { // FreshRSS
$old_cache_control = $this->data['headers']['cache-control'] ?? '';

// Update cache metadata
$this->data['headers'] = array_map(function (array $values): string {
return implode(',', $values);
}, $file->get_headers());

// Workaround for buggy servers returning wrong cache-control headers for 304 responses
if (is_numeric($old_cache_control)) {
$new_cache_control = $this->data['headers']['cache-control'] ?? '';
if (!is_numeric($new_cache_control)) {
$new_cache_control = $this->cache_duration;
}
// Allow servers to return a shorter cache duration for 304 responses, but not longer
$this->data['headers']['cache-control'] = min((int)$old_cache_control, (int)$new_cache_control);
}

$this->data['cache_expiration_time'] = \SimplePie\HTTP\Utils::negociate_cache_expiration_time($this->data['headers'] ?? [], $this->cache_duration, $this->cache_duration_min, $this->cache_duration_max);
}
if (!$cache->set_data($cacheKey, $this->data, $this->cache_duration)) { // FreshRSS
Expand Down

0 comments on commit ecee8d4

Please sign in to comment.