SimplePieFetch.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. final class FreshRSS_SimplePieFetch extends \SimplePie\File
  4. {
  5. public function __construct(string $url, int $timeout = 10, int $redirects = 5,
  6. ?array $headers = null, ?string $useragent = null, bool $force_fsockopen = false, array $curl_options = []) {
  7. // Remove case-insensitively default HTTP headers passed by $headers if they are overridden by $curl_options[CURLOPT_HTTPHEADER]
  8. if (is_array($curl_options[CURLOPT_HTTPHEADER] ?? null) && is_array($headers)) {
  9. $existingKeys = [];
  10. foreach ($curl_options[CURLOPT_HTTPHEADER] as $headerLine) {
  11. if (!is_string($headerLine)) {
  12. continue;
  13. }
  14. $parts = explode(':', $headerLine, 2);
  15. if (count($parts) >= 2) {
  16. $existingKeys[strtolower(trim($parts[0]))] = true;
  17. }
  18. }
  19. foreach ($headers as $key => $value) {
  20. if (isset($existingKeys[strtolower($key)])) {
  21. unset($headers[$key]);
  22. }
  23. }
  24. }
  25. parent::__construct($url, $timeout, $redirects, $headers, $useragent, $force_fsockopen, $curl_options);
  26. }
  27. #[\Override]
  28. protected function on_http_response($response, array $curl_options = []): void {
  29. if (FreshRSS_Context::systemConf()->simplepie_syslog_enabled) {
  30. syslog(LOG_INFO, 'FreshRSS SimplePie GET ' . $this->get_status_code() . ' ' . \SimplePie\Misc::url_remove_credentials($this->get_final_requested_uri()));
  31. }
  32. if (in_array($this->get_status_code(), [429, 503], true)) {
  33. $parser = new \SimplePie\HTTP\Parser(is_string($response) ? $response : '');
  34. if ($parser->parse()) {
  35. $headers = $parser->headers;
  36. } else {
  37. $headers = [];
  38. }
  39. $proxy = is_string($curl_options[CURLOPT_PROXY] ?? null) ? $curl_options[CURLOPT_PROXY] : '';
  40. $retryAfter = FreshRSS_http_Util::setRetryAfter($this->get_final_requested_uri(), $proxy, $headers['retry-after'] ?? '');
  41. if ($retryAfter > 0) {
  42. $domain = parse_url($this->get_final_requested_uri(), PHP_URL_HOST);
  43. if (is_string($domain) && $domain !== '') {
  44. if (is_int($port = parse_url($this->get_final_requested_uri(), PHP_URL_PORT))) {
  45. $domain .= ':' . $port;
  46. }
  47. $errorMessage = 'Will retry after ' . date('c', $retryAfter) . ' for domain `' . $domain . '`';
  48. Minz_Log::notice($errorMessage);
  49. }
  50. }
  51. }
  52. }
  53. }