From ecee8d486a0028899e75b7cf67766759a7787c4c Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Fri, 20 Sep 2024 16:32:33 +0200 Subject: [PATCH] Workaround for buggy servers returning wrong cache-control headers for 304 responses --- src/SimplePie.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/SimplePie.php b/src/SimplePie.php index 225e40278..bc5da0b08 100644 --- a/src/SimplePie.php +++ b/src/SimplePie.php @@ -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