From 76f5bee76d4640169d9a506b5b107f787797879c Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Thu, 27 Nov 2025 23:31:03 +0100 Subject: Better merging of custom HTTP headers (#8251) Alternative to https://github.com/FreshRSS/FreshRSS/pull/8246 See https://github.com/FreshRSS/FreshRSS/issues/8189#issuecomment-3569434305 --- app/Models/SimplePieCustom.php | 2 +- app/Models/SimplePieFetch.php | 59 ++++++++++++++++++++++++++++++++++++++++ app/Models/SimplePieResponse.php | 34 ----------------------- 3 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 app/Models/SimplePieFetch.php delete mode 100644 app/Models/SimplePieResponse.php (limited to 'app/Models') diff --git a/app/Models/SimplePieCustom.php b/app/Models/SimplePieCustom.php index 372ce6d3d..c3593e4a2 100644 --- a/app/Models/SimplePieCustom.php +++ b/app/Models/SimplePieCustom.php @@ -11,7 +11,7 @@ final class FreshRSS_SimplePieCustom extends \SimplePie\SimplePie public function __construct(array $attributes = [], array $curl_options = []) { parent::__construct(); $limits = FreshRSS_Context::systemConf()->limits; - $this->get_registry()->register(\SimplePie\File::class, FreshRSS_SimplePieResponse::class); + $this->get_registry()->register(\SimplePie\File::class, FreshRSS_SimplePieFetch::class); $this->set_useragent(FRESHRSS_USERAGENT); $this->set_cache_name_function('sha1'); $this->set_cache_location(CACHE_PATH); diff --git a/app/Models/SimplePieFetch.php b/app/Models/SimplePieFetch.php new file mode 100644 index 000000000..3d5c98b46 --- /dev/null +++ b/app/Models/SimplePieFetch.php @@ -0,0 +1,59 @@ += 2) { + $existingKeys[strtolower(trim($parts[0]))] = true; + } + } + foreach ($headers as $key => $value) { + if (isset($existingKeys[strtolower($key)])) { + unset($headers[$key]); + } + } + } + + parent::__construct($url, $timeout, $redirects, $headers, $useragent, $force_fsockopen, $curl_options); + } + + #[\Override] + protected function on_http_response($response, array $curl_options = []): void { + if (FreshRSS_Context::systemConf()->simplepie_syslog_enabled) { + syslog(LOG_INFO, 'FreshRSS SimplePie GET ' . $this->get_status_code() . ' ' . \SimplePie\Misc::url_remove_credentials($this->get_final_requested_uri())); + } + + if (in_array($this->get_status_code(), [429, 503], true)) { + $parser = new \SimplePie\HTTP\Parser(is_string($response) ? $response : ''); + if ($parser->parse()) { + $headers = $parser->headers; + } else { + $headers = []; + } + + $proxy = is_string($curl_options[CURLOPT_PROXY] ?? null) ? $curl_options[CURLOPT_PROXY] : ''; + $retryAfter = FreshRSS_http_Util::setRetryAfter($this->get_final_requested_uri(), $proxy, $headers['retry-after'] ?? ''); + if ($retryAfter > 0) { + $domain = parse_url($this->get_final_requested_uri(), PHP_URL_HOST); + if (is_string($domain) && $domain !== '') { + if (is_int($port = parse_url($this->get_final_requested_uri(), PHP_URL_PORT))) { + $domain .= ':' . $port; + } + $errorMessage = 'Will retry after ' . date('c', $retryAfter) . ' for domain `' . $domain . '`'; + Minz_Log::notice($errorMessage); + } + } + } + } +} diff --git a/app/Models/SimplePieResponse.php b/app/Models/SimplePieResponse.php deleted file mode 100644 index 42625ccf3..000000000 --- a/app/Models/SimplePieResponse.php +++ /dev/null @@ -1,34 +0,0 @@ -simplepie_syslog_enabled) { - syslog(LOG_INFO, 'FreshRSS SimplePie GET ' . $this->get_status_code() . ' ' . \SimplePie\Misc::url_remove_credentials($this->get_final_requested_uri())); - } - - if (in_array($this->get_status_code(), [429, 503], true)) { - $parser = new \SimplePie\HTTP\Parser(is_string($response) ? $response : ''); - if ($parser->parse()) { - $headers = $parser->headers; - } else { - $headers = []; - } - - $proxy = is_string($curl_options[CURLOPT_PROXY] ?? null) ? $curl_options[CURLOPT_PROXY] : ''; - $retryAfter = FreshRSS_http_Util::setRetryAfter($this->get_final_requested_uri(), $proxy, $headers['retry-after'] ?? ''); - if ($retryAfter > 0) { - $domain = parse_url($this->get_final_requested_uri(), PHP_URL_HOST); - if (is_string($domain) && $domain !== '') { - if (is_int($port = parse_url($this->get_final_requested_uri(), PHP_URL_PORT))) { - $domain .= ':' . $port; - } - $errorMessage = 'Will retry after ' . date('c', $retryAfter) . ' for domain `' . $domain . '`'; - Minz_Log::notice($errorMessage); - } - } - } - } -} -- cgit v1.2.3