From 73c3b21665e7e22c4c9908272279d9605282acca Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 2 Apr 2022 20:26:11 +0200 Subject: [PATCH 1/7] Move ACCEPT constants to own "enum" class --- mod/ostatus_subscribe.php | 4 +- mod/redir.php | 4 +- src/Content/OEmbed.php | 4 +- src/Content/Text/BBCode.php | 12 ++--- src/Core/Search.php | 6 +-- src/Core/Storage/Type/ExternalResource.php | 4 +- src/Model/APContact.php | 6 +-- src/Model/GServer.php | 50 +++++++++---------- src/Model/Photo.php | 6 +-- src/Model/Post/Link.php | 4 +- src/Model/Post/Media.php | 6 +-- src/Model/Profile.php | 4 +- src/Model/User.php | 4 +- src/Module/Admin/Summary.php | 4 +- src/Module/Debug/Feed.php | 4 +- src/Module/Photo.php | 4 +- src/Module/Proxy.php | 4 +- src/Network/HTTPClient/Client/HttpClient.php | 18 +------ .../HTTPClient/Client/HttpClientAccept.php | 23 +++++++++ src/Network/Probe.php | 34 ++++++------- src/Protocol/Diaspora.php | 4 +- src/Protocol/OStatus.php | 16 +++--- src/Protocol/Salmon.php | 4 +- src/Security/ExAuth.php | 4 +- src/Util/HTTPSignature.php | 4 +- src/Util/Images.php | 4 +- src/Util/ParseUrl.php | 6 +-- src/Worker/CheckVersion.php | 4 +- src/Worker/Directory.php | 4 +- src/Worker/NodeInfo.php | 4 +- src/Worker/OnePoll.php | 4 +- src/Worker/PullDirectory.php | 4 +- src/Worker/SearchDirectory.php | 4 +- src/Worker/UpdateServerDirectory.php | 7 ++- src/Worker/UpdateServerPeers.php | 4 +- 35 files changed, 144 insertions(+), 138 deletions(-) create mode 100644 src/Network/HTTPClient/Client/HttpClientAccept.php diff --git a/mod/ostatus_subscribe.php b/mod/ostatus_subscribe.php index c1a9df7a53..1ae6047a7d 100644 --- a/mod/ostatus_subscribe.php +++ b/mod/ostatus_subscribe.php @@ -24,7 +24,7 @@ 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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Protocol\ActivityPub; @@ -59,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'], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($api . 'statuses/friends.json?screen_name=' . $contact['nick'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact'); diff --git a/mod/redir.php b/mod/redir.php index dc086ae19a..ba1b7e13b6 100644 --- a/mod/redir.php +++ b/mod/redir.php @@ -27,7 +27,7 @@ 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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Strings; @@ -144,7 +144,7 @@ function redir_magic($a, $cid, $url) } // Test for magic auth on the target system - $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); 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 0be220c517..1ef72ae1df 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -32,7 +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\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Util\DateTimeFormat; use Friendica\Util\Network; use Friendica\Util\ParseUrl; @@ -98,7 +98,7 @@ class OEmbed if (!in_array($ext, $noexts)) { // try oembed autodiscovery - $html_text = DI::httpClient()->fetch($embedurl, 15, HttpClient::ACCEPT_HTML); + $html_text = DI::httpClient()->fetch($embedurl, 15, HttpClientAccept::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 e80025bc05..3a7775a664 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -39,7 +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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Object\Image; use Friendica\Protocol\Activity; @@ -502,7 +502,7 @@ class BBCode continue; } - $curlResult = DI::httpClient()->get($mtch[1], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_IMAGE]); + $curlResult = DI::httpClient()->get($mtch[1], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]); if (!$curlResult->isSuccess()) { continue; } @@ -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'), HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout'), HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); if ($curlResult->isSuccess()) { $mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; } else { @@ -1217,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], 0, HttpClient::ACCEPT_HTML); + $body = DI::httpClient()->fetch($match[1], 0, HttpClientAccept::HTML); if (empty($body)) { DI::cache()->set($cache_key, $text); return $text; @@ -1275,7 +1275,7 @@ class BBCode return $text; } - $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout'), HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout'), HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); if ($curlResult->isSuccess()) { $mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; } else { @@ -1293,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], 0, HttpClient::ACCEPT_HTML); + $body = DI::httpClient()->fetch($match[1], 0, HttpClientAccept::HTML); if (empty($body)) { DI::cache()->set($cache_key, $text); return $text; diff --git a/src/Core/Search.php b/src/Core/Search.php index 3e768b6c75..edaf03e2d2 100644 --- a/src/Core/Search.php +++ b/src/Core/Search.php @@ -23,7 +23,7 @@ namespace Friendica\Core; use Friendica\DI; use Friendica\Model\Contact; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPException; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Object\Search\ContactResult; @@ -123,7 +123,7 @@ class Search $searchUrl .= '&page=' . $page; } - $resultJson = DI::httpClient()->fetch($searchUrl, 0, HttpClient::ACCEPT_JSON); + $resultJson = DI::httpClient()->fetch($searchUrl, 0, HttpClientAccept::JSON); $results = json_decode($resultJson, true); @@ -229,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 => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::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 3a1dc129fc..34cd85edfb 100644 --- a/src/Core/Storage/Type/ExternalResource.php +++ b/src/Core/Storage/Type/ExternalResource.php @@ -25,7 +25,7 @@ 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\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\HTTPSignature; @@ -55,7 +55,7 @@ class ExternalResource implements ICanReadFromStorage } try { - $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, [HttpClientOptions::ACCEPT_CONTENT => [HttpClient::ACCEPT_IMAGE]]); + $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); } catch (Exception $exception) { throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception); } diff --git a/src/Model/APContact.php b/src/Model/APContact.php index 9759876fab..1af2c83123 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -28,7 +28,7 @@ use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\DI; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPException; use Friendica\Network\Probe; use Friendica\Protocol\ActivityNamespace; @@ -72,10 +72,10 @@ class APContact $data = ['addr' => $addr]; $template = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr); - $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClient::ACCEPT_JRD_JSON); + $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClientAccept::JRD_JSON); if (empty($webfinger['links'])) { $template = 'http://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr); - $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClient::ACCEPT_JRD_JSON); + $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClientAccept::JRD_JSON); if (empty($webfinger['links'])) { return []; } diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 731175ea50..bf089d7541 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -32,9 +32,9 @@ use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Module\Register; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses; -use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Protocol\Relay; use Friendica\Util\DateTimeFormat; use Friendica\Util\Network; @@ -344,7 +344,7 @@ class GServer // When a nodeinfo is present, we don't need to dig further $xrd_timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if ($curlResult->isTimeout()) { self::setFailure($url); return false; @@ -352,7 +352,7 @@ class GServer // On a redirect follow the new host but mark the old one as failure if ($curlResult->isSuccess() && !empty($curlResult->getRedirectUrl()) && (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); if (!empty($curlResult->getRedirectUrl()) && parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST)) { Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]); self::setFailure($url); @@ -394,7 +394,7 @@ class GServer $basedata = ['detection-method' => self::DETECT_MANUAL]; } - $curlResult = DI::httpClient()->get($baseurl, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $curlResult = DI::httpClient()->get($baseurl, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); if ($curlResult->isSuccess()) { if (!empty($curlResult->getRedirectUrl()) && (parse_url($baseurl, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) { Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]); @@ -418,7 +418,7 @@ class GServer // When the base path doesn't seem to contain a social network we try the complete path. // Most detectable system have to be installed in the root directory. // We checked the base to avoid false positives. - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); if ($curlResult->isSuccess()) { $urldata = self::analyseRootHeader($curlResult, $serverdata); $urldata = self::analyseRootBody($curlResult, $urldata, $url); @@ -588,7 +588,7 @@ class GServer { Logger::info('Discover relay data', ['server' => $server_url]); - $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return; } @@ -683,7 +683,7 @@ class GServer */ private static function fetchStatistics(string $url) { - $curlResult = DI::httpClient()->get($url . '/statistics.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/statistics.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return []; } @@ -809,7 +809,7 @@ class GServer */ private static function parseNodeinfo1(string $nodeinfo_url) { - $curlResult = DI::httpClient()->get($nodeinfo_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($nodeinfo_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return []; } @@ -902,7 +902,7 @@ class GServer */ private static function parseNodeinfo2(string $nodeinfo_url) { - $curlResult = DI::httpClient()->get($nodeinfo_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($nodeinfo_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return []; } @@ -997,7 +997,7 @@ class GServer */ private static function fetchSiteinfo(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/siteinfo.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/siteinfo.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1082,7 +1082,7 @@ class GServer private static function validHostMeta(string $url) { $xrd_timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_XRD_XML]); + $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::XRD_XML]); if (!$curlResult->isSuccess()) { return false; } @@ -1174,7 +1174,7 @@ class GServer { $serverdata['poco'] = ''; - $curlResult = DI::httpClient()->get($url . '/poco', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/poco', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1204,7 +1204,7 @@ class GServer */ public static function checkMastodonDirectory(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1231,7 +1231,7 @@ class GServer */ private static function detectPeertube(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/config', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/api/v1/config', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1278,7 +1278,7 @@ class GServer */ private static function detectNextcloud(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/status.php', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/status.php', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1302,7 +1302,7 @@ class GServer } private static function fetchWeeklyUsage(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1370,7 +1370,7 @@ class GServer */ private static function detectMastodonAlikes(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/instance', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/api/v1/instance', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1435,7 +1435,7 @@ class GServer */ private static function detectHubzilla(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1532,7 +1532,7 @@ class GServer */ private static function detectPumpIO(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1583,7 +1583,7 @@ class GServer private static function detectGNUSocial(string $url, array $serverdata) { // Test for GNU Social - $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { $serverdata['platform'] = 'gnusocial'; @@ -1601,7 +1601,7 @@ class GServer } // Test for Statusnet - $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { @@ -1639,9 +1639,9 @@ class GServer { // There is a bug in some versions of Friendica that will return an ActivityStream actor when the content type "application/json" is requested. // Because of this me must not use ACCEPT_JSON here. - $curlResult = DI::httpClient()->get($url . '/friendica/json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $curlResult = DI::httpClient()->get($url . '/friendica/json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); if (!$curlResult->isSuccess()) { - $curlResult = DI::httpClient()->get($url . '/friendika/json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $curlResult = DI::httpClient()->get($url . '/friendika/json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); $friendika = true; $platform = 'Friendika'; } else { @@ -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), 0, HttpClient::ACCEPT_JSON); + $curlResult = DI::httpClient()->fetch('https://the-federation.info/graphql?query=' . urlencode($query), 0, HttpClientAccept::JSON); if (!empty($curlResult)) { $data = json_decode($curlResult, true); if (!empty($data['data']['nodes'])) { @@ -1963,7 +1963,7 @@ class GServer if (!empty($accesstoken)) { $api = 'https://instances.social/api/1.0/instances/list?count=0'; - $curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]], HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]], HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if ($curlResult->isSuccess()) { $servers = json_decode($curlResult->getBody(), true); diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 74171bd7bd..5701eabdb9 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -32,7 +32,7 @@ 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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Object\Image; use Friendica\Util\DateTimeFormat; @@ -499,7 +499,7 @@ class Photo $filename = basename($image_url); if (!empty($image_url)) { - $ret = DI::httpClient()->get($image_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_IMAGE]); + $ret = DI::httpClient()->get($image_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]); Logger::debug('Got picture', ['Content-Type' => $ret->getHeader('Content-Type'), 'url' => $image_url]); $img_str = $ret->getBody(); $type = $ret->getContentType(); @@ -915,7 +915,7 @@ class Photo { $filename = basename($image_url); if (!empty($image_url)) { - $ret = DI::httpClient()->get($image_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_IMAGE]); + $ret = DI::httpClient()->get($image_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]); Logger::debug('Got picture', ['Content-Type' => $ret->getHeader('Content-Type'), 'url' => $image_url]); $img_str = $ret->getBody(); $type = $ret->getContentType(); diff --git a/src/Model/Post/Link.php b/src/Model/Post/Link.php index 6a88e4e03f..fb7d0edb8d 100644 --- a/src/Model/Post/Link.php +++ b/src/Model/Post/Link.php @@ -26,7 +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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Proxy; @@ -97,7 +97,7 @@ class Link return $url . $id; } - private static function fetchMimeType(string $url, string $accept = HttpClient::ACCEPT_DEFAULT) + private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT) { $timeout = DI::config()->get('system', 'xrd_timeout'); diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index 660d5f5420..b192bfbe7d 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -30,7 +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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Images; use Friendica\Util\Network; @@ -169,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, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::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, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); + $curlResult = DI::httpClient()->get($media['url'], [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); } if ($curlResult->isSuccess()) { diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 85af203001..5fbfc4ba5a 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -35,7 +35,7 @@ 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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; @@ -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()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); 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 497787d1c8..43bb31381c 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -34,7 +34,7 @@ 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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Security\TwoFactor\Model\AppSpecificPassword; use Friendica\Network\HTTPException; @@ -1152,7 +1152,7 @@ class User $photo_failure = false; $filename = basename($photo); - $curlResult = DI::httpClient()->get($photo, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_IMAGE]); + $curlResult = DI::httpClient()->get($photo, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]); if ($curlResult->isSuccess()) { Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $photo]); $img_str = $curlResult->getBody(); diff --git a/src/Module/Admin/Summary.php b/src/Module/Admin/Summary.php index 4c6eebf33c..bebe92e957 100644 --- a/src/Module/Admin/Summary.php +++ b/src/Module/Admin/Summary.php @@ -32,7 +32,7 @@ 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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException\ServiceUnavailableException; use Friendica\Util\DateTimeFormat; @@ -250,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', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_XRD_XML])->isSuccess(); + return DI::httpClient()->get(DI::baseUrl()->get() . '/.well-known/host-meta', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::XRD_XML])->isSuccess(); } } diff --git a/src/Module/Debug/Feed.php b/src/Module/Debug/Feed.php index cd80453dd3..766f213146 100644 --- a/src/Module/Debug/Feed.php +++ b/src/Module/Debug/Feed.php @@ -28,7 +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\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Protocol; use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; @@ -61,7 +61,7 @@ class Feed extends BaseModule $contact = Model\Contact::getByURLForUser($url, local_user(), null); - $xml = $this->httpClient->fetch($contact['poll'], 0, HttpClient::ACCEPT_FEED_XML); + $xml = $this->httpClient->fetch($contact['poll'], 0, HttpClientAccept::FEED_XML); $import_result = Protocol\Feed::import($xml); diff --git a/src/Module/Photo.php b/src/Module/Photo.php index 0af2d2eb00..f2765280b6 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -32,7 +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\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPException; use Friendica\Network\HTTPException\NotModifiedException; use Friendica\Object\Image; @@ -309,7 +309,7 @@ class Photo extends BaseModule } $mimetext = ''; if (!empty($url)) { - $mime = ParseUrl::getContentType($url, HttpClient::ACCEPT_IMAGE); + $mime = ParseUrl::getContentType($url, HttpClientAccept::IMAGE); if (!empty($mime)) { $mimetext = $mime[0] . '/' . $mime[1]; } else { diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index 751b4a2984..3ef3117d69 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -25,7 +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\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException\NotModifiedException; use Friendica\Object\Image; @@ -83,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(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClient::ACCEPT_IMAGE], 'timeout' => 10]); + $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::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 8cb2d9cb60..1cd53646d1 100644 --- a/src/Network/HTTPClient/Client/HttpClient.php +++ b/src/Network/HTTPClient/Client/HttpClient.php @@ -43,22 +43,6 @@ 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_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; /** @var Profiler */ @@ -157,7 +141,7 @@ class HttpClient implements ICanSendHttpRequests }; if (empty($conf[HttpClientOptions::HEADERS]['Accept'])) { - $conf[HttpClientOptions::HEADERS]['Accept'] = static::ACCEPT_DEFAULT; + $conf[HttpClientOptions::HEADERS]['Accept'] = HttpClientAccept::DEFAULT; } try { diff --git a/src/Network/HTTPClient/Client/HttpClientAccept.php b/src/Network/HTTPClient/Client/HttpClientAccept.php new file mode 100644 index 0000000000..7df59fc3ef --- /dev/null +++ b/src/Network/HTTPClient/Client/HttpClientAccept.php @@ -0,0 +1,23 @@ + $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 => HttpClient::ACCEPT_XRD_XML]); + $curlResult = DI::httpClient()->get($ssl_url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::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 => HttpClient::ACCEPT_XRD_XML]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::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, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); if (!$curlResult->isSuccess()) { return false; } @@ -531,7 +531,7 @@ class Probe $addr = $nick . '@' . $host; } - $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr); + $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); if (empty($webfinger)) { $lrdd = self::hostMeta($host); } @@ -545,7 +545,7 @@ class Probe $addr = $nick . '@' . $host; } - $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr); + $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); if (empty($webfinger)) { $lrdd = self::hostMeta($host); } @@ -563,13 +563,13 @@ class Probe $nick = substr($uri, 0, strpos($uri, '@')); $addr = $uri; - $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr); + $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); if (self::$istimeout) { return []; } if (empty($webfinger)) { - $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr); + $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); if (self::$istimeout) { return []; } @@ -834,7 +834,7 @@ class Probe public static function pollZot($url, $data) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if ($curlResult->isTimeout()) { return $data; } @@ -1000,7 +1000,7 @@ class Probe */ private static function pollNoscrape($noscrape_url, $data) { - $curlResult = DI::httpClient()->get($noscrape_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($noscrape_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if ($curlResult->isTimeout()) { self::$istimeout = true; return $data; @@ -1266,7 +1266,7 @@ class Probe */ private static function pollHcard($hcard_url, $data, $dfrn = false) { - $curlResult = DI::httpClient()->get($hcard_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $curlResult = DI::httpClient()->get($hcard_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -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_MAGIC_KEY]); + $curlResult = DI::httpClient()->get($pubkey, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::MAGIC_KEY]); if ($curlResult->isTimeout()) { self::$istimeout = true; return $short ? false : []; @@ -1562,7 +1562,7 @@ class Probe } // Fetch all additional data from the feed - $curlResult = DI::httpClient()->get($data["poll"], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_FEED_XML]); + $curlResult = DI::httpClient()->get($data["poll"], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::FEED_XML]); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -1614,7 +1614,7 @@ class Probe */ private static function pumpioProfileData($profile_link) { - $curlResult = DI::httpClient()->get($profile_link, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $curlResult = DI::httpClient()->get($profile_link, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return []; } @@ -1827,7 +1827,7 @@ class Probe */ private static function feed($url, $probe = true) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_FEED_XML]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::FEED_XML]); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -2049,7 +2049,7 @@ class Probe return ''; } - $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if ($curlResult->isSuccess() && !empty($curlResult->getBody())) { $noscrape = json_decode($curlResult->getBody(), true); @@ -2124,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'], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); + $curlResult = DI::httpClient()->get($data['poll'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); if (!$curlResult->isSuccess() || !$curlResult->getBody()) { return ''; } diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 154d65f6f9..4c053a47c9 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -41,7 +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\HTTPClient\Client\HttpClientAccept; use Friendica\Network\Probe; use Friendica\Util\Crypto; use Friendica\Util\DateTimeFormat; @@ -1060,7 +1060,7 @@ class Diaspora Logger::info("Fetch post from ".$source_url); - $envelope = DI::httpClient()->fetch($source_url, 0, HttpClient::ACCEPT_MAGIC); + $envelope = DI::httpClient()->fetch($source_url, 0, HttpClientAccept::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 076602f9ab..ed63476288 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -38,7 +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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\Probe; use Friendica\Util\DateTimeFormat; @@ -732,7 +732,7 @@ class OStatus self::$conv_list[$conversation] = true; - $curlResult = DI::httpClient()->get($conversation, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); + $curlResult = DI::httpClient()->get($conversation, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return; @@ -762,7 +762,7 @@ class OStatus } } if ($file != '') { - $conversation_atom = DI::httpClient()->get($attribute['href'], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); + $conversation_atom = DI::httpClient()->get($attribute['href'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); if ($conversation_atom->isSuccess()) { $xml = $conversation_atom->getBody(); @@ -876,7 +876,7 @@ class OStatus return; } - $curlResult = DI::httpClient()->get($self, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); + $curlResult = DI::httpClient()->get($self, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); if (!$curlResult->isSuccess()) { return; @@ -926,7 +926,7 @@ class OStatus } $stored = false; - $curlResult = DI::httpClient()->get($related, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); + $curlResult = DI::httpClient()->get($related, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return; @@ -958,7 +958,7 @@ class OStatus } } if ($atom_file != '') { - $curlResult = DI::httpClient()->get($atom_file, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); + $curlResult = DI::httpClient()->get($atom_file, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); if ($curlResult->isSuccess()) { Logger::info('Fetched XML for URI ' . $related_uri); @@ -970,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', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); + $curlResult = DI::httpClient()->get(str_replace('/notice/', '/api/statuses/show/', $related) . '.atom', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); if ($curlResult->isSuccess()) { Logger::info('GNU Social workaround to fetch XML for URI ' . $related_uri); @@ -981,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', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_ATOM_XML]); + $curlResult = DI::httpClient()->get(str_replace('/notice/', '/api/statuses/show/', $related_guess) . '.atom', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::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 2f12cdf1c8..c24e7f78a3 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -23,7 +23,7 @@ namespace Friendica\Protocol; use Friendica\Core\Logger; use Friendica\DI; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\Probe; use Friendica\Util\Crypto; use Friendica\Util\Strings; @@ -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_MAGIC_KEY); + $ret[$x] = DI::httpClient()->fetch($ret[$x], 0, HttpClientAccept::MAGIC_KEY); Logger::debug('Fetched public key', ['url' => $ret[$x]]); } } diff --git a/src/Security/ExAuth.php b/src/Security/ExAuth.php index f324b0a960..71eac5ec95 100644 --- a/src/Security/ExAuth.php +++ b/src/Security/ExAuth.php @@ -57,7 +57,7 @@ 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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException; use Friendica\Util\PidFile; @@ -241,7 +241,7 @@ class ExAuth $url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user; - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$curlResult->isSuccess()) { return false; diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index 2a48267191..161b0da2d8 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -28,7 +28,7 @@ use Friendica\DI; use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Model\User; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; /** @@ -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 = [HttpClientOptions::ACCEPT_CONTENT => [HttpClient::ACCEPT_JSON_AS]]) + public static function fetchRaw($request, $uid = 0, $opts = [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::JSON_AS]]) { $header = []; diff --git a/src/Util/Images.php b/src/Util/Images.php index f1e85a37d1..c69b7cb9e0 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -24,7 +24,7 @@ namespace Friendica\Util; use Friendica\Core\Logger; use Friendica\DI; use Friendica\Model\Photo; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; /** * Image utilities @@ -220,7 +220,7 @@ class Images } if (empty($img_str)) { - $img_str = DI::httpClient()->fetch($url, 4, HttpClient::ACCEPT_IMAGE); + $img_str = DI::httpClient()->fetch($url, 4, HttpClientAccept::IMAGE); } if (!$img_str) { diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 06ef3bdd0c..08d68635a9 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -29,7 +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\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPException; use Friendica\Network\HTTPClient\Client\HttpClientOptions; @@ -59,7 +59,7 @@ class ParseUrl * @param string $accept content-type to accept * @return array content type */ - public static function getContentType(string $url, string $accept = HttpClient::ACCEPT_DEFAULT) + public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT) { $curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => $accept]); @@ -222,7 +222,7 @@ class ParseUrl return $siteinfo; } - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::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 d803d9e3ad..87a9e2a0ac 100644 --- a/src/Worker/CheckVersion.php +++ b/src/Worker/CheckVersion.php @@ -24,7 +24,7 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; /** * Check the git repository VERSION file and save the version to the DB @@ -55,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, 0, HttpClient::ACCEPT_TEXT))); + $gitversion = DBA::escape(trim(DI::httpClient()->fetch($checked_url, 0, HttpClientAccept::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 d977e522d0..95d3b3bcc8 100644 --- a/src/Worker/Directory.php +++ b/src/Worker/Directory.php @@ -26,7 +26,7 @@ use Friendica\Core\Logger; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; /** * Sends updated profile data to the directory @@ -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_HTML); + DI::httpClient()->fetch($dir . '?url=' . bin2hex($arr['url']), 0, HttpClientAccept::HTML); } return; diff --git a/src/Worker/NodeInfo.php b/src/Worker/NodeInfo.php index f55c494ebd..0079eb9cd6 100644 --- a/src/Worker/NodeInfo.php +++ b/src/Worker/NodeInfo.php @@ -24,7 +24,7 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\DI; use Friendica\Model\Nodeinfo as ModelNodeInfo; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; class NodeInfo { @@ -35,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, 0, HttpClient::ACCEPT_HTML); + $ret = DI::httpClient()->fetch($url, 0, HttpClientAccept::HTML); Logger::debug('Check registering answer', ['answer' => $ret]); Logger::info('end'); } diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 839ab6ce13..79369681ca 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -30,7 +30,7 @@ use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\User; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; @@ -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 => HttpClientAccept::FEED_XML]); unlink($cookiejar); if ($curlResult->isTimeout()) { diff --git a/src/Worker/PullDirectory.php b/src/Worker/PullDirectory.php index 0ea03a17f3..87558834ad 100644 --- a/src/Worker/PullDirectory.php +++ b/src/Worker/PullDirectory.php @@ -24,7 +24,7 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\DI; use Friendica\Model\Contact; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; class PullDirectory { @@ -48,7 +48,7 @@ class PullDirectory Logger::info('Synchronization started.', ['now' => $now, 'directory' => $directory]); - $result = DI::httpClient()->fetch($directory . '/sync/pull/since/' . $now, 0, HttpClient::ACCEPT_JSON); + $result = DI::httpClient()->fetch($directory . '/sync/pull/since/' . $now, 0, HttpClientAccept::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 2344bb1bf6..7a36d20724 100644 --- a/src/Worker/SearchDirectory.php +++ b/src/Worker/SearchDirectory.php @@ -26,7 +26,7 @@ use Friendica\Core\Logger; use Friendica\Core\Search; use Friendica\DI; use Friendica\Model\Contact; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; class SearchDirectory { @@ -47,7 +47,7 @@ class SearchDirectory } } - $x = DI::httpClient()->fetch(Search::getGlobalDirectory() . '/lsearch?p=1&n=500&search=' . urlencode($search), 0, HttpClient::ACCEPT_JSON); + $x = DI::httpClient()->fetch(Search::getGlobalDirectory() . '/lsearch?p=1&n=500&search=' . urlencode($search), 0, HttpClientAccept::JSON); $j = json_decode($x); if (!empty($j->results)) { diff --git a/src/Worker/UpdateServerDirectory.php b/src/Worker/UpdateServerDirectory.php index 248ab1121b..b3d9c87d2a 100644 --- a/src/Worker/UpdateServerDirectory.php +++ b/src/Worker/UpdateServerDirectory.php @@ -22,11 +22,10 @@ namespace Friendica\Worker; use Friendica\Core\Logger; -use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\GServer; -use Friendica\Network\HTTPClient\Client\HttpClient; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; class UpdateServerDirectory { @@ -46,7 +45,7 @@ class UpdateServerDirectory private static function discoverPoCo(array $gserver) { - $result = DI::httpClient()->fetch($gserver['poco'] . '?fields=urls', 0, HttpClient::ACCEPT_JSON); + $result = DI::httpClient()->fetch($gserver['poco'] . '?fields=urls', 0, HttpClientAccept::JSON); if (empty($result)) { Logger::info('Empty result', ['url' => $gserver['url']]); return; @@ -79,7 +78,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', 0, HttpClient::ACCEPT_JSON); + $result = DI::httpClient()->fetch($gserver['url'] . '/api/v1/directory?order=new&local=true&limit=200&offset=0', 0, HttpClientAccept::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 d2a127b82f..5a889a0ea2 100644 --- a/src/Worker/UpdateServerPeers.php +++ b/src/Worker/UpdateServerPeers.php @@ -25,7 +25,7 @@ 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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Strings; @@ -37,7 +37,7 @@ class UpdateServerPeers */ public static function execute(string $url) { - $ret = DI::httpClient()->get($url . '/api/v1/instance/peers', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); + $ret = DI::httpClient()->get($url . '/api/v1/instance/peers', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); if (!$ret->isSuccess() || empty($ret->getBody())) { Logger::info('Server is not reachable or does not offer the "peers" endpoint', ['url' => $url]); return; From e299fc67c8809ecf1018e6680a3a5284bcf7d8d9 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 2 Apr 2022 21:16:22 +0200 Subject: [PATCH 2/7] Introduce "accept_header" as specific argument to the http client --- mod/ostatus_subscribe.php | 3 +- mod/redir.php | 3 +- src/Content/OEmbed.php | 2 +- src/Content/Text/BBCode.php | 10 +-- src/Core/Search.php | 5 +- src/Model/GServer.php | 48 ++++++------ src/Model/Photo.php | 5 +- src/Model/Post/Link.php | 2 +- src/Model/Post/Media.php | 4 +- src/Model/Profile.php | 3 +- src/Model/User.php | 3 +- src/Module/Admin/Summary.php | 3 +- src/Module/Debug/Feed.php | 2 +- src/Module/Magic.php | 4 +- .../Capability/ICanSendHttpRequests.php | 76 ++++++++++--------- src/Network/HTTPClient/Client/HttpClient.php | 28 +++---- .../HTTPClient/Client/HttpClientAccept.php | 4 + src/Network/Probe.php | 26 +++---- src/Protocol/DFRN.php | 3 +- src/Protocol/Diaspora.php | 4 +- src/Protocol/OStatus.php | 15 ++-- src/Protocol/Salmon.php | 8 +- src/Security/ExAuth.php | 3 +- src/Util/HTTPSignature.php | 6 +- src/Util/Images.php | 2 +- src/Util/ParseUrl.php | 6 +- src/Worker/CheckVersion.php | 2 +- src/Worker/Directory.php | 2 +- src/Worker/NodeInfo.php | 2 +- src/Worker/OnePoll.php | 2 +- src/Worker/PubSubPublish.php | 3 +- src/Worker/PullDirectory.php | 2 +- src/Worker/SearchDirectory.php | 2 +- src/Worker/UpdateServerDirectory.php | 4 +- src/Worker/UpdateServerPeers.php | 3 +- 35 files changed, 150 insertions(+), 150 deletions(-) diff --git a/mod/ostatus_subscribe.php b/mod/ostatus_subscribe.php index 1ae6047a7d..781de3493a 100644 --- a/mod/ostatus_subscribe.php +++ b/mod/ostatus_subscribe.php @@ -25,7 +25,6 @@ use Friendica\DI; use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Protocol\ActivityPub; function ostatus_subscribe_content(App $a) @@ -59,7 +58,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'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($api . 'statuses/friends.json?screen_name=' . $contact['nick'], HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact'); diff --git a/mod/redir.php b/mod/redir.php index ba1b7e13b6..7ebce17948 100644 --- a/mod/redir.php +++ b/mod/redir.php @@ -28,7 +28,6 @@ use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Profile; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Strings; function redir_init(App $a) { @@ -144,7 +143,7 @@ function redir_magic($a, $cid, $url) } // Test for magic auth on the target system - $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $serverret = DI::httpClient()->head($basepath . '/magic', HttpClientAccept::HTML); 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 1ef72ae1df..dbba99fcff 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -98,7 +98,7 @@ class OEmbed if (!in_array($ext, $noexts)) { // try oembed autodiscovery - $html_text = DI::httpClient()->fetch($embedurl, 15, HttpClientAccept::HTML); + $html_text = DI::httpClient()->fetch($embedurl, HttpClientAccept::HTML, 15); 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 3a7775a664..bb92640a1d 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -502,7 +502,7 @@ class BBCode continue; } - $curlResult = DI::httpClient()->get($mtch[1], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]); + $curlResult = DI::httpClient()->get($mtch[1], HttpClientAccept::IMAGE); if (!$curlResult->isSuccess()) { continue; } @@ -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'), HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); + $curlResult = DI::httpClient()->head($match[1], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); if ($curlResult->isSuccess()) { $mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; } else { @@ -1217,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], 0, HttpClientAccept::HTML); + $body = DI::httpClient()->fetch($match[1], HttpClientAccept::HTML, 0); if (empty($body)) { DI::cache()->set($cache_key, $text); return $text; @@ -1275,7 +1275,7 @@ class BBCode return $text; } - $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout'), HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); + $curlResult = DI::httpClient()->head($match[1], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); if ($curlResult->isSuccess()) { $mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; } else { @@ -1293,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], 0, HttpClientAccept::HTML); + $body = DI::httpClient()->fetch($match[1], HttpClientAccept::HTML, 0); if (empty($body)) { DI::cache()->set($cache_key, $text); return $text; diff --git a/src/Core/Search.php b/src/Core/Search.php index edaf03e2d2..17b9eeb8ff 100644 --- a/src/Core/Search.php +++ b/src/Core/Search.php @@ -25,7 +25,6 @@ use Friendica\DI; use Friendica\Model\Contact; use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPException; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Object\Search\ContactResult; use Friendica\Object\Search\ResultList; use Friendica\Util\Network; @@ -123,7 +122,7 @@ class Search $searchUrl .= '&page=' . $page; } - $resultJson = DI::httpClient()->fetch($searchUrl, 0, HttpClientAccept::JSON); + $resultJson = DI::httpClient()->fetch($searchUrl, HttpClientAccept::JSON); $results = json_decode($resultJson, true); @@ -229,7 +228,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 => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), HttpClientAccept::JSON); if ($curlResult->isSuccess()) { $searchResult = json_decode($curlResult->getBody(), true); if (!empty($searchResult['profiles'])) { diff --git a/src/Model/GServer.php b/src/Model/GServer.php index bf089d7541..dd38367c67 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -344,7 +344,7 @@ class GServer // When a nodeinfo is present, we don't need to dig further $xrd_timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', HttpClientAccept::JSON, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if ($curlResult->isTimeout()) { self::setFailure($url); return false; @@ -352,7 +352,7 @@ class GServer // On a redirect follow the new host but mark the old one as failure if ($curlResult->isSuccess() && !empty($curlResult->getRedirectUrl()) && (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if (!empty($curlResult->getRedirectUrl()) && parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST)) { Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]); self::setFailure($url); @@ -394,7 +394,7 @@ class GServer $basedata = ['detection-method' => self::DETECT_MANUAL]; } - $curlResult = DI::httpClient()->get($baseurl, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $curlResult = DI::httpClient()->get($baseurl, HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if ($curlResult->isSuccess()) { if (!empty($curlResult->getRedirectUrl()) && (parse_url($baseurl, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) { Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]); @@ -418,7 +418,7 @@ class GServer // When the base path doesn't seem to contain a social network we try the complete path. // Most detectable system have to be installed in the root directory. // We checked the base to avoid false positives. - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if ($curlResult->isSuccess()) { $urldata = self::analyseRootHeader($curlResult, $serverdata); $urldata = self::analyseRootBody($curlResult, $urldata, $url); @@ -588,7 +588,7 @@ class GServer { Logger::info('Discover relay data', ['server' => $server_url]); - $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay', HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return; } @@ -683,7 +683,7 @@ class GServer */ private static function fetchStatistics(string $url) { - $curlResult = DI::httpClient()->get($url . '/statistics.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/statistics.json', HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return []; } @@ -809,7 +809,7 @@ class GServer */ private static function parseNodeinfo1(string $nodeinfo_url) { - $curlResult = DI::httpClient()->get($nodeinfo_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return []; } @@ -902,7 +902,7 @@ class GServer */ private static function parseNodeinfo2(string $nodeinfo_url) { - $curlResult = DI::httpClient()->get($nodeinfo_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return []; } @@ -997,7 +997,7 @@ class GServer */ private static function fetchSiteinfo(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/siteinfo.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/siteinfo.json', HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1082,7 +1082,7 @@ class GServer private static function validHostMeta(string $url) { $xrd_timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::XRD_XML]); + $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if (!$curlResult->isSuccess()) { return false; } @@ -1174,7 +1174,7 @@ class GServer { $serverdata['poco'] = ''; - $curlResult = DI::httpClient()->get($url . '/poco', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/poco', HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1204,7 +1204,7 @@ class GServer */ public static function checkMastodonDirectory(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1231,7 +1231,7 @@ class GServer */ private static function detectPeertube(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/config', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/api/v1/config', HttpClientAccept::JSON); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1278,7 +1278,7 @@ class GServer */ private static function detectNextcloud(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/status.php', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/status.php', HttpClientAccept::JSON); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1302,7 +1302,7 @@ class GServer } private static function fetchWeeklyUsage(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', HttpClientAccept::JSON); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1370,7 +1370,7 @@ class GServer */ private static function detectMastodonAlikes(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/instance', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1435,7 +1435,7 @@ class GServer */ private static function detectHubzilla(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', HttpClientAccept::JSON); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1532,7 +1532,7 @@ class GServer */ private static function detectPumpIO(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json', HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1583,7 +1583,7 @@ class GServer private static function detectGNUSocial(string $url, array $serverdata) { // Test for GNU Social - $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', HttpClientAccept::JSON); if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { $serverdata['platform'] = 'gnusocial'; @@ -1601,7 +1601,7 @@ class GServer } // Test for Statusnet - $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json', HttpClientAccept::JSON); if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { @@ -1639,9 +1639,9 @@ class GServer { // There is a bug in some versions of Friendica that will return an ActivityStream actor when the content type "application/json" is requested. // Because of this me must not use ACCEPT_JSON here. - $curlResult = DI::httpClient()->get($url . '/friendica/json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); + $curlResult = DI::httpClient()->get($url . '/friendica/json'); if (!$curlResult->isSuccess()) { - $curlResult = DI::httpClient()->get($url . '/friendika/json', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); + $curlResult = DI::httpClient()->get($url . '/friendika/json'); $friendika = true; $platform = 'Friendika'; } else { @@ -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), 0, HttpClientAccept::JSON); + $curlResult = DI::httpClient()->fetch('https://the-federation.info/graphql?query=' . urlencode($query), HttpClientAccept::JSON); if (!empty($curlResult)) { $data = json_decode($curlResult, true); if (!empty($data['data']['nodes'])) { @@ -1963,7 +1963,7 @@ class GServer if (!empty($accesstoken)) { $api = 'https://instances.social/api/1.0/instances/list?count=0'; - $curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]], HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($api, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]); if ($curlResult->isSuccess()) { $servers = json_decode($curlResult->getBody(), true); diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 5701eabdb9..8583577890 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -33,7 +33,6 @@ use Friendica\Core\Storage\Exception\ReferenceStorageException; use Friendica\Core\Storage\Exception\StorageException; use Friendica\Core\Storage\Type\SystemResource; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Object\Image; use Friendica\Util\DateTimeFormat; use Friendica\Util\Images; @@ -499,7 +498,7 @@ class Photo $filename = basename($image_url); if (!empty($image_url)) { - $ret = DI::httpClient()->get($image_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]); + $ret = DI::httpClient()->get($image_url, HttpClientAccept::IMAGE); Logger::debug('Got picture', ['Content-Type' => $ret->getHeader('Content-Type'), 'url' => $image_url]); $img_str = $ret->getBody(); $type = $ret->getContentType(); @@ -915,7 +914,7 @@ class Photo { $filename = basename($image_url); if (!empty($image_url)) { - $ret = DI::httpClient()->get($image_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]); + $ret = DI::httpClient()->get($image_url, HttpClientAccept::IMAGE); Logger::debug('Got picture', ['Content-Type' => $ret->getHeader('Content-Type'), 'url' => $image_url]); $img_str = $ret->getBody(); $type = $ret->getContentType(); diff --git a/src/Model/Post/Link.php b/src/Model/Post/Link.php index fb7d0edb8d..cc93398a46 100644 --- a/src/Model/Post/Link.php +++ b/src/Model/Post/Link.php @@ -101,7 +101,7 @@ class Link { $timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]); + $curlResult = DI::httpClient()->head($url, $accept, [HttpClientOptions::TIMEOUT => $timeout]); if ($curlResult->isSuccess()) { if (empty($media['mimetype'])) { return $curlResult->getHeader('Content-Type')[0] ?? ''; diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index b192bfbe7d..ee50061cb8 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -169,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, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); + $curlResult = DI::httpClient()->head($media['url'], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]); // 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, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::DEFAULT]); + $curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]); } if ($curlResult->isSuccess()) { diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 5fbfc4ba5a..2af69d53ac 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -36,7 +36,6 @@ use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; @@ -751,7 +750,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()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $serverret = DI::httpClient()->head($basepath . '/magic', HttpClientAccept::HTML); 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 43bb31381c..500b63658d 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -35,7 +35,6 @@ use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Security\TwoFactor\Model\AppSpecificPassword; use Friendica\Network\HTTPException; use Friendica\Object\Image; @@ -1152,7 +1151,7 @@ class User $photo_failure = false; $filename = basename($photo); - $curlResult = DI::httpClient()->get($photo, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]); + $curlResult = DI::httpClient()->get($photo, HttpClientAccept::IMAGE); if ($curlResult->isSuccess()) { Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $photo]); $img_str = $curlResult->getBody(); diff --git a/src/Module/Admin/Summary.php b/src/Module/Admin/Summary.php index bebe92e957..557ecd7f58 100644 --- a/src/Module/Admin/Summary.php +++ b/src/Module/Admin/Summary.php @@ -33,7 +33,6 @@ use Friendica\Core\Config\Factory\Config; use Friendica\Model\Register; use Friendica\Module\BaseAdmin; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException\ServiceUnavailableException; use Friendica\Util\DateTimeFormat; @@ -250,7 +249,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', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::XRD_XML])->isSuccess(); + return DI::httpClient()->get(DI::baseUrl()->get() . '/.well-known/host-meta', HttpClientAccept::XRD_XML)->isSuccess(); } } diff --git a/src/Module/Debug/Feed.php b/src/Module/Debug/Feed.php index 766f213146..46ded4236b 100644 --- a/src/Module/Debug/Feed.php +++ b/src/Module/Debug/Feed.php @@ -61,7 +61,7 @@ class Feed extends BaseModule $contact = Model\Contact::getByURLForUser($url, local_user(), null); - $xml = $this->httpClient->fetch($contact['poll'], 0, HttpClientAccept::FEED_XML); + $xml = $this->httpClient->fetch($contact['poll'], HttpClientAccept::FEED_XML); $import_result = Protocol\Feed::import($xml); diff --git a/src/Module/Magic.php b/src/Module/Magic.php index 16b3492376..e4ed1e93bf 100644 --- a/src/Module/Magic.php +++ b/src/Module/Magic.php @@ -29,7 +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\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\HTTPSignature; use Friendica\Util\Profiler; @@ -113,7 +113,7 @@ class Magic extends BaseModule ); // Try to get an authentication token from the other instance. - $curlResult = $this->httpClient->get($basepath . '/owa', [HttpClientOptions::HEADERS => $header]); + $curlResult = $this->httpClient->get($basepath . '/owa', HttpClientAccept::DEFAULT, [HttpClientOptions::HEADERS => $header]); if ($curlResult->isSuccess()) { $j = json_decode($curlResult->getBody(), true); diff --git a/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php b/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php index d84b44d454..ed42c75455 100644 --- a/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php +++ b/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php @@ -21,6 +21,7 @@ namespace Friendica\Network\HTTPClient\Capability; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use GuzzleHttp\Exception\TransferException; /** @@ -35,13 +36,13 @@ interface ICanSendHttpRequests * to preserve cookies from one request to the next. * * @param string $url URL to fetch - * @param int $timeout Timeout in seconds, default system config value or 60 seconds * @param string $accept_content supply Accept: header with 'accept_content' as the value + * @param int $timeout Timeout in seconds, default system config value or 60 seconds * @param string $cookiejar Path to cookie jar file * * @return string The fetched content */ - public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string; + public function fetch(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): string; /** * Fetches the whole response of an URL. @@ -50,72 +51,75 @@ interface ICanSendHttpRequests * all the information collected during the fetch. * * @param string $url URL to fetch - * @param int $timeout Timeout in seconds, default system config value or 60 seconds * @param string $accept_content supply Accept: header with 'accept_content' as the value + * @param int $timeout Timeout in seconds, default system config value or 60 seconds * @param string $cookiejar Path to cookie jar file * * @return ICanHandleHttpResponses With all relevant information, 'body' contains the actual fetched content. */ - public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses; + public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses; /** * Send a HEAD to a URL. * - * @param string $url URL to fetch - * @param array $opts (optional parameters) associative array with: - * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value - * 'timeout' => int Timeout in seconds, default system config value or 60 seconds - * 'cookiejar' => path to cookie jar file - * 'header' => header array + * @param string $url URL to fetch + * @param string $accept_content supply Accept: header with 'accept_content' as the value + * @param array $opts (optional parameters) associative array with: + * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter) + * 'timeout' => int Timeout in seconds, default system config value or 60 seconds + * 'cookiejar' => path to cookie jar file + * 'header' => header array * * @return ICanHandleHttpResponses */ - public function head(string $url, array $opts = []): ICanHandleHttpResponses; + public function head(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses; /** - * Send a GET to an URL. + * Send a GET to a URL. * - * @param string $url URL to fetch - * @param array $opts (optional parameters) associative array with: - * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value - * 'timeout' => int Timeout in seconds, default system config value or 60 seconds - * 'cookiejar' => path to cookie jar file - * 'header' => header array - * 'content_length' => int maximum File content length + * @param string $url URL to get + * @param string $accept_content supply Accept: header with 'accept_content' as the value + * @param array $opts (optional parameters) associative array with: + * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter) + * 'timeout' => int Timeout in seconds, default system config value or 60 seconds + * 'cookiejar' => path to cookie jar file + * 'header' => header array * * @return ICanHandleHttpResponses */ - public function get(string $url, array $opts = []): ICanHandleHttpResponses; + public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses; /** * Sends a HTTP request to a given url * - * @param string $method A HTTP request - * @param string $url Url to send to - * @param array $opts (optional parameters) associative array with: - * 'body' => (mixed) setting the body for sending data - * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value - * 'timeout' => int Timeout in seconds, default system config value or 60 seconds - * 'cookiejar' => path to cookie jar file - * 'header' => header array - * 'content_length' => int maximum File content length - * 'auth' => array authentication settings + * @param string $method A HTTP request + * @param string $url Url to send to + * @param string $accept_content supply Accept: header with 'accept_content' as the value + * @param array $opts (optional parameters) associative array with: + * 'body' => (mixed) setting the body for sending data + * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter) + * 'timeout' => int Timeout in seconds, default system config value or 60 seconds + * 'cookiejar' => path to cookie jar file + * 'header' => header array + * 'content_length' => int maximum File content length + * 'auth' => array authentication settings * * @return ICanHandleHttpResponses */ - public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses; + public function request(string $method, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses; /** * Send POST request to an URL * - * @param string $url URL to post - * @param mixed $params array of POST variables - * @param array $headers HTTP headers - * @param int $timeout The timeout in seconds, default system config value or 60 seconds + * @param string $url URL to post + * @param mixed $params array of POST variables + * @param string $accept_content supply Accept: header with 'accept_content' as the value + * @param array $headers HTTP headers + * @param int $timeout The timeout in seconds, default system config value or 60 seconds * * @return ICanHandleHttpResponses The content */ - public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses; + public function post(string $url, $params, string $accept_content = HttpClientAccept::DEFAULT, array $headers = [], int $timeout = 0): ICanHandleHttpResponses; /** * Returns the original URL of the provided URL diff --git a/src/Network/HTTPClient/Client/HttpClient.php b/src/Network/HTTPClient/Client/HttpClient.php index 1cd53646d1..da3af042da 100644 --- a/src/Network/HTTPClient/Client/HttpClient.php +++ b/src/Network/HTTPClient/Client/HttpClient.php @@ -63,7 +63,7 @@ class HttpClient implements ICanSendHttpRequests /** * {@inheritDoc} */ - public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses + public function request(string $method, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses { $this->profiler->startRecording('network'); $this->logger->debug('Request start.', ['url' => $url, 'method' => $method]); @@ -141,7 +141,7 @@ class HttpClient implements ICanSendHttpRequests }; if (empty($conf[HttpClientOptions::HEADERS]['Accept'])) { - $conf[HttpClientOptions::HEADERS]['Accept'] = HttpClientAccept::DEFAULT; + $conf[HttpClientOptions::HEADERS]['Accept'] = $accept_content; } try { @@ -167,23 +167,23 @@ class HttpClient implements ICanSendHttpRequests /** {@inheritDoc} */ - public function head(string $url, array $opts = []): ICanHandleHttpResponses + public function head(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses { - return $this->request('head', $url, $opts); + return $this->request('head', $url, $accept_content, $opts); } /** * {@inheritDoc} */ - public function get(string $url, array $opts = []): ICanHandleHttpResponses + public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses { - return $this->request('get', $url, $opts); + return $this->request('get', $url, $accept_content, $opts); } /** * {@inheritDoc} */ - public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses + public function post(string $url, $params, string $accept_content = HttpClientAccept::DEFAULT, array $headers = [], int $timeout = 0): ICanHandleHttpResponses { $opts = []; @@ -197,7 +197,7 @@ class HttpClient implements ICanSendHttpRequests $opts[HttpClientOptions::TIMEOUT] = $timeout; } - return $this->request('post', $url, $opts); + return $this->request('post', $url, $accept_content, $opts); } /** @@ -237,9 +237,9 @@ class HttpClient implements ICanSendHttpRequests /** * {@inheritDoc} */ - public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string + public function fetch(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): string { - $ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar); + $ret = $this->fetchFull($url, $accept_content, $timeout, $cookiejar); return $ret->getBody(); } @@ -247,14 +247,14 @@ class HttpClient implements ICanSendHttpRequests /** * {@inheritDoc} */ - public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses + public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses { return $this->get( $url, + $accept_content, [ - HttpClientOptions::TIMEOUT => $timeout, - HttpClientOptions::ACCEPT_CONTENT => $accept_content, - HttpClientOptions::COOKIEJAR => $cookiejar + HttpClientOptions::TIMEOUT => $timeout, + HttpClientOptions::COOKIEJAR => $cookiejar ] ); } diff --git a/src/Network/HTTPClient/Client/HttpClientAccept.php b/src/Network/HTTPClient/Client/HttpClientAccept.php index 7df59fc3ef..4837ba6aed 100644 --- a/src/Network/HTTPClient/Client/HttpClientAccept.php +++ b/src/Network/HTTPClient/Client/HttpClientAccept.php @@ -2,6 +2,9 @@ namespace Friendica\Network\HTTPClient\Client; +/** + * This class contains a list of possible HTTPClient ACCEPT options. + */ class HttpClientAccept { /** @var string Default value for "Accept" header */ @@ -20,4 +23,5 @@ class HttpClientAccept public const TEXT = 'text/plain,text/*;q=0.9,*/*;q=0.8'; public const VIDEO = 'video/mp4,video/*;q=0.9,*/*;q=0.8'; public const XRD_XML = 'application/xrd+xml,text/xml;q=0.9,*/*;q=0.8'; + public const XML = 'application/xml,text/xml;q=0.9,*/*;q=0.8'; } diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 8fb9fe85ed..82d608f2c4 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 => HttpClientAccept::XRD_XML]); + $curlResult = DI::httpClient()->get($ssl_url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); $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 => HttpClientAccept::XRD_XML]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); $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, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000]); if (!$curlResult->isSuccess()) { return false; } @@ -834,7 +834,7 @@ class Probe public static function pollZot($url, $data) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::JSON); if ($curlResult->isTimeout()) { return $data; } @@ -931,7 +931,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, $type, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -1000,7 +1000,7 @@ class Probe */ private static function pollNoscrape($noscrape_url, $data) { - $curlResult = DI::httpClient()->get($noscrape_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON); if ($curlResult->isTimeout()) { self::$istimeout = true; return $data; @@ -1266,7 +1266,7 @@ class Probe */ private static function pollHcard($hcard_url, $data, $dfrn = false) { - $curlResult = DI::httpClient()->get($hcard_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -1527,7 +1527,7 @@ class Probe $pubkey = substr($pubkey, 5); } } elseif (Strings::normaliseLink($pubkey) == 'http://') { - $curlResult = DI::httpClient()->get($pubkey, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::MAGIC_KEY]); + $curlResult = DI::httpClient()->get($pubkey, HttpClientAccept::MAGIC_KEY); if ($curlResult->isTimeout()) { self::$istimeout = true; return $short ? false : []; @@ -1562,7 +1562,7 @@ class Probe } // Fetch all additional data from the feed - $curlResult = DI::httpClient()->get($data["poll"], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::FEED_XML]); + $curlResult = DI::httpClient()->get($data["poll"], HttpClientAccept::FEED_XML); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -1614,7 +1614,7 @@ class Probe */ private static function pumpioProfileData($profile_link) { - $curlResult = DI::httpClient()->get($profile_link, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $curlResult = DI::httpClient()->get($profile_link, HttpClientAccept::HTML); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return []; } @@ -1827,7 +1827,7 @@ class Probe */ private static function feed($url, $probe = true) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::FEED_XML]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML); if ($curlResult->isTimeout()) { self::$istimeout = true; return []; @@ -2049,7 +2049,7 @@ class Probe return ''; } - $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], HttpClientAccept::JSON); if ($curlResult->isSuccess() && !empty($curlResult->getBody())) { $noscrape = json_decode($curlResult->getBody(), true); @@ -2124,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'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); + $curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::ATOM_XML); if (!$curlResult->isSuccess() || !$curlResult->getBody()) { return ''; } diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 0fb7394579..bd90456c0c 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -42,6 +42,7 @@ use Friendica\Model\Post; use Friendica\Model\Profile; use Friendica\Model\Tag; use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\Probe; use Friendica\Util\Crypto; use Friendica\Util\DateTimeFormat; @@ -1013,7 +1014,7 @@ class DFRN $content_type = ($public_batch ? "application/magic-envelope+xml" : "application/json"); - $postResult = DI::httpClient()->post($dest_url, $envelope, ['Content-Type' => $content_type]); + $postResult = DI::httpClient()->post($dest_url, $envelope, HttpClientAccept::DEFAULT, ['Content-Type' => $content_type]); $xml = $postResult->getBody(); $curl_stat = $postResult->getReturnCode(); diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 4c053a47c9..828237a5e7 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -1060,7 +1060,7 @@ class Diaspora Logger::info("Fetch post from ".$source_url); - $envelope = DI::httpClient()->fetch($source_url, 0, HttpClientAccept::MAGIC); + $envelope = DI::httpClient()->fetch($source_url, HttpClientAccept::MAGIC); if ($envelope) { Logger::info("Envelope was fetched."); $x = self::verifyMagicEnvelope($envelope); @@ -3018,7 +3018,7 @@ class Diaspora if (!intval(DI::config()->get("system", "diaspora_test"))) { $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json"); - $postResult = DI::httpClient()->post($dest_url . "/", $envelope, ['Content-Type' => $content_type]); + $postResult = DI::httpClient()->post($dest_url . "/", $envelope, HttpClientAccept::DEFAULT, ['Content-Type' => $content_type]); $return_code = $postResult->getReturnCode(); } else { Logger::notice("test_mode"); diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index ed63476288..fe8f060aa3 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -39,7 +39,6 @@ use Friendica\Model\Post; use Friendica\Model\Tag; use Friendica\Model\User; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\Probe; use Friendica\Util\DateTimeFormat; use Friendica\Util\Images; @@ -732,7 +731,7 @@ class OStatus self::$conv_list[$conversation] = true; - $curlResult = DI::httpClient()->get($conversation, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); + $curlResult = DI::httpClient()->get($conversation, HttpClientAccept::ATOM_XML); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return; @@ -762,7 +761,7 @@ class OStatus } } if ($file != '') { - $conversation_atom = DI::httpClient()->get($attribute['href'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); + $conversation_atom = DI::httpClient()->get($attribute['href'], HttpClientAccept::ATOM_XML); if ($conversation_atom->isSuccess()) { $xml = $conversation_atom->getBody(); @@ -876,7 +875,7 @@ class OStatus return; } - $curlResult = DI::httpClient()->get($self, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); + $curlResult = DI::httpClient()->get($self, HttpClientAccept::ATOM_XML); if (!$curlResult->isSuccess()) { return; @@ -926,7 +925,7 @@ class OStatus } $stored = false; - $curlResult = DI::httpClient()->get($related, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); + $curlResult = DI::httpClient()->get($related, HttpClientAccept::ATOM_XML); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return; @@ -958,7 +957,7 @@ class OStatus } } if ($atom_file != '') { - $curlResult = DI::httpClient()->get($atom_file, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); + $curlResult = DI::httpClient()->get($atom_file, HttpClientAccept::ATOM_XML); if ($curlResult->isSuccess()) { Logger::info('Fetched XML for URI ' . $related_uri); @@ -970,7 +969,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', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); + $curlResult = DI::httpClient()->get(str_replace('/notice/', '/api/statuses/show/', $related) . '.atom', HttpClientAccept::ATOM_XML); if ($curlResult->isSuccess()) { Logger::info('GNU Social workaround to fetch XML for URI ' . $related_uri); @@ -981,7 +980,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', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]); + $curlResult = DI::httpClient()->get(str_replace('/notice/', '/api/statuses/show/', $related_guess) . '.atom', HttpClientAccept::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 c24e7f78a3..cf202ba13c 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, HttpClientAccept::MAGIC_KEY); + $ret[$x] = DI::httpClient()->fetch($ret[$x], HttpClientAccept::MAGIC_KEY); Logger::debug('Fetched public key', ['url' => $ret[$x]]); } } @@ -157,7 +157,7 @@ class Salmon $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, [ + $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon), ]); @@ -182,7 +182,7 @@ class Salmon $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, [ + $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon), ]); @@ -205,7 +205,7 @@ class Salmon $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, [ + $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon)]); $return_code = $postResult->getReturnCode(); diff --git a/src/Security/ExAuth.php b/src/Security/ExAuth.php index 71eac5ec95..b0a5e25b21 100644 --- a/src/Security/ExAuth.php +++ b/src/Security/ExAuth.php @@ -58,7 +58,6 @@ use Friendica\Database\Database; use Friendica\DI; use Friendica\Model\User; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPException; use Friendica\Util\PidFile; @@ -241,7 +240,7 @@ class ExAuth $url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user; - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { return false; diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index 161b0da2d8..248e99cb4e 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -304,7 +304,7 @@ class HTTPSignature $headers['Content-Type'] = 'application/activity+json'; - $postResult = DI::httpClient()->post($target, $content, $headers); + $postResult = DI::httpClient()->post($target, $content, HttpClientAccept::DEFAULT, $headers); $return_code = $postResult->getReturnCode(); Logger::info('Transmit to ' . $target . ' returned ' . $return_code); @@ -452,9 +452,9 @@ class HTTPSignature $curl_opts[HttpClientOptions::HEADERS] = $header; if (!empty($opts['nobody'])) { - $curlResult = DI::httpClient()->head($request, $curl_opts); + $curlResult = DI::httpClient()->head($request, HttpClientAccept::JSON_AS, $curl_opts); } else { - $curlResult = DI::httpClient()->get($request, $curl_opts); + $curlResult = DI::httpClient()->get($request, HttpClientAccept::JSON_AS, $curl_opts); } $return_code = $curlResult->getReturnCode(); diff --git a/src/Util/Images.php b/src/Util/Images.php index c69b7cb9e0..c268916cc9 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -220,7 +220,7 @@ class Images } if (empty($img_str)) { - $img_str = DI::httpClient()->fetch($url, 4, HttpClientAccept::IMAGE); + $img_str = DI::httpClient()->fetch($url, HttpClientAccept::IMAGE, 4); } if (!$img_str) { diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 08d68635a9..e1adc7dbdd 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -61,11 +61,11 @@ class ParseUrl */ public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT) { - $curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => $accept]); + $curlResult = DI::httpClient()->head($url, $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, HttpClientOptions::ACCEPT_CONTENT => $accept]); + $curlResult = DI::httpClient()->get($url, $accept, [HttpClientOptions::CONTENT_LENGTH => 1000000]); } if (!$curlResult->isSuccess()) { @@ -222,7 +222,7 @@ class ParseUrl return $siteinfo; } - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000]); 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 87a9e2a0ac..f2de4674e7 100644 --- a/src/Worker/CheckVersion.php +++ b/src/Worker/CheckVersion.php @@ -55,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, 0, HttpClientAccept::TEXT))); + $gitversion = DBA::escape(trim(DI::httpClient()->fetch($checked_url, HttpClientAccept::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 95d3b3bcc8..65034a44ae 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, HttpClientAccept::HTML); + DI::httpClient()->fetch($dir . '?url=' . bin2hex($arr['url']), HttpClientAccept::HTML); } return; diff --git a/src/Worker/NodeInfo.php b/src/Worker/NodeInfo.php index 0079eb9cd6..90747b8472 100644 --- a/src/Worker/NodeInfo.php +++ b/src/Worker/NodeInfo.php @@ -35,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, 0, HttpClientAccept::HTML); + $ret = DI::httpClient()->fetch($url, HttpClientAccept::HTML); Logger::debug('Check registering answer', ['answer' => $ret]); Logger::info('end'); } diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 79369681ca..66b9851cf2 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 => HttpClientAccept::FEED_XML]); + $curlResult = DI::httpClient()->get($contact['poll'], HttpClientAccept::FEED_XML, [HttpClientOptions::COOKIEJAR => $cookiejar]); unlink($cookiejar); if ($curlResult->isTimeout()) { diff --git a/src/Worker/PubSubPublish.php b/src/Worker/PubSubPublish.php index e84ac3152e..e8b352950a 100644 --- a/src/Worker/PubSubPublish.php +++ b/src/Worker/PubSubPublish.php @@ -25,6 +25,7 @@ use Friendica\Core\Logger; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\PushSubscriber; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Protocol\OStatus; class PubSubPublish @@ -68,7 +69,7 @@ class PubSubPublish Logger::debug('POST', ['headers' => $headers, 'params' => $params]); - $postResult = DI::httpClient()->post($subscriber['callback_url'], $params, $headers); + $postResult = DI::httpClient()->post($subscriber['callback_url'], $params, HttpClientAccept::DEFAULT, $headers); $ret = $postResult->getReturnCode(); if ($ret >= 200 && $ret <= 299) { diff --git a/src/Worker/PullDirectory.php b/src/Worker/PullDirectory.php index 87558834ad..2cf0841246 100644 --- a/src/Worker/PullDirectory.php +++ b/src/Worker/PullDirectory.php @@ -48,7 +48,7 @@ class PullDirectory Logger::info('Synchronization started.', ['now' => $now, 'directory' => $directory]); - $result = DI::httpClient()->fetch($directory . '/sync/pull/since/' . $now, 0, HttpClientAccept::JSON); + $result = DI::httpClient()->fetch($directory . '/sync/pull/since/' . $now, HttpClientAccept::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 7a36d20724..b1f8e7f9d2 100644 --- a/src/Worker/SearchDirectory.php +++ b/src/Worker/SearchDirectory.php @@ -47,7 +47,7 @@ class SearchDirectory } } - $x = DI::httpClient()->fetch(Search::getGlobalDirectory() . '/lsearch?p=1&n=500&search=' . urlencode($search), 0, HttpClientAccept::JSON); + $x = DI::httpClient()->fetch(Search::getGlobalDirectory() . '/lsearch?p=1&n=500&search=' . urlencode($search), HttpClientAccept::JSON); $j = json_decode($x); if (!empty($j->results)) { diff --git a/src/Worker/UpdateServerDirectory.php b/src/Worker/UpdateServerDirectory.php index b3d9c87d2a..c5a1a078b8 100644 --- a/src/Worker/UpdateServerDirectory.php +++ b/src/Worker/UpdateServerDirectory.php @@ -45,7 +45,7 @@ class UpdateServerDirectory private static function discoverPoCo(array $gserver) { - $result = DI::httpClient()->fetch($gserver['poco'] . '?fields=urls', 0, HttpClientAccept::JSON); + $result = DI::httpClient()->fetch($gserver['poco'] . '?fields=urls', HttpClientAccept::JSON); if (empty($result)) { Logger::info('Empty result', ['url' => $gserver['url']]); return; @@ -78,7 +78,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', 0, HttpClientAccept::JSON); + $result = DI::httpClient()->fetch($gserver['url'] . '/api/v1/directory?order=new&local=true&limit=200&offset=0', HttpClientAccept::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 5a889a0ea2..98ae88a97b 100644 --- a/src/Worker/UpdateServerPeers.php +++ b/src/Worker/UpdateServerPeers.php @@ -26,7 +26,6 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\GServer; use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\Strings; class UpdateServerPeers @@ -37,7 +36,7 @@ class UpdateServerPeers */ public static function execute(string $url) { - $ret = DI::httpClient()->get($url . '/api/v1/instance/peers', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]); + $ret = DI::httpClient()->get($url . '/api/v1/instance/peers', HttpClientAccept::JSON); if (!$ret->isSuccess() || empty($ret->getBody())) { Logger::info('Server is not reachable or does not offer the "peers" endpoint', ['url' => $url]); return; From 4aeccd31579a51b317a08f79bd9c0243241d5203 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 2 Apr 2022 21:22:14 +0200 Subject: [PATCH 3/7] Added license --- .../HTTPClient/Client/HttpClientAccept.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Network/HTTPClient/Client/HttpClientAccept.php b/src/Network/HTTPClient/Client/HttpClientAccept.php index 4837ba6aed..7daa4ae13e 100644 --- a/src/Network/HTTPClient/Client/HttpClientAccept.php +++ b/src/Network/HTTPClient/Client/HttpClientAccept.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Network\HTTPClient\Client; From 04866195b4fd6c7e9ae71953f8d5fe753a533e88 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 3 Apr 2022 19:33:09 +0200 Subject: [PATCH 4/7] Remove accept parameter for head/post again --- mod/redir.php | 3 +- src/Content/Text/BBCode.php | 4 +- src/Model/Post/Media.php | 2 +- src/Model/Profile.php | 3 +- .../Capability/ICanSendHttpRequests.php | 61 +++++++++---------- src/Network/HTTPClient/Client/HttpClient.php | 18 +++--- src/Protocol/DFRN.php | 3 +- src/Protocol/Diaspora.php | 2 +- src/Protocol/Salmon.php | 6 +- src/Util/HTTPSignature.php | 4 +- src/Util/ParseUrl.php | 2 +- src/Worker/PubSubPublish.php | 3 +- 12 files changed, 54 insertions(+), 57 deletions(-) diff --git a/mod/redir.php b/mod/redir.php index 7ebce17948..d6896f3a80 100644 --- a/mod/redir.php +++ b/mod/redir.php @@ -27,7 +27,6 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Profile; -use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Util\Strings; function redir_init(App $a) { @@ -143,7 +142,7 @@ function redir_magic($a, $cid, $url) } // Test for magic auth on the target system - $serverret = DI::httpClient()->head($basepath . '/magic', HttpClientAccept::HTML); + $serverret = DI::httpClient()->head($basepath . '/magic'); if ($serverret->isSuccess()) { $separator = strpos($target_url, '?') ? '&' : '?'; $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url); diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index bb92640a1d..c9a1bfe052 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], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); + $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); if ($curlResult->isSuccess()) { $mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; } else { @@ -1275,7 +1275,7 @@ class BBCode return $text; } - $curlResult = DI::httpClient()->head($match[1], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); + $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); if ($curlResult->isSuccess()) { $mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; } else { diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index ee50061cb8..b783fb6a4f 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -169,7 +169,7 @@ 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'], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]); + $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout]); // Workaround for systems that can't handle a HEAD request if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) { diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 2af69d53ac..2b2c1ac97d 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -35,7 +35,6 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; @@ -750,7 +749,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()->head($basepath . '/magic', HttpClientAccept::HTML); + $serverret = DI::httpClient()->head($basepath . '/magic'); 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/Capability/ICanSendHttpRequests.php b/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php index ed42c75455..4c6a725f1b 100644 --- a/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php +++ b/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php @@ -59,21 +59,6 @@ interface ICanSendHttpRequests */ public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses; - /** - * Send a HEAD to a URL. - * - * @param string $url URL to fetch - * @param string $accept_content supply Accept: header with 'accept_content' as the value - * @param array $opts (optional parameters) associative array with: - * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter) - * 'timeout' => int Timeout in seconds, default system config value or 60 seconds - * 'cookiejar' => path to cookie jar file - * 'header' => header array - * - * @return ICanHandleHttpResponses - */ - public function head(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses; - /** * Send a GET to a URL. * @@ -90,14 +75,39 @@ interface ICanSendHttpRequests public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses; /** - * Sends a HTTP request to a given url + * Send a HEAD to a URL. + * + * @param string $url URL to fetch + * @param array $opts (optional parameters) associative array with: + * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter) + * 'timeout' => int Timeout in seconds, default system config value or 60 seconds + * 'cookiejar' => path to cookie jar file + * 'header' => header array + * + * @return ICanHandleHttpResponses + */ + public function head(string $url, array $opts = []): ICanHandleHttpResponses; + + /** + * Send POST request to an URL + * + * @param string $url URL to post + * @param mixed $params array of POST variables + * @param array $headers HTTP headers + * @param int $timeout The timeout in seconds, default system config value or 60 seconds + * + * @return ICanHandleHttpResponses The content + */ + public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses; + + /** + * Sends an HTTP request to a given url * * @param string $method A HTTP request * @param string $url Url to send to - * @param string $accept_content supply Accept: header with 'accept_content' as the value * @param array $opts (optional parameters) associative array with: * 'body' => (mixed) setting the body for sending data - * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter) + * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value * 'timeout' => int Timeout in seconds, default system config value or 60 seconds * 'cookiejar' => path to cookie jar file * 'header' => header array @@ -106,20 +116,7 @@ interface ICanSendHttpRequests * * @return ICanHandleHttpResponses */ - public function request(string $method, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses; - - /** - * Send POST request to an URL - * - * @param string $url URL to post - * @param mixed $params array of POST variables - * @param string $accept_content supply Accept: header with 'accept_content' as the value - * @param array $headers HTTP headers - * @param int $timeout The timeout in seconds, default system config value or 60 seconds - * - * @return ICanHandleHttpResponses The content - */ - public function post(string $url, $params, string $accept_content = HttpClientAccept::DEFAULT, array $headers = [], int $timeout = 0): ICanHandleHttpResponses; + public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses; /** * Returns the original URL of the provided URL diff --git a/src/Network/HTTPClient/Client/HttpClient.php b/src/Network/HTTPClient/Client/HttpClient.php index da3af042da..28b0f0369e 100644 --- a/src/Network/HTTPClient/Client/HttpClient.php +++ b/src/Network/HTTPClient/Client/HttpClient.php @@ -63,7 +63,7 @@ class HttpClient implements ICanSendHttpRequests /** * {@inheritDoc} */ - public function request(string $method, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses + public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses { $this->profiler->startRecording('network'); $this->logger->debug('Request start.', ['url' => $url, 'method' => $method]); @@ -141,7 +141,8 @@ class HttpClient implements ICanSendHttpRequests }; if (empty($conf[HttpClientOptions::HEADERS]['Accept'])) { - $conf[HttpClientOptions::HEADERS]['Accept'] = $accept_content; + $this->logger->info('Accept header was missing, using default.', ['url' => $url, 'callstack' => System::callstack()]); + $conf[HttpClientOptions::HEADERS]['Accept'] = HttpClientAccept::DEFAULT; } try { @@ -167,9 +168,9 @@ class HttpClient implements ICanSendHttpRequests /** {@inheritDoc} */ - public function head(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses + public function head(string $url, array $opts = []): ICanHandleHttpResponses { - return $this->request('head', $url, $accept_content, $opts); + return $this->request('head', $url, $opts); } /** @@ -177,13 +178,16 @@ class HttpClient implements ICanSendHttpRequests */ public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses { - return $this->request('get', $url, $accept_content, $opts); + // In case there is no + $opts[HttpClientOptions::ACCEPT_CONTENT] = $opts[HttpClientOptions::ACCEPT_CONTENT] ?? $accept_content; + + return $this->request('get', $url, $opts); } /** * {@inheritDoc} */ - public function post(string $url, $params, string $accept_content = HttpClientAccept::DEFAULT, array $headers = [], int $timeout = 0): ICanHandleHttpResponses + public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses { $opts = []; @@ -197,7 +201,7 @@ class HttpClient implements ICanSendHttpRequests $opts[HttpClientOptions::TIMEOUT] = $timeout; } - return $this->request('post', $url, $accept_content, $opts); + return $this->request('post', $url, $opts); } /** diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index bd90456c0c..0fb7394579 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -42,7 +42,6 @@ use Friendica\Model\Post; use Friendica\Model\Profile; use Friendica\Model\Tag; use Friendica\Model\User; -use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\Probe; use Friendica\Util\Crypto; use Friendica\Util\DateTimeFormat; @@ -1014,7 +1013,7 @@ class DFRN $content_type = ($public_batch ? "application/magic-envelope+xml" : "application/json"); - $postResult = DI::httpClient()->post($dest_url, $envelope, HttpClientAccept::DEFAULT, ['Content-Type' => $content_type]); + $postResult = DI::httpClient()->post($dest_url, $envelope, ['Content-Type' => $content_type]); $xml = $postResult->getBody(); $curl_stat = $postResult->getReturnCode(); diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 828237a5e7..71726534d0 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3018,7 +3018,7 @@ class Diaspora if (!intval(DI::config()->get("system", "diaspora_test"))) { $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json"); - $postResult = DI::httpClient()->post($dest_url . "/", $envelope, HttpClientAccept::DEFAULT, ['Content-Type' => $content_type]); + $postResult = DI::httpClient()->post($dest_url . "/", $envelope, ['Content-Type' => $content_type]); $return_code = $postResult->getReturnCode(); } else { Logger::notice("test_mode"); diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php index cf202ba13c..9724f82df7 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -157,7 +157,7 @@ class Salmon $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ + $postResult = DI::httpClient()->post($url, $salmon, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon), ]); @@ -182,7 +182,7 @@ class Salmon $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ + $postResult = DI::httpClient()->post($url, $salmon, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon), ]); @@ -205,7 +205,7 @@ class Salmon $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ + $postResult = DI::httpClient()->post($url, $salmon, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon)]); $return_code = $postResult->getReturnCode(); diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index 248e99cb4e..cc25f14918 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -304,7 +304,7 @@ class HTTPSignature $headers['Content-Type'] = 'application/activity+json'; - $postResult = DI::httpClient()->post($target, $content, HttpClientAccept::DEFAULT, $headers); + $postResult = DI::httpClient()->post($target, $content, $headers); $return_code = $postResult->getReturnCode(); Logger::info('Transmit to ' . $target . ' returned ' . $return_code); @@ -452,7 +452,7 @@ class HTTPSignature $curl_opts[HttpClientOptions::HEADERS] = $header; if (!empty($opts['nobody'])) { - $curlResult = DI::httpClient()->head($request, HttpClientAccept::JSON_AS, $curl_opts); + $curlResult = DI::httpClient()->head($request, $curl_opts); } else { $curlResult = DI::httpClient()->get($request, HttpClientAccept::JSON_AS, $curl_opts); } diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index e1adc7dbdd..f3831b52e7 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -61,7 +61,7 @@ class ParseUrl */ public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT) { - $curlResult = DI::httpClient()->head($url, $accept); + $curlResult = DI::httpClient()->head($url); // Workaround for systems that can't handle a HEAD request if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) { diff --git a/src/Worker/PubSubPublish.php b/src/Worker/PubSubPublish.php index e8b352950a..e84ac3152e 100644 --- a/src/Worker/PubSubPublish.php +++ b/src/Worker/PubSubPublish.php @@ -25,7 +25,6 @@ use Friendica\Core\Logger; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\PushSubscriber; -use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Protocol\OStatus; class PubSubPublish @@ -69,7 +68,7 @@ class PubSubPublish Logger::debug('POST', ['headers' => $headers, 'params' => $params]); - $postResult = DI::httpClient()->post($subscriber['callback_url'], $params, HttpClientAccept::DEFAULT, $headers); + $postResult = DI::httpClient()->post($subscriber['callback_url'], $params, $headers); $ret = $postResult->getReturnCode(); if ($ret >= 200 && $ret <= 299) { From 277cd5143361ebc2024cc810740d50be3a93a4cb Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 8 Apr 2022 20:11:51 +0200 Subject: [PATCH 5/7] fix doc --- src/Network/HTTPClient/Capability/ICanSendHttpRequests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php b/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php index 4c6a725f1b..fb51995b3b 100644 --- a/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php +++ b/src/Network/HTTPClient/Capability/ICanSendHttpRequests.php @@ -79,7 +79,7 @@ interface ICanSendHttpRequests * * @param string $url URL to fetch * @param array $opts (optional parameters) associative array with: - * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter) + * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value * 'timeout' => int Timeout in seconds, default system config value or 60 seconds * 'cookiejar' => path to cookie jar file * 'header' => header array From 8e2cc678dc38b3e3447baf587668f2c97b0f5478 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 8 Apr 2022 20:41:25 +0200 Subject: [PATCH 6/7] head fixings --- mod/redir.php | 4 +++- src/Model/Post/Link.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mod/redir.php b/mod/redir.php index d6896f3a80..ba1b7e13b6 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\HttpClientAccept; +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()->head($basepath . '/magic'); + $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); if ($serverret->isSuccess()) { $separator = strpos($target_url, '?') ? '&' : '?'; $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url); diff --git a/src/Model/Post/Link.php b/src/Model/Post/Link.php index cc93398a46..fb7d0edb8d 100644 --- a/src/Model/Post/Link.php +++ b/src/Model/Post/Link.php @@ -101,7 +101,7 @@ class Link { $timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->head($url, $accept, [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 3033f79529106feab29901fe53064fe5e9a75b6d Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 8 Apr 2022 20:48:57 +0200 Subject: [PATCH 7/7] head fixings --- src/Model/Profile.php | 4 +++- src/Util/ParseUrl.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 2b2c1ac97d..5fbfc4ba5a 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\HttpClientAccept; +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()->head($basepath . '/magic'); + $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]); if ($serverret->isSuccess()) { Logger::info('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path); System::externalRedirect($magic_path); diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index f3831b52e7..1cc35fadcc 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -61,7 +61,7 @@ class ParseUrl */ public static function getContentType(string $url, string $accept = HttpClientAccept::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)) {