From afcc38786e85cfe99efacfddb3a222989bd09f5e Mon Sep 17 00:00:00 2001 From: Dag Date: Sun, 31 Jul 2022 04:21:56 +0200 Subject: [PATCH] fix: use default headers in getContents() (#2927) --- .../prepare_release/fetch_contributors.php | 6 ++--- lib/contents.php | 26 +++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/contrib/prepare_release/fetch_contributors.php b/contrib/prepare_release/fetch_contributors.php index 724d9fd1..479b95c3 100644 --- a/contrib/prepare_release/fetch_contributors.php +++ b/contrib/prepare_release/fetch_contributors.php @@ -12,9 +12,9 @@ $next = true; while ($next) { /* Collect all contributors */ $headers = [ - 'Accept: application/json', - 'Content-Type: application/json', - 'User-Agent: RSS-Bridge' + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'User-Agent' => 'RSS-Bridge', ]; $result = _http_request($url, ['headers' => $headers]); diff --git a/lib/contents.php b/lib/contents.php index 60282212..94e45b95 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -75,8 +75,26 @@ function getContents( $cache->purgeCache(86400); // 24 hours (forced) $cache->setKey([$url]); + // Snagged from https://github.com/lwthiker/curl-impersonate/blob/main/firefox/curl_ff102 + $defaultHttpHeaders = [ + 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', + 'Accept-Language' => 'en-US,en;q=0.5', + 'Upgrade-Insecure-Requests' => '1', + 'Sec-Fetch-Dest' => 'document', + 'Sec-Fetch-Mode' => 'navigate', + 'Sec-Fetch-Site' => 'none', + 'Sec-Fetch-User' => '?1', + 'TE' => 'Trailers', + ]; + $httpHeadersNormalized = []; + foreach ($httpHeaders as $httpHeader) { + $parts = explode(':', $httpHeader); + $headerName = trim($parts[0]); + $headerValue = trim(implode(':', array_slice($parts, 1))); + $httpHeadersNormalized[$headerName] = $headerValue; + } $config = [ - 'headers' => $httpHeaders, + 'headers' => array_merge($defaultHttpHeaders, $httpHeadersNormalized), 'curl_options' => $curlOptions, ]; if (Configuration::getConfig('proxy', 'url') && !defined('NOPROXY')) { @@ -154,7 +172,11 @@ function _http_request(string $url, array $config = []): array curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); curl_setopt($ch, CURLOPT_HEADER, false); - curl_setopt($ch, CURLOPT_HTTPHEADER, $config['headers']); + $httpHeaders = []; + foreach ($config['headers'] as $name => $value) { + $httpHeaders[] = sprintf('%s: %s', $name, $value); + } + curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders); curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']); curl_setopt($ch, CURLOPT_TIMEOUT, $config['timeout']); curl_setopt($ch, CURLOPT_ENCODING, '');