From 1fae0123ec0f20d085b87b468a8a0bee32754ecf Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 29 Mar 2022 06:24:20 +0000 Subject: [PATCH 1/6] Some more "accept" parameters are added --- mod/ostatus_subscribe.php | 4 +++- mod/redir.php | 4 +++- src/Content/OEmbed.php | 3 ++- src/Content/Text/BBCode.php | 15 +++++++----- src/Core/Search.php | 5 ++-- src/Core/Storage/Type/ExternalResource.php | 5 +++- src/Model/GServer.php | 2 +- src/Model/Photo.php | 8 +++++-- src/Model/Post/Media.php | 5 ++-- src/Model/Profile.php | 4 +++- src/Model/User.php | 5 +++- src/Module/Admin/Summary.php | 4 +++- src/Module/Debug/Feed.php | 3 ++- src/Module/Magic.php | 1 + src/Module/Photo.php | 5 +++- src/Module/Proxy.php | 5 +++- src/Network/HTTPClient/Client/HttpClient.php | 2 ++ src/Network/Probe.php | 25 ++++++++++---------- src/Protocol/Diaspora.php | 3 ++- src/Protocol/OStatus.php | 15 ++++++------ src/Protocol/Salmon.php | 3 ++- src/Security/ExAuth.php | 4 +++- src/Util/Images.php | 3 ++- src/Util/ParseUrl.php | 12 ++++++---- src/Worker/CheckVersion.php | 3 ++- src/Worker/Directory.php | 3 ++- src/Worker/NodeInfo.php | 3 ++- src/Worker/OnePoll.php | 2 +- src/Worker/PullDirectory.php | 3 ++- src/Worker/SearchDirectory.php | 3 ++- src/Worker/UpdateServerDirectory.php | 5 ++-- src/Worker/UpdateServerPeers.php | 4 +++- 32 files changed, 111 insertions(+), 60 deletions(-) diff --git a/mod/ostatus_subscribe.php b/mod/ostatus_subscribe.php index f4d5077c0..c1a9df7a5 100644 --- a/mod/ostatus_subscribe.php +++ b/mod/ostatus_subscribe.php @@ -24,6 +24,8 @@ use Friendica\Core\Protocol; use Friendica\DI; use Friendica\Model\APContact; use Friendica\Model\Contact; +use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Protocol\ActivityPub; function ostatus_subscribe_content(App $a) @@ -57,7 +59,7 @@ function ostatus_subscribe_content(App $a) $api = $contact['baseurl'] . '/api/'; // Fetching friends - $curlResult = DI::httpClient()->get($api . 'statuses/friends.json?screen_name=' . $contact['nick']); + $curlResult = DI::httpClient()->get($api . 'statuses/friends.json?screen_name=' . $contact['nick'], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact'); diff --git a/mod/redir.php b/mod/redir.php index fe0da9e66..5a707d229 100644 --- a/mod/redir.php +++ b/mod/redir.php @@ -27,6 +27,8 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Profile; +use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Strings; function redir_init(App $a) { @@ -142,7 +144,7 @@ function redir_magic($a, $cid, $url) } // Test for magic auth on the target system - $serverret = DI::httpClient()->get($basepath . '/magic'); + $serverret = DI::httpClient()->get($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); if ($serverret->isSuccess()) { $separator = strpos($target_url, '?') ? '&' : '?'; $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url); diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php index 4e1ae2946..0be220c51 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -32,6 +32,7 @@ use Friendica\Core\Renderer; use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Util\DateTimeFormat; use Friendica\Util\Network; use Friendica\Util\ParseUrl; @@ -97,7 +98,7 @@ class OEmbed if (!in_array($ext, $noexts)) { // try oembed autodiscovery - $html_text = DI::httpClient()->fetch($embedurl, 15, 'text/*'); + $html_text = DI::httpClient()->fetch($embedurl, 15, HttpClient::ACCEPT_HTML); if (!empty($html_text)) { $dom = new DOMDocument(); if (@$dom->loadHTML($html_text)) { diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 2c17663c1..f30b21351 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -39,6 +39,7 @@ use Friendica\Model\Event; use Friendica\Model\Photo; use Friendica\Model\Post; use Friendica\Model\Tag; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Object\Image; use Friendica\Protocol\Activity; @@ -501,11 +502,13 @@ class BBCode continue; } - $curlResult = DI::httpClient()->get($mtch[1]); + $curlResult = DI::httpClient()->get($mtch[1], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_IMAGE]); if (!$curlResult->isSuccess()) { continue; } + Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $mtch[1]]); + $i = $curlResult->getBody(); $type = $curlResult->getContentType(); $type = Images::getMimeTypeByData($i, $mtch[1], $type); @@ -1096,7 +1099,7 @@ class BBCode $attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8'); } - $img_str = ' $value) { if (!empty($value)) { @@ -1214,7 +1217,7 @@ class BBCode $text = "[url=" . $match[2] . ']' . $match[2] . "[/url]"; // if its not a picture then look if its a page that contains a picture link - $body = DI::httpClient()->fetch($match[1]); + $body = DI::httpClient()->fetch($match[1], 0, HttpClient::ACCEPT_HTML); if (empty($body)) { DI::cache()->set($cache_key, $text); return $text; @@ -1290,7 +1293,7 @@ class BBCode } // if its not a picture then look if its a page that contains a picture link - $body = DI::httpClient()->fetch($match[1]); + $body = DI::httpClient()->fetch($match[1], 0, HttpClient::ACCEPT_HTML); if (empty($body)) { DI::cache()->set($cache_key, $text); return $text; @@ -1780,7 +1783,7 @@ class BBCode $text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '' . DI::l10n()->t('Image/photo') . '', $text); $text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '' . DI::l10n()->t('Image/photo') . '', $text); - + $text = self::convertImages($text, $simple_html, $uriid); $text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", '
' . DI::l10n()->t('Encrypted content') . '
', $text); @@ -1894,7 +1897,7 @@ class BBCode } else { $text = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $text); } - + if (!$for_plaintext) { if (in_array($simple_html, [self::OSTATUS, self::API, self::ACTIVITYPUB])) { $text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text); diff --git a/src/Core/Search.php b/src/Core/Search.php index 6c741821e..3e768b6c7 100644 --- a/src/Core/Search.php +++ b/src/Core/Search.php @@ -23,6 +23,7 @@ namespace Friendica\Core; use Friendica\DI; use Friendica\Model\Contact; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPException; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Object\Search\ContactResult; @@ -122,7 +123,7 @@ class Search $searchUrl .= '&page=' . $page; } - $resultJson = DI::httpClient()->fetch($searchUrl, 0, 'application/json'); + $resultJson = DI::httpClient()->fetch($searchUrl, 0, HttpClient::ACCEPT_JSON); $results = json_decode($resultJson, true); @@ -228,7 +229,7 @@ class Search $return = Contact::searchByName($search, $mode); } else { $p = $page > 1 ? 'p=' . $page : ''; - $curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), [HttpClientOptions::ACCEPT_CONTENT => ['application/json']]); + $curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if ($curlResult->isSuccess()) { $searchResult = json_decode($curlResult->getBody(), true); if (!empty($searchResult['profiles'])) { diff --git a/src/Core/Storage/Type/ExternalResource.php b/src/Core/Storage/Type/ExternalResource.php index 767ec7957..23239965d 100644 --- a/src/Core/Storage/Type/ExternalResource.php +++ b/src/Core/Storage/Type/ExternalResource.php @@ -22,8 +22,10 @@ namespace Friendica\Core\Storage\Type; use Exception; +use Friendica\Core\Logger; use Friendica\Core\Storage\Exception\ReferenceStorageException; use Friendica\Core\Storage\Capability\ICanReadFromStorage; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Util\HTTPSignature; /** @@ -52,11 +54,12 @@ class ExternalResource implements ICanReadFromStorage } try { - $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, ['accept_content' => []]); + $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, ['accept_content' => [HttpClient::ACCEPT_IMAGE]]); } catch (Exception $exception) { throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception); } if ($fetchResult->isSuccess()) { + Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => $data->uid, 'url' => $data->url]); return $fetchResult->getBody(); } else { throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $fetchResult->getReturnCode(), new Exception($fetchResult->getBody())); diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 3cb4dc5f1..731175ea5 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -1946,7 +1946,7 @@ class GServer $protocols = ['activitypub', 'diaspora', 'dfrn', 'ostatus']; foreach ($protocols as $protocol) { $query = '{nodes(protocol:"' . $protocol . '"){host}}'; - $curlResult = DI::httpClient()->fetch('https://the-federation.info/graphql?query=' . urlencode($query)); + $curlResult = DI::httpClient()->fetch('https://the-federation.info/graphql?query=' . urlencode($query), 0, HttpClient::ACCEPT_JSON); if (!empty($curlResult)) { $data = json_decode($curlResult, true); if (!empty($data['data']['nodes'])) { diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 8ba01e336..74171bd7b 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -32,6 +32,8 @@ use Friendica\Core\Storage\Exception\InvalidClassStorageException; use Friendica\Core\Storage\Exception\ReferenceStorageException; use Friendica\Core\Storage\Exception\StorageException; use Friendica\Core\Storage\Type\SystemResource; +use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Object\Image; use Friendica\Util\DateTimeFormat; use Friendica\Util\Images; @@ -497,7 +499,8 @@ class Photo $filename = basename($image_url); if (!empty($image_url)) { - $ret = DI::httpClient()->get($image_url); + $ret = DI::httpClient()->get($image_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_IMAGE]); + Logger::debug('Got picture', ['Content-Type' => $ret->getHeader('Content-Type'), 'url' => $image_url]); $img_str = $ret->getBody(); $type = $ret->getContentType(); } else { @@ -912,7 +915,8 @@ class Photo { $filename = basename($image_url); if (!empty($image_url)) { - $ret = DI::httpClient()->get($image_url); + $ret = DI::httpClient()->get($image_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_IMAGE]); + Logger::debug('Got picture', ['Content-Type' => $ret->getHeader('Content-Type'), 'url' => $image_url]); $img_str = $ret->getBody(); $type = $ret->getContentType(); } else { diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index 254b88115..660d5f542 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -30,6 +30,7 @@ use Friendica\DI; use Friendica\Model\Item; use Friendica\Model\Photo; use Friendica\Model\Post; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Images; use Friendica\Util\Network; @@ -168,11 +169,11 @@ class Media // Fetch the mimetype or size if missing. if (empty($media['mimetype']) || empty($media['size'])) { $timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout]); + $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); // Workaround for systems that can't handle a HEAD request if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) { - $curlResult = DI::httpClient()->get($media['url'], [HttpClientOptions::TIMEOUT => $timeout]); + $curlResult = DI::httpClient()->get($media['url'], [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); } if ($curlResult->isSuccess()) { diff --git a/src/Model/Profile.php b/src/Model/Profile.php index d77935520..3a453cc87 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -35,6 +35,8 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; @@ -749,7 +751,7 @@ class Profile $magic_path = $basepath . '/magic' . '?owa=1&dest=' . $dest . '&' . $addr_request; // We have to check if the remote server does understand /magic without invoking something - $serverret = DI::httpClient()->get($basepath . '/magic'); + $serverret = DI::httpClient()->get($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); if ($serverret->isSuccess()) { Logger::info('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path); System::externalRedirect($magic_path); diff --git a/src/Model/User.php b/src/Model/User.php index 93538bce6..497787d1c 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -34,6 +34,8 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Security\TwoFactor\Model\AppSpecificPassword; use Friendica\Network\HTTPException; use Friendica\Object\Image; @@ -1150,8 +1152,9 @@ class User $photo_failure = false; $filename = basename($photo); - $curlResult = DI::httpClient()->get($photo); + $curlResult = DI::httpClient()->get($photo, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_IMAGE]); if ($curlResult->isSuccess()) { + Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $photo]); $img_str = $curlResult->getBody(); $type = $curlResult->getContentType(); } else { diff --git a/src/Module/Admin/Summary.php b/src/Module/Admin/Summary.php index 500732a9f..4c6eebf33 100644 --- a/src/Module/Admin/Summary.php +++ b/src/Module/Admin/Summary.php @@ -32,6 +32,8 @@ use Friendica\DI; use Friendica\Core\Config\Factory\Config; use Friendica\Model\Register; use Friendica\Module\BaseAdmin; +use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException\ServiceUnavailableException; use Friendica\Util\DateTimeFormat; @@ -248,7 +250,7 @@ class Summary extends BaseAdmin private static function checkSelfHostMeta() { // Fetch the host-meta to check if this really is a vital server - return DI::httpClient()->get(DI::baseUrl()->get() . '/.well-known/host-meta')->isSuccess(); + return DI::httpClient()->get(DI::baseUrl()->get() . '/.well-known/host-meta', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_XRD_XML])->isSuccess(); } } diff --git a/src/Module/Debug/Feed.php b/src/Module/Debug/Feed.php index 470b61df2..cd80453dd 100644 --- a/src/Module/Debug/Feed.php +++ b/src/Module/Debug/Feed.php @@ -28,6 +28,7 @@ use Friendica\Core\Renderer; use Friendica\Model; use Friendica\Module\Response; use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Protocol; use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; @@ -60,7 +61,7 @@ class Feed extends BaseModule $contact = Model\Contact::getByURLForUser($url, local_user(), null); - $xml = $this->httpClient->fetch($contact['poll']); + $xml = $this->httpClient->fetch($contact['poll'], 0, HttpClient::ACCEPT_FEED_XML); $import_result = Protocol\Feed::import($xml); diff --git a/src/Module/Magic.php b/src/Module/Magic.php index f6f3bbc59..16b349237 100644 --- a/src/Module/Magic.php +++ b/src/Module/Magic.php @@ -29,6 +29,7 @@ use Friendica\Database\Database; use Friendica\Model\Contact; use Friendica\Model\User; use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\HTTPSignature; use Friendica\Util\Profiler; diff --git a/src/Module/Photo.php b/src/Module/Photo.php index 3d3110fd8..0af2d2eb0 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -32,6 +32,7 @@ use Friendica\Model\Profile; use Friendica\Core\Storage\Type\ExternalResource; use Friendica\Core\Storage\Type\SystemResource; use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPException; use Friendica\Network\HTTPException\NotModifiedException; use Friendica\Object\Image; @@ -308,7 +309,7 @@ class Photo extends BaseModule } $mimetext = ''; if (!empty($url)) { - $mime = ParseUrl::getContentType($url); + $mime = ParseUrl::getContentType($url, HttpClient::ACCEPT_IMAGE); if (!empty($mime)) { $mimetext = $mime[0] . '/' . $mime[1]; } else { @@ -317,6 +318,8 @@ class Photo extends BaseModule if (!empty($mimetext) && ($mime[0] != 'image') && ($mimetext != 'application/octet-stream')) { Logger::info('Unexpected Content-Type', ['mime' => $mimetext, 'url' => $url]); $mimetext = ''; + } if (!empty($mimetext)) { + Logger::debug('Expected Content-Type', ['mime' => $mimetext, 'url' => $url]); } } if (empty($mimetext)) { diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index 419289c39..dfc8113a4 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -25,6 +25,7 @@ use Friendica\BaseModule; use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPException\NotModifiedException; use Friendica\Object\Image; use Friendica\Util\HTTPSignature; @@ -81,7 +82,7 @@ class Proxy extends BaseModule $request['url'] = str_replace(' ', '+', $request['url']); // Fetch the content with the local user - $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), ['accept_content' => [], 'timeout' => 10]); + $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), ['accept_content' => [HttpClient::ACCEPT_IMAGE], 'timeout' => 10]); $img_str = $fetchResult->getBody(); if (!$fetchResult->isSuccess() || empty($img_str)) { @@ -90,6 +91,8 @@ class Proxy extends BaseModule // stop. } + Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => local_user(), 'image' => $request['url']]); + $mime = Images::getMimeTypeByData($img_str); $image = new Image($img_str, $mime); diff --git a/src/Network/HTTPClient/Client/HttpClient.php b/src/Network/HTTPClient/Client/HttpClient.php index 1f6839762..792cc7965 100644 --- a/src/Network/HTTPClient/Client/HttpClient.php +++ b/src/Network/HTTPClient/Client/HttpClient.php @@ -52,7 +52,9 @@ class HttpClient implements ICanSendHttpRequests const ACCEPT_JRD_JSON = 'application/jrd+json,application/json;q=0.9'; const ACCEPT_JSON = 'application/json,*/*;q=0.9'; const ACCEPT_JSON_AS = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"'; + const ACCEPT_MAGIC = 'application/magic-envelope+xml'; const ACCEPT_RSS_XML = 'application/rss+xml,text/xml;q=0.9,*/*;q=0.8'; + const ACCEPT_TEXT = 'text/plain,text/*;q=0.9,*/*;q=0.8'; const ACCEPT_VIDEO = 'video/mp4,video/*;q=0.9,*/*;q=0.8'; const ACCEPT_XRD_XML = 'application/xrd+xml,text/xml;q=0.9,*/*;q=0.8'; diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 810885323..ceb684b70 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -170,7 +170,7 @@ class Probe Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]); $xrd = null; - $curlResult = DI::httpClient()->get($ssl_url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]); + $curlResult = DI::httpClient()->get($ssl_url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_XRD_XML]); $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); if ($curlResult->isSuccess()) { $xml = $curlResult->getBody(); @@ -187,7 +187,7 @@ class Probe } if (!is_object($xrd) && !empty($url)) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_XRD_XML]); $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); if ($curlResult->isTimeout()) { Logger::info('Probing timeout', ['url' => $url]); @@ -429,7 +429,7 @@ class Probe */ private static function getHideStatus($url) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if (!$curlResult->isSuccess()) { return false; } @@ -834,7 +834,8 @@ class Probe public static function pollZot($url, $data) { - $curlResult = DI::httpClient()->get($url); + // @todo Check if this endpoint might be queried via a POST + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); if ($curlResult->isTimeout()) { return $data; } @@ -1000,7 +1001,7 @@ class Probe */ private static function pollNoscrape($noscrape_url, $data) { - $curlResult = DI::httpClient()->get($noscrape_url); + $curlResult = DI::httpClient()->get($noscrape_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if ($curlResult->isTimeout()) { self::$istimeout = true; return $data; @@ -1266,7 +1267,7 @@ class Probe */ private static function pollHcard($hcard_url, $data, $dfrn = false) { - $curlResult = DI::httpClient()->get($hcard_url); + $curlResult = DI::httpClient()->get($hcard_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -1527,7 +1528,7 @@ class Probe $pubkey = substr($pubkey, 5); } } elseif (Strings::normaliseLink($pubkey) == 'http://') { - $curlResult = DI::httpClient()->get($pubkey); + $curlResult = DI::httpClient()->get($pubkey, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); if ($curlResult->isTimeout()) { self::$istimeout = true; return $short ? false : []; @@ -1561,7 +1562,7 @@ class Probe } // Fetch all additional data from the feed - $curlResult = DI::httpClient()->get($data["poll"]); + $curlResult = DI::httpClient()->get($data["poll"], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_FEED_XML]); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -1613,7 +1614,7 @@ class Probe */ private static function pumpioProfileData($profile_link) { - $curlResult = DI::httpClient()->get($profile_link); + $curlResult = DI::httpClient()->get($profile_link, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return []; } @@ -1826,7 +1827,7 @@ class Probe */ private static function feed($url, $probe = true) { - $curlResult = DI::httpClient()->get($url); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_FEED_XML]); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -2048,7 +2049,7 @@ class Probe return ''; } - $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick']); + $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if ($curlResult->isSuccess() && !empty($curlResult->getBody())) { $noscrape = json_decode($curlResult->getBody(), true); @@ -2123,7 +2124,7 @@ class Probe private static function updateFromFeed(array $data) { // Search for the newest entry in the feed - $curlResult = DI::httpClient()->get($data['poll']); + $curlResult = DI::httpClient()->get($data['poll'], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); if (!$curlResult->isSuccess() || !$curlResult->getBody()) { return ''; } diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 9f7781d13..154d65f6f 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -41,6 +41,7 @@ use Friendica\Model\Mail; use Friendica\Model\Post; use Friendica\Model\Tag; use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\Probe; use Friendica\Util\Crypto; use Friendica\Util\DateTimeFormat; @@ -1059,7 +1060,7 @@ class Diaspora Logger::info("Fetch post from ".$source_url); - $envelope = DI::httpClient()->fetch($source_url); + $envelope = DI::httpClient()->fetch($source_url, 0, HttpClient::ACCEPT_MAGIC); if ($envelope) { Logger::info("Envelope was fetched."); $x = self::verifyMagicEnvelope($envelope); diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index a2c9a8447..076602f9a 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -38,6 +38,7 @@ use Friendica\Model\ItemURI; use Friendica\Model\Post; use Friendica\Model\Tag; use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\Probe; use Friendica\Util\DateTimeFormat; @@ -731,7 +732,7 @@ class OStatus self::$conv_list[$conversation] = true; - $curlResult = DI::httpClient()->get($conversation, [HttpClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]); + $curlResult = DI::httpClient()->get($conversation, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return; @@ -761,7 +762,7 @@ class OStatus } } if ($file != '') { - $conversation_atom = DI::httpClient()->get($attribute['href']); + $conversation_atom = DI::httpClient()->get($attribute['href'], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); if ($conversation_atom->isSuccess()) { $xml = $conversation_atom->getBody(); @@ -875,7 +876,7 @@ class OStatus return; } - $curlResult = DI::httpClient()->get($self); + $curlResult = DI::httpClient()->get($self, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); if (!$curlResult->isSuccess()) { return; @@ -925,7 +926,7 @@ class OStatus } $stored = false; - $curlResult = DI::httpClient()->get($related, [HttpClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]); + $curlResult = DI::httpClient()->get($related, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return; @@ -957,7 +958,7 @@ class OStatus } } if ($atom_file != '') { - $curlResult = DI::httpClient()->get($atom_file); + $curlResult = DI::httpClient()->get($atom_file, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); if ($curlResult->isSuccess()) { Logger::info('Fetched XML for URI ' . $related_uri); @@ -969,7 +970,7 @@ class OStatus // Workaround for older GNU Social servers if (($xml == '') && strstr($related, '/notice/')) { - $curlResult = DI::httpClient()->get(str_replace('/notice/', '/api/statuses/show/', $related) . '.atom'); + $curlResult = DI::httpClient()->get(str_replace('/notice/', '/api/statuses/show/', $related) . '.atom', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); if ($curlResult->isSuccess()) { Logger::info('GNU Social workaround to fetch XML for URI ' . $related_uri); @@ -980,7 +981,7 @@ class OStatus // Even more worse workaround for GNU Social ;-) if ($xml == '') { $related_guess = self::convertHref($related_uri); - $curlResult = DI::httpClient()->get(str_replace('/notice/', '/api/statuses/show/', $related_guess) . '.atom'); + $curlResult = DI::httpClient()->get(str_replace('/notice/', '/api/statuses/show/', $related_guess) . '.atom', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); if ($curlResult->isSuccess()) { Logger::info('GNU Social workaround 2 to fetch XML for URI ' . $related_uri); diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php index aecc35e9f..12d05f9b3 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -23,6 +23,7 @@ namespace Friendica\Protocol; use Friendica\Core\Logger; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\Probe; use Friendica\Util\Crypto; use Friendica\Util\Strings; @@ -72,7 +73,7 @@ class Salmon $ret[$x] = substr($ret[$x], 5); } } elseif (Strings::normaliseLink($ret[$x]) == 'http://') { - $ret[$x] = DI::httpClient()->fetch($ret[$x]); + $ret[$x] = DI::httpClient()->fetch($ret[$x], 0, HttpClient::ACCEPT_DEFAULT); } } } diff --git a/src/Security/ExAuth.php b/src/Security/ExAuth.php index a402218f5..f324b0a96 100644 --- a/src/Security/ExAuth.php +++ b/src/Security/ExAuth.php @@ -57,6 +57,8 @@ use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Database\Database; use Friendica\DI; use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException; use Friendica\Util\PidFile; @@ -239,7 +241,7 @@ class ExAuth $url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user; - $curlResult = DI::httpClient()->get($url); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return false; diff --git a/src/Util/Images.php b/src/Util/Images.php index d7a58b52a..f1e85a37d 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -24,6 +24,7 @@ namespace Friendica\Util; use Friendica\Core\Logger; use Friendica\DI; use Friendica\Model\Photo; +use Friendica\Network\HTTPClient\Client\HttpClient; /** * Image utilities @@ -219,7 +220,7 @@ class Images } if (empty($img_str)) { - $img_str = DI::httpClient()->fetch($url, 4); + $img_str = DI::httpClient()->fetch($url, 4, HttpClient::ACCEPT_IMAGE); } if (!$img_str) { diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 24be55cab..06ef3bdd0 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -29,6 +29,7 @@ use Friendica\Core\Logger; use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPException; use Friendica\Network\HTTPClient\Client\HttpClientOptions; @@ -54,16 +55,17 @@ class ParseUrl /** * Fetch the content type of the given url - * @param string $url URL of the page + * @param string $url URL of the page + * @param string $accept content-type to accept * @return array content type */ - public static function getContentType(string $url) + public static function getContentType(string $url, string $accept = HttpClient::ACCEPT_DEFAULT) { - $curlResult = DI::httpClient()->head($url); + $curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => $accept]); // Workaround for systems that can't handle a HEAD request if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::ACCEPT_CONTENT => $accept]); } if (!$curlResult->isSuccess()) { @@ -220,7 +222,7 @@ class ParseUrl return $siteinfo; } - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { Logger::info('Empty body or error when fetching', ['url' => $url, 'success' => $curlResult->isSuccess(), 'code' => $curlResult->getReturnCode()]); return $siteinfo; diff --git a/src/Worker/CheckVersion.php b/src/Worker/CheckVersion.php index 14ebd8738..d803d9e3a 100644 --- a/src/Worker/CheckVersion.php +++ b/src/Worker/CheckVersion.php @@ -24,6 +24,7 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; /** * Check the git repository VERSION file and save the version to the DB @@ -54,7 +55,7 @@ class CheckVersion Logger::info("Checking VERSION from: ".$checked_url); // fetch the VERSION file - $gitversion = DBA::escape(trim(DI::httpClient()->fetch($checked_url))); + $gitversion = DBA::escape(trim(DI::httpClient()->fetch($checked_url, 0, HttpClient::ACCEPT_TEXT))); Logger::notice("Upstream VERSION is: ".$gitversion); DI::config()->set('system', 'git_friendica_version', $gitversion); diff --git a/src/Worker/Directory.php b/src/Worker/Directory.php index d499f778c..db199a856 100644 --- a/src/Worker/Directory.php +++ b/src/Worker/Directory.php @@ -26,6 +26,7 @@ use Friendica\Core\Logger; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; /** * Sends updated profile data to the directory @@ -53,7 +54,7 @@ class Directory Logger::info('Updating directory: ' . $arr['url']); if (strlen($arr['url'])) { - DI::httpClient()->fetch($dir . '?url=' . bin2hex($arr['url'])); + DI::httpClient()->fetch($dir . '?url=' . bin2hex($arr['url']), 0, HttpClient::ACCEPT_DEFAULT); } return; diff --git a/src/Worker/NodeInfo.php b/src/Worker/NodeInfo.php index 4f18c7060..f55c494eb 100644 --- a/src/Worker/NodeInfo.php +++ b/src/Worker/NodeInfo.php @@ -24,6 +24,7 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\DI; use Friendica\Model\Nodeinfo as ModelNodeInfo; +use Friendica\Network\HTTPClient\Client\HttpClient; class NodeInfo { @@ -34,7 +35,7 @@ class NodeInfo // Now trying to register $url = 'http://the-federation.info/register/' . DI::baseUrl()->getHostname(); Logger::debug('Check registering url', ['url' => $url]); - $ret = DI::httpClient()->fetch($url); + $ret = DI::httpClient()->fetch($url, 0, HttpClient::ACCEPT_HTML); Logger::debug('Check registering answer', ['answer' => $ret]); Logger::info('end'); } diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 40295f0c9..839ab6ce1 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -155,7 +155,7 @@ class OnePoll } $cookiejar = tempnam(System::getTempPath(), 'cookiejar-onepoll-'); - $curlResult = DI::httpClient()->get($contact['poll'], [HttpClientOptions::COOKIEJAR => $cookiejar], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_FEED_XML]); + $curlResult = DI::httpClient()->get($contact['poll'], [HttpClientOptions::COOKIEJAR => $cookiejar, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_FEED_XML]); unlink($cookiejar); if ($curlResult->isTimeout()) { diff --git a/src/Worker/PullDirectory.php b/src/Worker/PullDirectory.php index 04aab5016..0ea03a17f 100644 --- a/src/Worker/PullDirectory.php +++ b/src/Worker/PullDirectory.php @@ -24,6 +24,7 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\DI; use Friendica\Model\Contact; +use Friendica\Network\HTTPClient\Client\HttpClient; class PullDirectory { @@ -47,7 +48,7 @@ class PullDirectory Logger::info('Synchronization started.', ['now' => $now, 'directory' => $directory]); - $result = DI::httpClient()->fetch($directory . '/sync/pull/since/' . $now); + $result = DI::httpClient()->fetch($directory . '/sync/pull/since/' . $now, 0, HttpClient::ACCEPT_JSON); if (empty($result)) { Logger::info('Directory server return empty result.', ['directory' => $directory]); return; diff --git a/src/Worker/SearchDirectory.php b/src/Worker/SearchDirectory.php index 9d6426856..2344bb1bf 100644 --- a/src/Worker/SearchDirectory.php +++ b/src/Worker/SearchDirectory.php @@ -26,6 +26,7 @@ use Friendica\Core\Logger; use Friendica\Core\Search; use Friendica\DI; use Friendica\Model\Contact; +use Friendica\Network\HTTPClient\Client\HttpClient; class SearchDirectory { @@ -46,7 +47,7 @@ class SearchDirectory } } - $x = DI::httpClient()->fetch(Search::getGlobalDirectory() . '/lsearch?p=1&n=500&search=' . urlencode($search)); + $x = DI::httpClient()->fetch(Search::getGlobalDirectory() . '/lsearch?p=1&n=500&search=' . urlencode($search), 0, HttpClient::ACCEPT_JSON); $j = json_decode($x); if (!empty($j->results)) { diff --git a/src/Worker/UpdateServerDirectory.php b/src/Worker/UpdateServerDirectory.php index 10cc66cc1..248ab1121 100644 --- a/src/Worker/UpdateServerDirectory.php +++ b/src/Worker/UpdateServerDirectory.php @@ -26,6 +26,7 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\GServer; +use Friendica\Network\HTTPClient\Client\HttpClient; class UpdateServerDirectory { @@ -45,7 +46,7 @@ class UpdateServerDirectory private static function discoverPoCo(array $gserver) { - $result = DI::httpClient()->fetch($gserver['poco'] . '?fields=urls'); + $result = DI::httpClient()->fetch($gserver['poco'] . '?fields=urls', 0, HttpClient::ACCEPT_JSON); if (empty($result)) { Logger::info('Empty result', ['url' => $gserver['url']]); return; @@ -78,7 +79,7 @@ class UpdateServerDirectory private static function discoverMastodonDirectory(array $gserver) { - $result = DI::httpClient()->fetch($gserver['url'] . '/api/v1/directory?order=new&local=true&limit=200&offset=0'); + $result = DI::httpClient()->fetch($gserver['url'] . '/api/v1/directory?order=new&local=true&limit=200&offset=0', 0, HttpClient::ACCEPT_JSON); if (empty($result)) { Logger::info('Empty result', ['url' => $gserver['url']]); return; diff --git a/src/Worker/UpdateServerPeers.php b/src/Worker/UpdateServerPeers.php index c14dccc3a..d2a127b82 100644 --- a/src/Worker/UpdateServerPeers.php +++ b/src/Worker/UpdateServerPeers.php @@ -25,6 +25,8 @@ use Friendica\Core\Logger; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\GServer; +use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Strings; class UpdateServerPeers @@ -35,7 +37,7 @@ class UpdateServerPeers */ public static function execute(string $url) { - $ret = DI::httpClient()->get($url . '/api/v1/instance/peers'); + $ret = DI::httpClient()->get($url . '/api/v1/instance/peers', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$ret->isSuccess() || empty($ret->getBody())) { Logger::info('Server is not reachable or does not offer the "peers" endpoint', ['url' => $url]); return; From 908d2594ed7cda13f2d46d62c8f23d2b8aa8b817 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 29 Mar 2022 08:57:38 +0000 Subject: [PATCH 2/6] Use "HttpClientOptions" --- src/Core/Storage/Type/ExternalResource.php | 3 ++- src/Module/Proxy.php | 3 ++- src/Network/HTTPClient/Client/HttpClient.php | 6 +++--- src/Network/Probe.php | 2 +- src/Util/HTTPSignature.php | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Core/Storage/Type/ExternalResource.php b/src/Core/Storage/Type/ExternalResource.php index 23239965d..3a1dc129f 100644 --- a/src/Core/Storage/Type/ExternalResource.php +++ b/src/Core/Storage/Type/ExternalResource.php @@ -26,6 +26,7 @@ use Friendica\Core\Logger; use Friendica\Core\Storage\Exception\ReferenceStorageException; use Friendica\Core\Storage\Capability\ICanReadFromStorage; use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\HTTPSignature; /** @@ -54,7 +55,7 @@ class ExternalResource implements ICanReadFromStorage } try { - $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, ['accept_content' => [HttpClient::ACCEPT_IMAGE]]); + $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, [HttpClientOptions::ACCEPT_CONTENT => [HttpClient::ACCEPT_IMAGE]]); } catch (Exception $exception) { throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception); } diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index dfc8113a4..751b4a298 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -26,6 +26,7 @@ use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\DI; use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException\NotModifiedException; use Friendica\Object\Image; use Friendica\Util\HTTPSignature; @@ -82,7 +83,7 @@ class Proxy extends BaseModule $request['url'] = str_replace(' ', '+', $request['url']); // Fetch the content with the local user - $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), ['accept_content' => [HttpClient::ACCEPT_IMAGE], 'timeout' => 10]); + $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClient::ACCEPT_IMAGE], 'timeout' => 10]); $img_str = $fetchResult->getBody(); if (!$fetchResult->isSuccess() || empty($img_str)) { diff --git a/src/Network/HTTPClient/Client/HttpClient.php b/src/Network/HTTPClient/Client/HttpClient.php index 792cc7965..e8e77a6b0 100644 --- a/src/Network/HTTPClient/Client/HttpClient.php +++ b/src/Network/HTTPClient/Client/HttpClient.php @@ -267,9 +267,9 @@ class HttpClient implements ICanSendHttpRequests return $this->get( $url, [ - 'timeout' => $timeout, - 'accept_content' => $accept_content, - 'cookiejar' => $cookiejar + HttpClientOptions::TIMEOUT => $timeout, + HttpClientOptions::ACCEPT_CONTENT => $accept_content, + HttpClientOptions::COOKIEJAR => $cookiejar ] ); } diff --git a/src/Network/Probe.php b/src/Network/Probe.php index ceb684b70..9ffbc3e0f 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -932,7 +932,7 @@ class Probe { $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20); - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => [$type]]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => $type]); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index faad39dde..2a4826719 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -416,7 +416,7 @@ class HTTPSignature * @return \Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses CurlResult * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => [HttpClient::ACCEPT_JSON_AS]]) + public static function fetchRaw($request, $uid = 0, $opts = [HttpClientOptions::ACCEPT_CONTENT => [HttpClient::ACCEPT_JSON_AS]]) { $header = []; From 28e3eee91b7a8882afb5ba2fe371dee614ff20a2 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 29 Mar 2022 13:34:54 +0000 Subject: [PATCH 3/6] Add "Accept" to "head" --- src/Content/Text/BBCode.php | 4 ++-- src/Model/Post/Link.php | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index f30b21351..e80025bc0 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1204,7 +1204,7 @@ class BBCode $text = DI::cache()->get($cache_key); if (is_null($text)) { - $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); + $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout'), HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); if ($curlResult->isSuccess()) { $mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; } else { @@ -1275,7 +1275,7 @@ class BBCode return $text; } - $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); + $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout'), HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); if ($curlResult->isSuccess()) { $mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; } else { diff --git a/src/Model/Post/Link.php b/src/Model/Post/Link.php index 78f57425f..6a88e4e03 100644 --- a/src/Model/Post/Link.php +++ b/src/Model/Post/Link.php @@ -26,6 +26,7 @@ use Friendica\Core\System; use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Proxy; @@ -96,11 +97,11 @@ class Link return $url . $id; } - private static function fetchMimeType(string $url) + private static function fetchMimeType(string $url, string $accept = HttpClient::ACCEPT_DEFAULT) { $timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout]); + $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]); if ($curlResult->isSuccess()) { if (empty($media['mimetype'])) { return $curlResult->getHeader('Content-Type')[0] ?? ''; From 94b7c1fc7728b994d7940cf764652a6d9e7c1939 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 29 Mar 2022 14:02:10 +0000 Subject: [PATCH 4/6] Use a more specific "accept" value --- src/Network/Probe.php | 3 +-- src/Worker/Directory.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 9ffbc3e0f..1a44bcbc0 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -834,8 +834,7 @@ class Probe public static function pollZot($url, $data) { - // @todo Check if this endpoint might be queried via a POST - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if ($curlResult->isTimeout()) { return $data; } diff --git a/src/Worker/Directory.php b/src/Worker/Directory.php index db199a856..d977e522d 100644 --- a/src/Worker/Directory.php +++ b/src/Worker/Directory.php @@ -54,7 +54,7 @@ class Directory Logger::info('Updating directory: ' . $arr['url']); if (strlen($arr['url'])) { - DI::httpClient()->fetch($dir . '?url=' . bin2hex($arr['url']), 0, HttpClient::ACCEPT_DEFAULT); + DI::httpClient()->fetch($dir . '?url=' . bin2hex($arr['url']), 0, HttpClient::ACCEPT_HTML); } return; From 3e21b035b72252ca765cbae20ce8550f349e84ef Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 29 Mar 2022 17:26:31 +0000 Subject: [PATCH 5/6] Added logging --- src/Network/Probe.php | 1 + src/Protocol/Salmon.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 1a44bcbc0..b347f235a 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -1532,6 +1532,7 @@ class Probe self::$istimeout = true; return $short ? false : []; } + Logger::debug('Fetched public key', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $pubkey]); $pubkey = $curlResult->getBody(); } diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php index 12d05f9b3..4fcef9177 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -74,6 +74,7 @@ class Salmon } } elseif (Strings::normaliseLink($ret[$x]) == 'http://') { $ret[$x] = DI::httpClient()->fetch($ret[$x], 0, HttpClient::ACCEPT_DEFAULT); + Logger::debug('Fetched public key', ['url' => $ret[$x]]); } } } From 2ca51f752daa718e0f8e0fb913e42a34d4856f7a Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 30 Mar 2022 14:39:53 +0000 Subject: [PATCH 6/6] Use appropriate "accept" values --- mod/redir.php | 2 +- src/Model/Profile.php | 2 +- src/Network/HTTPClient/Client/HttpClient.php | 27 ++++++++++---------- src/Network/Probe.php | 2 +- src/Protocol/Salmon.php | 2 +- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mod/redir.php b/mod/redir.php index 5a707d229..dc086ae19 100644 --- a/mod/redir.php +++ b/mod/redir.php @@ -144,7 +144,7 @@ function redir_magic($a, $cid, $url) } // Test for magic auth on the target system - $serverret = DI::httpClient()->get($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if ($serverret->isSuccess()) { $separator = strpos($target_url, '?') ? '&' : '?'; $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url); diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 3a453cc87..85af20300 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -751,7 +751,7 @@ class Profile $magic_path = $basepath . '/magic' . '?owa=1&dest=' . $dest . '&' . $addr_request; // We have to check if the remote server does understand /magic without invoking something - $serverret = DI::httpClient()->get($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if ($serverret->isSuccess()) { Logger::info('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path); System::externalRedirect($magic_path); diff --git a/src/Network/HTTPClient/Client/HttpClient.php b/src/Network/HTTPClient/Client/HttpClient.php index e8e77a6b0..8cb2d9cb6 100644 --- a/src/Network/HTTPClient/Client/HttpClient.php +++ b/src/Network/HTTPClient/Client/HttpClient.php @@ -44,19 +44,20 @@ use Psr\Log\LoggerInterface; class HttpClient implements ICanSendHttpRequests { /** @var string Default value for "Accept" header */ - const ACCEPT_DEFAULT = '*/*'; - const ACCEPT_ATOM_XML = 'application/atom+xml,text/xml;q=0.9,*/*;q=0.8'; - const ACCEPT_FEED_XML = 'application/atom+xml,application/rss+xml;q=0.9,application/rdf+xml;q=0.8,text/xml;q=0.7,*/*;q=0.6'; - const ACCEPT_HTML = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; - const ACCEPT_IMAGE = 'image/png,image/jpeg,image/gif,image/*;q=0.9,*/*;q=0.8'; - const ACCEPT_JRD_JSON = 'application/jrd+json,application/json;q=0.9'; - const ACCEPT_JSON = 'application/json,*/*;q=0.9'; - const ACCEPT_JSON_AS = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"'; - const ACCEPT_MAGIC = 'application/magic-envelope+xml'; - const ACCEPT_RSS_XML = 'application/rss+xml,text/xml;q=0.9,*/*;q=0.8'; - const ACCEPT_TEXT = 'text/plain,text/*;q=0.9,*/*;q=0.8'; - const ACCEPT_VIDEO = 'video/mp4,video/*;q=0.9,*/*;q=0.8'; - const ACCEPT_XRD_XML = 'application/xrd+xml,text/xml;q=0.9,*/*;q=0.8'; + const ACCEPT_DEFAULT = '*/*'; + const ACCEPT_ATOM_XML = 'application/atom+xml,text/xml;q=0.9,*/*;q=0.8'; + const ACCEPT_FEED_XML = 'application/atom+xml,application/rss+xml;q=0.9,application/rdf+xml;q=0.8,text/xml;q=0.7,*/*;q=0.6'; + const ACCEPT_HTML = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; + const ACCEPT_IMAGE = 'image/png,image/jpeg,image/gif,image/*;q=0.9,*/*;q=0.8'; + const ACCEPT_JRD_JSON = 'application/jrd+json,application/json;q=0.9'; + const ACCEPT_JSON = 'application/json,*/*;q=0.9'; + const ACCEPT_JSON_AS = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"'; + const ACCEPT_MAGIC = 'application/magic-envelope+xml'; + const ACCEPT_MAGIC_KEY = 'application/magic-public-key'; + const ACCEPT_RSS_XML = 'application/rss+xml,text/xml;q=0.9,*/*;q=0.8'; + const ACCEPT_TEXT = 'text/plain,text/*;q=0.9,*/*;q=0.8'; + const ACCEPT_VIDEO = 'video/mp4,video/*;q=0.9,*/*;q=0.8'; + const ACCEPT_XRD_XML = 'application/xrd+xml,text/xml;q=0.9,*/*;q=0.8'; /** @var LoggerInterface */ private $logger; diff --git a/src/Network/Probe.php b/src/Network/Probe.php index b347f235a..51fb84261 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -1527,7 +1527,7 @@ class Probe $pubkey = substr($pubkey, 5); } } elseif (Strings::normaliseLink($pubkey) == 'http://') { - $curlResult = DI::httpClient()->get($pubkey, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $curlResult = DI::httpClient()->get($pubkey, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_MAGIC_KEY]); if ($curlResult->isTimeout()) { self::$istimeout = true; return $short ? false : []; diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php index 4fcef9177..2f12cdf1c 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -73,7 +73,7 @@ class Salmon $ret[$x] = substr($ret[$x], 5); } } elseif (Strings::normaliseLink($ret[$x]) == 'http://') { - $ret[$x] = DI::httpClient()->fetch($ret[$x], 0, HttpClient::ACCEPT_DEFAULT); + $ret[$x] = DI::httpClient()->fetch($ret[$x], 0, HttpClient::ACCEPT_MAGIC_KEY); Logger::debug('Fetched public key', ['url' => $ret[$x]]); } }