SimplePieFetch.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. $redirects = $curl_options[CURLOPT_MAXREDIRS] ?? null;
  26. if (!is_int($redirects)) {
  27. $redirects = 4;
  28. } elseif ($redirects < 0) {
  29. $redirects = -1; // infinite redirects
  30. }
  31. if (isset($curl_options[CURLOPT_FOLLOWLOCATION])) {
  32. if ($curl_options[CURLOPT_FOLLOWLOCATION] == true) {
  33. unset($curl_options[CURLOPT_FOLLOWLOCATION]); // Favour the custom SimplePie redirects for security
  34. } else {
  35. $curl_options[CURLOPT_FOLLOWLOCATION] = false;
  36. unset($curl_options[CURLOPT_MAXREDIRS]);
  37. $redirects = 0;
  38. }
  39. }
  40. parent::__construct($url, $timeout, $redirects, $headers, $useragent, $force_fsockopen, $curl_options);
  41. }
  42. #[\Override]
  43. protected function get_curl_resolve_info(string $url): array|null|false {
  44. return FreshRSS_http_Util::getCurlResolveInfo($url);
  45. }
  46. #[\Override]
  47. protected function on_http_response($response, array $curl_options = []): void {
  48. if (FreshRSS_Context::systemConf()->simplepie_syslog_enabled) {
  49. syslog(LOG_INFO, 'FreshRSS SimplePie GET ' . $this->get_status_code() . ' ' . \SimplePie\Misc::url_remove_credentials($this->get_final_requested_uri()));
  50. }
  51. if (in_array($this->get_status_code(), [429, 503], true)) {
  52. $parser = new \SimplePie\HTTP\Parser(is_string($response) ? $response : '');
  53. if ($parser->parse()) {
  54. $headers = $parser->headers;
  55. } else {
  56. $headers = [];
  57. }
  58. $proxy = is_string($curl_options[CURLOPT_PROXY] ?? null) ? $curl_options[CURLOPT_PROXY] : '';
  59. $retryAfter = FreshRSS_http_Util::setRetryAfter($this->get_final_requested_uri(), $proxy, $headers['retry-after'] ?? '');
  60. if ($retryAfter > 0) {
  61. $domain = parse_url($this->get_final_requested_uri(), PHP_URL_HOST);
  62. if (is_string($domain) && $domain !== '') {
  63. if (is_int($port = parse_url($this->get_final_requested_uri(), PHP_URL_PORT))) {
  64. $domain .= ':' . $port;
  65. }
  66. $errorMessage = 'Will retry after ' . date('c', $retryAfter) . ' for domain `' . $domain . '`';
  67. Minz_Log::notice($errorMessage);
  68. }
  69. }
  70. }
  71. }
  72. }