Skip to content

Commit

Permalink
Introduce "accept_header" as specific argument to the http client
Browse files Browse the repository at this point in the history
  • Loading branch information
nupplaphil committed Apr 2, 2022
1 parent 73c3b21 commit e299fc6
Show file tree
Hide file tree
Showing 35 changed files with 150 additions and 150 deletions.
3 changes: 1 addition & 2 deletions mod/ostatus_subscribe.php
Expand Up @@ -25,7 +25,6 @@
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)
Expand Down Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions mod/redir.php
Expand Up @@ -28,7 +28,6 @@
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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Content/OEmbed.php
Expand Up @@ -98,7 +98,7 @@ public static function fetchURL($embedurl, bool $no_rich_type = false, bool $use

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)) {
Expand Down
10 changes: 5 additions & 5 deletions src/Content/Text/BBCode.php
Expand Up @@ -502,7 +502,7 @@ public static function scaleExternalImages(string $srctext)
continue;
}

$curlResult = DI::httpClient()->get($mtch[1], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]);
$curlResult = DI::httpClient()->get($mtch[1], HttpClientAccept::IMAGE);
if (!$curlResult->isSuccess()) {
continue;
}
Expand Down Expand Up @@ -1204,7 +1204,7 @@ private static function removePictureLinksCallback($match)
$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 {
Expand All @@ -1217,7 +1217,7 @@ private static function removePictureLinksCallback($match)
$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;
Expand Down Expand Up @@ -1275,7 +1275,7 @@ private static function cleanPictureLinksCallback($match)
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 {
Expand All @@ -1293,7 +1293,7 @@ private static function cleanPictureLinksCallback($match)
}

// 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;
Expand Down
5 changes: 2 additions & 3 deletions src/Core/Search.php
Expand Up @@ -25,7 +25,6 @@
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;
Expand Down Expand Up @@ -123,7 +122,7 @@ public static function getContactsFromGlobalDirectory($search, $type = self::TYP
$searchUrl .= '&page=' . $page;
}

$resultJson = DI::httpClient()->fetch($searchUrl, 0, HttpClientAccept::JSON);
$resultJson = DI::httpClient()->fetch($searchUrl, HttpClientAccept::JSON);

$results = json_decode($resultJson, true);

Expand Down Expand Up @@ -229,7 +228,7 @@ public static function searchContact($search, $mode, int $page = 1)
$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'])) {
Expand Down
48 changes: 24 additions & 24 deletions src/Model/GServer.php
Expand Up @@ -344,15 +344,15 @@ public static function detect(string $url, string $network = '', bool $only_node

// 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;
}

// 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);
Expand Down Expand Up @@ -394,7 +394,7 @@ public static function detect(string $url, string $network = '', bool $only_node
$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()]);
Expand All @@ -418,7 +418,7 @@ public static function detect(string $url, string $network = '', bool $only_node
// 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);
Expand Down Expand Up @@ -588,7 +588,7 @@ private static function discoverRelay(string $server_url)
{
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;
}
Expand Down Expand Up @@ -683,7 +683,7 @@ private static function discoverRelay(string $server_url)
*/
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 [];
}
Expand Down Expand Up @@ -809,7 +809,7 @@ private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $http
*/
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 [];
}
Expand Down Expand Up @@ -902,7 +902,7 @@ private static function parseNodeinfo1(string $nodeinfo_url)
*/
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 [];
}
Expand Down Expand Up @@ -997,7 +997,7 @@ private static function parseNodeinfo2(string $nodeinfo_url)
*/
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;
}
Expand Down Expand Up @@ -1082,7 +1082,7 @@ private static function fetchSiteinfo(string $url, array $serverdata)
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;
}
Expand Down Expand Up @@ -1174,7 +1174,7 @@ private static function checkPoCo(string $url, array $serverdata)
{
$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;
}
Expand Down Expand Up @@ -1204,7 +1204,7 @@ private static function checkPoCo(string $url, array $serverdata)
*/
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;
}
Expand All @@ -1231,7 +1231,7 @@ public static function checkMastodonDirectory(string $url, array $serverdata)
*/
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;
}
Expand Down Expand Up @@ -1278,7 +1278,7 @@ private static function detectPeertube(string $url, array $serverdata)
*/
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;
}
Expand All @@ -1302,7 +1302,7 @@ private static function detectNextcloud(string $url, array $serverdata)
}

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;
}
Expand Down Expand Up @@ -1370,7 +1370,7 @@ private static function detectFromContacts(string $url, array $serverdata)
*/
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;
}
Expand Down Expand Up @@ -1435,7 +1435,7 @@ private static function detectMastodonAlikes(string $url, array $serverdata)
*/
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;
}
Expand Down Expand Up @@ -1532,7 +1532,7 @@ private static function toBoolean($val)
*/
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;
}
Expand Down Expand Up @@ -1583,7 +1583,7 @@ private static function detectPumpIO(string $url, array $serverdata)
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';
Expand All @@ -1601,7 +1601,7 @@ private static function detectGNUSocial(string $url, array $serverdata)
}

// 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)) {

Expand Down Expand Up @@ -1639,9 +1639,9 @@ private static function detectFriendica(string $url, array $serverdata)
{
// 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 {
Expand Down Expand Up @@ -1946,7 +1946,7 @@ private static function discoverFederation()
$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'])) {
Expand All @@ -1963,7 +1963,7 @@ private static function discoverFederation()

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);

Expand Down
5 changes: 2 additions & 3 deletions src/Model/Photo.php
Expand Up @@ -33,7 +33,6 @@
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;
Expand Down Expand Up @@ -499,7 +498,7 @@ public static function importProfilePhoto($image_url, $uid, $cid, $quit_on_error

$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();
Expand Down Expand Up @@ -915,7 +914,7 @@ private static function loadImageFromURL(string $image_url)
{
$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();
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Post/Link.php
Expand Up @@ -101,7 +101,7 @@ private static function fetchMimeType(string $url, string $accept = HttpClientAc
{
$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] ?? '';
Expand Down

0 comments on commit e299fc6

Please sign in to comment.