Restructure HTTPClient for new paradigm

This commit is contained in:
Philipp 2021-10-23 12:50:31 +02:00
parent fa55928ea3
commit 409d909d0f
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
25 changed files with 210 additions and 198 deletions

View file

@ -39,7 +39,7 @@ use Friendica\Model\Event;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Util\Images; use Friendica\Util\Images;
@ -1201,7 +1201,7 @@ class BBCode
$text = DI::cache()->get($cache_key); $text = DI::cache()->get($cache_key);
if (is_null($text)) { if (is_null($text)) {
$curlResult = DI::httpClient()->head($match[1], [HTTPClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; $mimetype = $curlResult->getHeader('Content-Type')[0] ?? '';
} else { } else {
@ -1272,7 +1272,7 @@ class BBCode
return $text; return $text;
} }
$curlResult = DI::httpClient()->head($match[1], [HTTPClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]); $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$mimetype = $curlResult->getHeader('Content-Type')[0] ?? ''; $mimetype = $curlResult->getHeader('Content-Type')[0] ?? '';
} else { } else {

View file

@ -24,7 +24,7 @@ namespace Friendica\Core;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Object\Search\ContactResult; use Friendica\Object\Search\ContactResult;
use Friendica\Object\Search\ResultList; use Friendica\Object\Search\ResultList;
use Friendica\Util\Network; use Friendica\Util\Network;
@ -228,7 +228,7 @@ class Search
$return = Contact::searchByName($search, $mode); $return = Contact::searchByName($search, $mode);
} else { } else {
$p = $page > 1 ? 'p=' . $page : ''; $p = $page > 1 ? 'p=' . $page : '';
$curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), [HTTPClientOptions::ACCEPT_CONTENT => ['application/json']]); $curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), [HttpClientOptions::ACCEPT_CONTENT => ['application/json']]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$searchResult = json_decode($curlResult->getBody(), true); $searchResult = json_decode($curlResult->getBody(), true);
if (!empty($searchResult['profiles'])) { if (!empty($searchResult['profiles'])) {

View file

@ -415,11 +415,11 @@ abstract class DI
// //
/** /**
* @return Network\IHTTPClient * @return Network\HTTPClient\Capability\ICanRequestPerHttp
*/ */
public static function httpClient() public static function httpClient()
{ {
return self::$dice->create(Network\IHTTPClient::class); return self::$dice->create(Network\HTTPClient\Capability\ICanRequestPerHttp::class);
} }
// //

View file

@ -32,8 +32,8 @@ use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Module\Register; use Friendica\Module\Register;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Network\IHTTPResult; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Protocol\Relay; use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network; use Friendica\Util\Network;
@ -315,7 +315,7 @@ class GServer
// When a nodeinfo is present, we don't need to dig further // When a nodeinfo is present, we don't need to dig further
$xrd_timeout = DI::config()->get('system', 'xrd_timeout'); $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
$curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HTTPClientOptions::TIMEOUT => $xrd_timeout]); $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::TIMEOUT => $xrd_timeout]);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::setFailure($url); self::setFailure($url);
return false; return false;
@ -323,7 +323,7 @@ class GServer
// On a redirect follow the new host but mark the old one as failure // On a redirect follow the new host but mark the old one as failure
if ($curlResult->isSuccess() && (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) { if ($curlResult->isSuccess() && (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) {
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout]); $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
if (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST)) { if (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()]); Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]);
self::setFailure($url); self::setFailure($url);
@ -359,7 +359,7 @@ class GServer
$basedata = ['detection-method' => self::DETECT_MANUAL]; $basedata = ['detection-method' => self::DETECT_MANUAL];
} }
$curlResult = DI::httpClient()->get($baseurl, [HTTPClientOptions::TIMEOUT => $xrd_timeout]); $curlResult = DI::httpClient()->get($baseurl, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
if ((parse_url($baseurl, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) { if ((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()]); Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]);
@ -383,7 +383,7 @@ class GServer
// When the base path doesn't seem to contain a social network we try the complete path. // 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. // Most detectable system have to be installed in the root directory.
// We checked the base to avoid false positives. // We checked the base to avoid false positives.
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout]); $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$urldata = self::analyseRootHeader($curlResult, $serverdata); $urldata = self::analyseRootHeader($curlResult, $serverdata);
$urldata = self::analyseRootBody($curlResult, $urldata, $url); $urldata = self::analyseRootBody($curlResult, $urldata, $url);
@ -672,13 +672,13 @@ class GServer
/** /**
* Detect server type by using the nodeinfo data * Detect server type by using the nodeinfo data
* *
* @param string $url address of the server * @param string $url address of the server
* @param IHTTPResult $httpResult * @param ICanHandleHttpResponses $httpResult
* *
* @return array Server data * @return array Server data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
private static function fetchNodeinfo(string $url, IHTTPResult $httpResult) private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult)
{ {
if (!$httpResult->isSuccess()) { if (!$httpResult->isSuccess()) {
return []; return [];
@ -959,7 +959,7 @@ class GServer
private static function validHostMeta(string $url) private static function validHostMeta(string $url)
{ {
$xrd_timeout = DI::config()->get('system', 'xrd_timeout'); $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
$curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HTTPClientOptions::TIMEOUT => $xrd_timeout]); $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout]);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
return false; return false;
} }
@ -1725,7 +1725,7 @@ class GServer
if (!empty($accesstoken)) { if (!empty($accesstoken)) {
$api = 'https://instances.social/api/1.0/instances/list?count=0'; $api = 'https://instances.social/api/1.0/instances/list?count=0';
$curlResult = DI::httpClient()->get($api, [HTTPClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]); $curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$servers = json_decode($curlResult->getBody(), true); $servers = json_decode($curlResult->getBody(), true);

View file

@ -26,7 +26,7 @@ use Friendica\Core\System;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Util\Proxy; use Friendica\Util\Proxy;
/** /**
@ -100,7 +100,7 @@ class Link
{ {
$timeout = DI::config()->get('system', 'xrd_timeout'); $timeout = DI::config()->get('system', 'xrd_timeout');
$curlResult = DI::httpClient()->head($url, [HTTPClientOptions::TIMEOUT => $timeout]); $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
if (empty($media['mimetype'])) { if (empty($media['mimetype'])) {
return $curlResult->getHeader('Content-Type')[0] ?? ''; return $curlResult->getHeader('Content-Type')[0] ?? '';

View file

@ -30,7 +30,7 @@ use Friendica\DI;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Util\Images; use Friendica\Util\Images;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\ParseUrl; use Friendica\Util\ParseUrl;
@ -168,7 +168,7 @@ class Media
// Fetch the mimetype or size if missing. // Fetch the mimetype or size if missing.
if (empty($media['mimetype']) || empty($media['size'])) { if (empty($media['mimetype']) || empty($media['size'])) {
$timeout = DI::config()->get('system', 'xrd_timeout'); $timeout = DI::config()->get('system', 'xrd_timeout');
$curlResult = DI::httpClient()->head($media['url'], [HTTPClientOptions::TIMEOUT => $timeout]); $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
if (empty($media['mimetype'])) { if (empty($media['mimetype'])) {
$media['mimetype'] = $curlResult->getHeader('Content-Type')[0] ?? ''; $media['mimetype'] = $curlResult->getHeader('Content-Type')[0] ?? '';

View file

@ -28,7 +28,7 @@ use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Util\HTTPSignature; use Friendica\Util\HTTPSignature;
use Friendica\Util\Strings; use Friendica\Util\Strings;
@ -102,7 +102,7 @@ class Magic extends BaseModule
); );
// Try to get an authentication token from the other instance. // Try to get an authentication token from the other instance.
$curlResult = DI::httpClient()->get($basepath . '/owa', [HTTPClientOptions::HEADERS => $header]); $curlResult = DI::httpClient()->get($basepath . '/owa', [HttpClientOptions::HEADERS => $header]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$j = json_decode($curlResult->getBody(), true); $j = json_decode($curlResult->getBody(), true);

View file

@ -1,13 +1,13 @@
<?php <?php
namespace Friendica\Network; namespace Friendica\Network\HTTPClient\Capability;
use Psr\Http\Message\MessageInterface; use Psr\Http\Message\MessageInterface;
/** /**
* Temporary class to map Friendica used variables based on PSR-7 HTTPResponse * Temporary class to map Friendica used variables based on PSR-7 HTTPResponse
*/ */
interface IHTTPResult interface ICanHandleHttpResponses
{ {
/** /**
* Gets the Return Code * Gets the Return Code
@ -25,13 +25,14 @@ interface IHTTPResult
/** /**
* Returns the headers * Returns the headers
* @see MessageInterface::getHeader()
* *
* @param string $header optional header field. Return all fields if empty * @param string $header optional header field. Return all fields if empty
* *
* @return string[] the headers or the specified content of the header variable * @return string[] the headers or the specified content of the header variable
*@see MessageInterface::getHeader()
*
*/ */
public function getHeader($header); public function getHeader(string $header);
/** /**
* Returns all headers * Returns all headers

View file

@ -19,14 +19,14 @@
* *
*/ */
namespace Friendica\Network; namespace Friendica\Network\HTTPClient\Capability;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
/** /**
* Interface for calling HTTP requests and returning their responses * Interface for calling HTTP requests and returning their responses
*/ */
interface IHTTPClient interface ICanRequestPerHttp
{ {
/** /**
* Fetches the content of an URL * Fetches the content of an URL
@ -41,7 +41,7 @@ interface IHTTPClient
* *
* @return string The fetched content * @return string The fetched content
*/ */
public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''); public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string;
/** /**
* Fetches the whole response of an URL. * Fetches the whole response of an URL.
@ -54,12 +54,12 @@ interface IHTTPClient
* @param string $accept_content supply Accept: header with 'accept_content' as the value * @param string $accept_content supply Accept: header with 'accept_content' as the value
* @param string $cookiejar Path to cookie jar file * @param string $cookiejar Path to cookie jar file
* *
* @return IHTTPResult With all relevant information, 'body' contains the actual fetched content. * @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 = ''); public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses;
/** /**
* Send a HEAD to an URL. * Send a HEAD to a URL.
* *
* @param string $url URL to fetch * @param string $url URL to fetch
* @param array $opts (optional parameters) associative array with: * @param array $opts (optional parameters) associative array with:
@ -68,9 +68,9 @@ interface IHTTPClient
* 'cookiejar' => path to cookie jar file * 'cookiejar' => path to cookie jar file
* 'header' => header array * 'header' => header array
* *
* @return CurlResult * @return ICanHandleHttpResponses
*/ */
public function head(string $url, array $opts = []); public function head(string $url, array $opts = []): ICanHandleHttpResponses;
/** /**
* Send a GET to an URL. * Send a GET to an URL.
@ -83,9 +83,9 @@ interface IHTTPClient
* 'header' => header array * 'header' => header array
* 'content_length' => int maximum File content length * 'content_length' => int maximum File content length
* *
* @return IHTTPResult * @return ICanHandleHttpResponses
*/ */
public function get(string $url, array $opts = []); public function get(string $url, array $opts = []): ICanHandleHttpResponses;
/** /**
* Sends a HTTP request to a given url * Sends a HTTP request to a given url
@ -101,9 +101,9 @@ interface IHTTPClient
* 'content_length' => int maximum File content length * 'content_length' => int maximum File content length
* 'auth' => array authentication settings * 'auth' => array authentication settings
* *
* @return IHTTPResult * @return ICanHandleHttpResponses
*/ */
public function request(string $method, string $url, array $opts = []); public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses;
/** /**
* Send POST request to an URL * Send POST request to an URL
@ -113,9 +113,9 @@ interface IHTTPClient
* @param array $headers HTTP headers * @param array $headers HTTP headers
* @param int $timeout The timeout in seconds, default system config value or 60 seconds * @param int $timeout The timeout in seconds, default system config value or 60 seconds
* *
* @return IHTTPResult The content * @return ICanHandleHttpResponses The content
*/ */
public function post(string $url, $params, array $headers = [], int $timeout = 0); public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses;
/** /**
* Returns the original URL of the provided URL * Returns the original URL of the provided URL
@ -129,5 +129,5 @@ interface IHTTPClient
* *
* @throws TransferException In case there's an error during the resolving * @throws TransferException In case there's an error during the resolving
*/ */
public function finalUrl(string $url); public function finalUrl(string $url): string;
} }

View file

@ -19,9 +19,14 @@
* *
*/ */
namespace Friendica\Network; namespace Friendica\Network\HTTPClient\Client;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Network\HTTPClient\Response\CurlResult;
use Friendica\Network\HTTPClient\Response\GuzzleResponse;
use Friendica\Network\HTTPClient\Capability\ICanRequestPerHttp;
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use GuzzleHttp\Client; use GuzzleHttp\Client;
@ -37,7 +42,7 @@ use Psr\Log\LoggerInterface;
/** /**
* Performs HTTP requests to a given URL * Performs HTTP requests to a given URL
*/ */
class HTTPClient implements IHTTPClient class HttpClientCan implements ICanRequestPerHttp
{ {
/** @var LoggerInterface */ /** @var LoggerInterface */
private $logger; private $logger;
@ -59,7 +64,7 @@ class HTTPClient implements IHTTPClient
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function request(string $method, string $url, array $opts = []): IHTTPResult public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses
{ {
$this->profiler->startRecording('network'); $this->profiler->startRecording('network');
$this->logger->debug('Request start.', ['url' => $url, 'method' => $method]); $this->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
@ -95,43 +100,43 @@ class HTTPClient implements IHTTPClient
$conf = []; $conf = [];
if (!empty($opts[HTTPClientOptions::COOKIEJAR])) { if (!empty($opts[HttpClientOptions::COOKIEJAR])) {
$jar = new FileCookieJar($opts[HTTPClientOptions::COOKIEJAR]); $jar = new FileCookieJar($opts[HttpClientOptions::COOKIEJAR]);
$conf[RequestOptions::COOKIES] = $jar; $conf[RequestOptions::COOKIES] = $jar;
} }
$headers = []; $headers = [];
if (!empty($opts[HTTPClientOptions::ACCEPT_CONTENT])) { if (!empty($opts[HttpClientOptions::ACCEPT_CONTENT])) {
$headers['Accept'] = $opts[HTTPClientOptions::ACCEPT_CONTENT]; $headers['Accept'] = $opts[HttpClientOptions::ACCEPT_CONTENT];
} }
if (!empty($opts[HTTPClientOptions::LEGACY_HEADER])) { if (!empty($opts[HttpClientOptions::LEGACY_HEADER])) {
$this->logger->notice('Wrong option \'headers\' used.'); $this->logger->notice('Wrong option \'headers\' used.');
$headers = array_merge($opts[HTTPClientOptions::LEGACY_HEADER], $headers); $headers = array_merge($opts[HttpClientOptions::LEGACY_HEADER], $headers);
} }
if (!empty($opts[HTTPClientOptions::HEADERS])) { if (!empty($opts[HttpClientOptions::HEADERS])) {
$headers = array_merge($opts[HTTPClientOptions::HEADERS], $headers); $headers = array_merge($opts[HttpClientOptions::HEADERS], $headers);
} }
$conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers); $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers);
if (!empty($opts[HTTPClientOptions::TIMEOUT])) { if (!empty($opts[HttpClientOptions::TIMEOUT])) {
$conf[RequestOptions::TIMEOUT] = $opts[HTTPClientOptions::TIMEOUT]; $conf[RequestOptions::TIMEOUT] = $opts[HttpClientOptions::TIMEOUT];
} }
if (!empty($opts[HTTPClientOptions::BODY])) { if (!empty($opts[HttpClientOptions::BODY])) {
$conf[RequestOptions::BODY] = $opts[HTTPClientOptions::BODY]; $conf[RequestOptions::BODY] = $opts[HttpClientOptions::BODY];
} }
if (!empty($opts[HTTPClientOptions::AUTH])) { if (!empty($opts[HttpClientOptions::AUTH])) {
$conf[RequestOptions::AUTH] = $opts[HTTPClientOptions::AUTH]; $conf[RequestOptions::AUTH] = $opts[HttpClientOptions::AUTH];
} }
$conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) { $conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
if (!empty($opts[HTTPClientOptions::CONTENT_LENGTH]) && if (!empty($opts[HttpClientOptions::CONTENT_LENGTH]) &&
(int)$response->getHeaderLine('Content-Length') > $opts[HTTPClientOptions::CONTENT_LENGTH]) { (int)$response->getHeaderLine('Content-Length') > $opts[HttpClientOptions::CONTENT_LENGTH]) {
throw new TransferException('The file is too big!'); throw new TransferException('The file is too big!');
} }
}; };
@ -159,7 +164,7 @@ class HTTPClient implements IHTTPClient
/** {@inheritDoc} /** {@inheritDoc}
*/ */
public function head(string $url, array $opts = []): IHTTPResult public function head(string $url, array $opts = []): ICanHandleHttpResponses
{ {
return $this->request('head', $url, $opts); return $this->request('head', $url, $opts);
} }
@ -167,7 +172,7 @@ class HTTPClient implements IHTTPClient
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function get(string $url, array $opts = []): IHTTPResult public function get(string $url, array $opts = []): ICanHandleHttpResponses
{ {
return $this->request('get', $url, $opts); return $this->request('get', $url, $opts);
} }
@ -175,18 +180,18 @@ class HTTPClient implements IHTTPClient
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function post(string $url, $params, array $headers = [], int $timeout = 0): IHTTPResult public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses
{ {
$opts = []; $opts = [];
$opts[HTTPClientOptions::BODY] = $params; $opts[HttpClientOptions::BODY] = $params;
if (!empty($headers)) { if (!empty($headers)) {
$opts[HTTPClientOptions::HEADERS] = $headers; $opts[HttpClientOptions::HEADERS] = $headers;
} }
if (!empty($timeout)) { if (!empty($timeout)) {
$opts[HTTPClientOptions::TIMEOUT] = $timeout; $opts[HttpClientOptions::TIMEOUT] = $timeout;
} }
return $this->request('post', $url, $opts); return $this->request('post', $url, $opts);
@ -195,7 +200,7 @@ class HTTPClient implements IHTTPClient
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function finalUrl(string $url) public function finalUrl(string $url): string
{ {
$this->profiler->startRecording('network'); $this->profiler->startRecording('network');
@ -229,7 +234,7 @@ class HTTPClient implements IHTTPClient
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '') public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string
{ {
$ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar); $ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar);
@ -239,7 +244,7 @@ class HTTPClient implements IHTTPClient
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '') public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses
{ {
return $this->get( return $this->get(
$url, $url,

View file

@ -1,13 +1,13 @@
<?php <?php
namespace Friendica\Network; namespace Friendica\Network\HTTPClient\Client;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
/** /**
* This class contains a list of possible HTTPClient request options. * This class contains a list of possible HTTPClient request options.
*/ */
class HTTPClientOptions class HttpClientOptions
{ {
/** /**
* accept_content: (array) supply Accept: header with 'accept_content' as the value * accept_content: (array) supply Accept: header with 'accept_content' as the value

View file

@ -1,15 +1,15 @@
<?php <?php
namespace Friendica\Factory; namespace Friendica\Network\HTTPClient\Factory;
use Friendica\App; use Friendica\App;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Network\HTTPClient; use Friendica\Network\HTTPClient\Client;
use Friendica\Network\IHTTPClient; use Friendica\Network\HTTPClient\Capability\ICanRequestPerHttp;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Friendica\Util\Strings; use Friendica\Util\Strings;
use GuzzleHttp\Client; use GuzzleHttp;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
use mattwright\URLResolver; use mattwright\URLResolver;
@ -18,9 +18,9 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
require_once __DIR__ . '/../../static/dbstructure.config.php'; require_once __DIR__ . '/../../../../static/dbstructure.config.php';
class HTTPClientFactory extends BaseFactory class HttpClient extends BaseFactory
{ {
/** @var IManageConfigValues */ /** @var IManageConfigValues */
private $config; private $config;
@ -42,17 +42,17 @@ class HTTPClientFactory extends BaseFactory
* *
* @param HandlerStack|null $handlerStack (optional) A handler replacement (just usefull at test environments) * @param HandlerStack|null $handlerStack (optional) A handler replacement (just usefull at test environments)
* *
* @return IHTTPClient * @return ICanRequestPerHttp
*/ */
public function createClient(HandlerStack $handlerStack = null): IHTTPClient public function createClient(HandlerStack $handlerStack = null): ICanRequestPerHttp
{ {
$proxy = $this->config->get('system', 'proxy'); $proxy = $this->config->get('system', 'proxy');
if (!empty($proxy)) { if (!empty($proxy)) {
$proxyuser = $this->config->get('system', 'proxyuser'); $proxyUser = $this->config->get('system', 'proxyuser');
if (!empty($proxyuser)) { if (!empty($proxyUser)) {
$proxy = $proxyuser . '@' . $proxy; $proxy = $proxyUser . '@' . $proxy;
} }
} }
@ -72,7 +72,7 @@ class HTTPClientFactory extends BaseFactory
DB_UPDATE_VERSION . '; ' . DB_UPDATE_VERSION . '; ' .
$this->baseUrl->get(); $this->baseUrl->get();
$guzzle = new Client([ $guzzle = new GuzzleHttp\Client([
RequestOptions::ALLOW_REDIRECTS => [ RequestOptions::ALLOW_REDIRECTS => [
'max' => 8, 'max' => 8,
'on_redirect' => $onRedirect, 'on_redirect' => $onRedirect,
@ -88,7 +88,7 @@ class HTTPClientFactory extends BaseFactory
RequestOptions::FORCE_IP_RESOLVE => ($this->config->get('system', 'ipv4_resolve') ? 'v4' : null), RequestOptions::FORCE_IP_RESOLVE => ($this->config->get('system', 'ipv4_resolve') ? 'v4' : null),
RequestOptions::CONNECT_TIMEOUT => 10, RequestOptions::CONNECT_TIMEOUT => 10,
RequestOptions::TIMEOUT => $this->config->get('system', 'curl_timeout', 60), RequestOptions::TIMEOUT => $this->config->get('system', 'curl_timeout', 60),
// by default we will allow self-signed certs // by default, we will allow self-signed certs,
// but it can be overridden // but it can be overridden
RequestOptions::VERIFY => (bool)$this->config->get('system', 'verifyssl'), RequestOptions::VERIFY => (bool)$this->config->get('system', 'verifyssl'),
RequestOptions::PROXY => $proxy, RequestOptions::PROXY => $proxy,
@ -108,6 +108,6 @@ class HTTPClientFactory extends BaseFactory
// Some websites test the browser for cookie support, so this enhances results. // Some websites test the browser for cookie support, so this enhances results.
$resolver->setCookieJar(get_temppath() .'/resolver-cookie-' . Strings::getRandomName(10)); $resolver->setCookieJar(get_temppath() .'/resolver-cookie-' . Strings::getRandomName(10));
return new HTTPClient($logger, $this->profiler, $guzzle, $resolver); return new Client\HttpClientCan($logger, $this->profiler, $guzzle, $resolver);
} }
} }

View file

@ -19,17 +19,17 @@
* *
*/ */
namespace Friendica\Network; namespace Friendica\Network\HTTPClient\Response;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\Network; use Friendica\Util\Network;
/** /**
* A content class for Curl call results * A content class for Curl call results
*/ */
class CurlResult implements IHTTPResult class CurlResult implements ICanHandleHttpResponses
{ {
/** /**
* @var int HTTP return code or 0 if timeout or failure * @var int HTTP return code or 0 if timeout or failure
@ -101,35 +101,36 @@ class CurlResult implements IHTTPResult
* *
* @param string $url optional URL * @param string $url optional URL
* *
* @return IHTTPResult a CURL with error response * @return ICanHandleHttpResponses a CURL with error response
* @throws InternalServerErrorException * @throws InternalServerErrorException
*/ */
public static function createErrorCurl($url = '') public static function createErrorCurl(string $url = '')
{ {
return new CurlResult($url, '', ['http_code' => 0]); return new CurlResult($url, '', ['http_code' => 0]);
} }
/** /**
* Curl constructor. * Curl constructor.
* @param string $url the URL which was called *
* @param string $result the result of the curl execution * @param string $url the URL which was called
* @param array $info an additional info array * @param string $result the result of the curl execution
* @param int $errorNumber the error number or 0 (zero) if no error * @param array $info an additional info array
* @param string $error the error message or '' (the empty string) if no * @param int $errorNumber the error number or 0 (zero) if no error
* @param string $error the error message or '' (the empty string) if no
* *
* @throws InternalServerErrorException when HTTP code of the CURL response is missing * @throws InternalServerErrorException when HTTP code of the CURL response is missing
*/ */
public function __construct($url, $result, $info, $errorNumber = 0, $error = '') public function __construct(string $url, string $result, array $info, int $errorNumber = 0, string $error = '')
{ {
if (!array_key_exists('http_code', $info)) { if (!array_key_exists('http_code', $info)) {
throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code'); throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
} }
$this->returnCode = $info['http_code']; $this->returnCode = $info['http_code'];
$this->url = $url; $this->url = $url;
$this->info = $info; $this->info = $info;
$this->errorNumber = $errorNumber; $this->errorNumber = $errorNumber;
$this->error = $error; $this->error = $error;
Logger::debug('construct', ['url' => $url, 'returncode' => $this->returnCode, 'result' => $result]); Logger::debug('construct', ['url' => $url, 'returncode' => $this->returnCode, 'result' => $result]);
@ -145,15 +146,15 @@ class CurlResult implements IHTTPResult
// allow for HTTP/2.x without fixing code // allow for HTTP/2.x without fixing code
$header = ''; $header = '';
$base = $result; $base = $result;
while (preg_match('/^HTTP\/.+? \d+/', $base)) { while (preg_match('/^HTTP\/.+? \d+/', $base)) {
$chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4); $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
$header .= $chunk; $header .= $chunk;
$base = substr($base, strlen($chunk)); $base = substr($base, strlen($chunk));
} }
$this->body = substr($result, strlen($header)); $this->body = substr($result, strlen($header));
$this->header = $header; $this->header = $header;
$this->header_fields = []; // Is filled on demand $this->header_fields = []; // Is filled on demand
} }
@ -185,7 +186,7 @@ class CurlResult implements IHTTPResult
$this->redirectUrl = $this->info['url']; $this->redirectUrl = $this->info['url'];
} }
if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) { if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode == 307) {
$redirect_parts = parse_url($this->info['redirect_url'] ?? ''); $redirect_parts = parse_url($this->info['redirect_url'] ?? '');
if (empty($redirect_parts)) { if (empty($redirect_parts)) {
$redirect_parts = []; $redirect_parts = [];
@ -229,19 +230,19 @@ class CurlResult implements IHTTPResult
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getReturnCode() public function getReturnCode(): string
{ {
return $this->returnCode; return $this->returnCode;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getContentType() public function getContentType(): string
{ {
return $this->contentType; return $this->contentType;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getHeader($header) public function getHeader(string $header): array
{ {
if (empty($header)) { if (empty($header)) {
return []; return [];
@ -259,13 +260,13 @@ class CurlResult implements IHTTPResult
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getHeaders() public function getHeaders(): array
{ {
return $this->getHeaderArray(); return $this->getHeaderArray();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function inHeader(string $field) public function inHeader(string $field): bool
{ {
$field = strtolower(trim($field)); $field = strtolower(trim($field));
@ -275,7 +276,7 @@ class CurlResult implements IHTTPResult
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getHeaderArray() public function getHeaderArray(): array
{ {
if (!empty($this->header_fields)) { if (!empty($this->header_fields)) {
return $this->header_fields; return $this->header_fields;
@ -285,9 +286,9 @@ class CurlResult implements IHTTPResult
$lines = explode("\n", trim($this->header)); $lines = explode("\n", trim($this->header));
foreach ($lines as $line) { foreach ($lines as $line) {
$parts = explode(':', $line); $parts = explode(':', $line);
$headerfield = strtolower(trim(array_shift($parts))); $headerfield = strtolower(trim(array_shift($parts)));
$headerdata = trim(implode(':', $parts)); $headerdata = trim(implode(':', $parts));
if (empty($this->header_fields[$headerfield])) { if (empty($this->header_fields[$headerfield])) {
$this->header_fields[$headerfield] = [$headerdata]; $this->header_fields[$headerfield] = [$headerdata];
} elseif (!in_array($headerdata, $this->header_fields[$headerfield])) { } elseif (!in_array($headerdata, $this->header_fields[$headerfield])) {
@ -299,49 +300,49 @@ class CurlResult implements IHTTPResult
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function isSuccess() public function isSuccess(): bool
{ {
return $this->isSuccess; return $this->isSuccess;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getUrl() public function getUrl(): string
{ {
return $this->url; return $this->url;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getRedirectUrl() public function getRedirectUrl(): string
{ {
return $this->redirectUrl; return $this->redirectUrl;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getBody() public function getBody(): string
{ {
return $this->body; return $this->body;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function isRedirectUrl() public function isRedirectUrl(): bool
{ {
return $this->isRedirectUrl; return $this->isRedirectUrl;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getErrorNumber() public function getErrorNumber(): int
{ {
return $this->errorNumber; return $this->errorNumber;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getError() public function getError(): string
{ {
return $this->error; return $this->error;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function isTimeout() public function isTimeout(): bool
{ {
return $this->isTimeout; return $this->isTimeout;
} }

View file

@ -19,10 +19,10 @@
* *
*/ */
namespace Friendica\Network; namespace Friendica\Network\HTTPClient\Response;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Network\HTTPException\NotImplementedException; use Friendica\Network\HTTPException\NotImplementedException;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@ -30,7 +30,7 @@ use Psr\Http\Message\ResponseInterface;
/** /**
* A content wrapper class for Guzzle call results * A content wrapper class for Guzzle call results
*/ */
class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface class GuzzleResponse extends Response implements ICanHandleHttpResponses, ResponseInterface
{ {
/** @var string The URL */ /** @var string The URL */
private $url; private $url;
@ -79,68 +79,72 @@ class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getReturnCode() public function getReturnCode(): string
{ {
return $this->getStatusCode(); return $this->getStatusCode();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getContentType() public function getContentType(): string
{ {
$contentTypes = $this->getHeader('Content-Type') ?? []; $contentTypes = $this->getHeader('Content-Type') ?? [];
return array_pop($contentTypes) ?? ''; return array_pop($contentTypes) ?? '';
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function inHeader(string $field) public function inHeader(string $field): bool
{ {
return $this->hasHeader($field); return $this->hasHeader($field);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getHeaderArray() public function getHeaderArray(): array
{ {
return $this->getHeaders(); return $this->getHeaders();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function isSuccess() public function isSuccess(): bool
{ {
return $this->isSuccess; return $this->isSuccess;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getUrl() public function getUrl(): string
{ {
return $this->url; return $this->url;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getRedirectUrl() public function getRedirectUrl(): string
{ {
return $this->url; return $this->url;
} }
/** {@inheritDoc} */ /** {@inheritDoc}
public function isRedirectUrl() *
* @throws NotImplementedException
*/
public function isRedirectUrl(): bool
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getErrorNumber() public function getErrorNumber(): int
{ {
return $this->errorNumber; return $this->errorNumber;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getError() public function getError(): string
{ {
return $this->error; return $this->error;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public function isTimeout() public function isTimeout(): bool
{ {
return $this->isTimeout; return $this->isTimeout;
} }

View file

@ -34,6 +34,7 @@ use Friendica\Model\Contact;
use Friendica\Model\GServer; use Friendica\Model\GServer;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Protocol\ActivityNamespace; use Friendica\Protocol\ActivityNamespace;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Email; use Friendica\Protocol\Email;
@ -170,7 +171,7 @@ class Probe
Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]); Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]);
$xrd = null; $xrd = null;
$curlResult = DI::httpClient()->get($ssl_url, [HTTPClientOptions::TIMEOUT => $xrd_timeout, HTTPClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]); $curlResult = DI::httpClient()->get($ssl_url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]);
$ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$xml = $curlResult->getBody(); $xml = $curlResult->getBody();
@ -187,7 +188,7 @@ class Probe
} }
if (!is_object($xrd) && !empty($url)) { if (!is_object($xrd) && !empty($url)) {
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout, HTTPClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]); $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]);
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
Logger::info('Probing timeout', ['url' => $url]); Logger::info('Probing timeout', ['url' => $url]);
@ -429,7 +430,7 @@ class Probe
*/ */
private static function getHideStatus($url) private static function getHideStatus($url)
{ {
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::CONTENT_LENGTH => 1000000]); $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
return false; return false;
} }
@ -950,7 +951,7 @@ class Probe
{ {
$xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20); $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout, HTTPClientOptions::ACCEPT_CONTENT => [$type]]); $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => [$type]]);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$istimeout = true;
return []; return [];

View file

@ -38,7 +38,7 @@ use Friendica\Model\ItemURI;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images; use Friendica\Util\Images;
@ -728,7 +728,7 @@ class OStatus
self::$conv_list[$conversation] = true; self::$conv_list[$conversation] = true;
$curlResult = DI::httpClient()->get($conversation, [HTTPClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]); $curlResult = DI::httpClient()->get($conversation, [HttpClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]);
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
return; return;
@ -922,7 +922,7 @@ class OStatus
} }
$stored = false; $stored = false;
$curlResult = DI::httpClient()->get($related, [HTTPClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]); $curlResult = DI::httpClient()->get($related, [HttpClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]);
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
return; return;

View file

@ -28,9 +28,9 @@ use Friendica\DI;
use Friendica\Model\APContact; use Friendica\Model\APContact;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\CurlResult; use Friendica\Network\HTTPClient\Response\CurlResult;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Network\IHTTPResult; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
/** /**
* Implements HTTP Signatures per draft-cavage-http-signatures-07. * Implements HTTP Signatures per draft-cavage-http-signatures-07.
@ -414,7 +414,7 @@ class HTTPSignature
* 'nobody' => only return the header * 'nobody' => only return the header
* 'cookiejar' => path to cookie jar file * 'cookiejar' => path to cookie jar file
* *
* @return IHTTPResult CurlResult * @return \Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses CurlResult
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => ['application/activity+json', 'application/ld+json']]) public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => ['application/activity+json', 'application/ld+json']])
@ -450,7 +450,7 @@ class HTTPSignature
} }
$curl_opts = $opts; $curl_opts = $opts;
$curl_opts[HTTPClientOptions::HEADERS] = $header; $curl_opts[HttpClientOptions::HEADERS] = $header;
if (!empty($opts['nobody'])) { if (!empty($opts['nobody'])) {
$curlResult = DI::httpClient()->head($request, $curl_opts); $curlResult = DI::httpClient()->head($request, $curl_opts);

View file

@ -30,7 +30,7 @@ use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
/** /**
* Get information about a given URL * Get information about a given URL
@ -214,7 +214,7 @@ class ParseUrl
return $siteinfo; return $siteinfo;
} }
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::CONTENT_LENGTH => 1000000]); $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]);
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
return $siteinfo; return $siteinfo;
} }

View file

@ -29,7 +29,7 @@ use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\HTTPClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Email; use Friendica\Protocol\Email;
@ -153,7 +153,7 @@ class OnePoll
} }
$cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-'); $cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-');
$curlResult = DI::httpClient()->get($contact['poll'], [HTTPClientOptions::COOKIEJAR => $cookiejar]); $curlResult = DI::httpClient()->get($contact['poll'], [HttpClientOptions::COOKIEJAR => $cookiejar]);
unlink($cookiejar); unlink($cookiejar);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {

View file

@ -224,8 +224,8 @@ return [
['getBackend', [], Dice::CHAIN_CALL], ['getBackend', [], Dice::CHAIN_CALL],
], ],
], ],
Network\IHTTPClient::class => [ Network\HTTPClient\Capability\ICanRequestPerHttp::class => [
'instanceOf' => Factory\HTTPClientFactory::class, 'instanceOf' => Network\HTTPClient\Factory\HttpClient::class,
'call' => [ 'call' => [
['createClient', [], Dice::CHAIN_CALL], ['createClient', [], Dice::CHAIN_CALL],
], ],

View file

@ -23,8 +23,8 @@ namespace Friendica\Test;
use Dice\Dice; use Dice\Dice;
use Friendica\DI; use Friendica\DI;
use Friendica\Factory\HTTPClientFactory; use Friendica\Network\HTTPClient\Factory\HttpClient;
use Friendica\Network\IHTTPClient; use Friendica\Network\HTTPClient\Capability\ICanRequestPerHttp;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
/** /**
@ -49,8 +49,8 @@ trait DiceHttpMockHandlerTrait
$dice = DI::getDice(); $dice = DI::getDice();
// addRule() clones the current instance and returns a new one, so no concurrency problems :-) // addRule() clones the current instance and returns a new one, so no concurrency problems :-)
$newDice = $dice->addRule(IHTTPClient::class, [ $newDice = $dice->addRule(ICanRequestPerHttp::class, [
'instanceOf' => HTTPClientFactory::class, 'instanceOf' => HttpClient::class,
'call' => [ 'call' => [
['createClient', [$this->httpRequestHandler], Dice::CHAIN_CALL], ['createClient', [$this->httpRequestHandler], Dice::CHAIN_CALL],
], ],

View file

@ -25,8 +25,8 @@ namespace Friendica\Core;
use Dice\Dice; use Dice\Dice;
use Friendica\Core\Config\ValueObject\Cache; use Friendica\Core\Config\ValueObject\Cache;
use Friendica\DI; use Friendica\DI;
use Friendica\Network\IHTTPResult; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Network\IHTTPClient; use Friendica\Network\HTTPClient\Capability\ICanRequestPerHttp;
use Friendica\Test\MockedTest; use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait; use Friendica\Test\Util\VFSTrait;
use Mockery; use Mockery;
@ -319,7 +319,7 @@ class InstallerTest extends MockedTest
$this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; }); $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
// Mocking the CURL Response // Mocking the CURL Response
$IHTTPResult = Mockery::mock(IHTTPResult::class); $IHTTPResult = Mockery::mock(ICanHandleHttpResponses::class);
$IHTTPResult $IHTTPResult
->shouldReceive('getReturnCode') ->shouldReceive('getReturnCode')
->andReturn('404'); ->andReturn('404');
@ -331,7 +331,7 @@ class InstallerTest extends MockedTest
->andReturn('test Error'); ->andReturn('test Error');
// Mocking the CURL Request // Mocking the CURL Request
$networkMock = Mockery::mock(IHTTPClient::class); $networkMock = Mockery::mock(ICanRequestPerHttp::class);
$networkMock $networkMock
->shouldReceive('fetchFull') ->shouldReceive('fetchFull')
->with('https://test/install/testrewrite') ->with('https://test/install/testrewrite')
@ -342,7 +342,7 @@ class InstallerTest extends MockedTest
->andReturn($IHTTPResult); ->andReturn($IHTTPResult);
$this->dice->shouldReceive('create') $this->dice->shouldReceive('create')
->with(IHTTPClient::class) ->with(ICanRequestPerHttp::class)
->andReturn($networkMock); ->andReturn($networkMock);
DI::init($this->dice); DI::init($this->dice);
@ -366,19 +366,19 @@ class InstallerTest extends MockedTest
$this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; }); $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
// Mocking the failed CURL Response // Mocking the failed CURL Response
$IHTTPResultF = Mockery::mock(IHTTPResult::class); $IHTTPResultF = Mockery::mock(ICanHandleHttpResponses::class);
$IHTTPResultF $IHTTPResultF
->shouldReceive('getReturnCode') ->shouldReceive('getReturnCode')
->andReturn('404'); ->andReturn('404');
// Mocking the working CURL Response // Mocking the working CURL Response
$IHTTPResultW = Mockery::mock(IHTTPResult::class); $IHTTPResultW = Mockery::mock(ICanHandleHttpResponses::class);
$IHTTPResultW $IHTTPResultW
->shouldReceive('getReturnCode') ->shouldReceive('getReturnCode')
->andReturn('204'); ->andReturn('204');
// Mocking the CURL Request // Mocking the CURL Request
$networkMock = Mockery::mock(IHTTPClient::class); $networkMock = Mockery::mock(ICanRequestPerHttp::class);
$networkMock $networkMock
->shouldReceive('fetchFull') ->shouldReceive('fetchFull')
->with('https://test/install/testrewrite') ->with('https://test/install/testrewrite')
@ -389,7 +389,7 @@ class InstallerTest extends MockedTest
->andReturn($IHTTPResultW); ->andReturn($IHTTPResultW);
$this->dice->shouldReceive('create') $this->dice->shouldReceive('create')
->with(IHTTPClient::class) ->with(ICanRequestPerHttp::class)
->andReturn($networkMock); ->andReturn($networkMock);
DI::init($this->dice); DI::init($this->dice);

View file

@ -40,7 +40,7 @@ use Friendica\DI;
use Friendica\Core\Config\Factory\Config; use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Config\Repository; use Friendica\Core\Config\Repository;
use Friendica\Core\Storage\Type; use Friendica\Core\Storage\Type;
use Friendica\Network\HTTPClient; use Friendica\Network\HTTPClient\Client\HttpClientCan;
use Friendica\Test\DatabaseTest; use Friendica\Test\DatabaseTest;
use Friendica\Test\Util\Database\StaticDatabase; use Friendica\Test\Util\Database\StaticDatabase;
use Friendica\Test\Util\VFSTrait; use Friendica\Test\Util\VFSTrait;
@ -61,7 +61,7 @@ class StorageManagerTest extends DatabaseTest
private $logger; private $logger;
/** @var L10n */ /** @var L10n */
private $l10n; private $l10n;
/** @var HTTPClient */ /** @var HttpClientCan */
private $httpRequest; private $httpRequest;
protected function setUp(): void protected function setUp(): void
@ -93,7 +93,7 @@ class StorageManagerTest extends DatabaseTest
$this->l10n = \Mockery::mock(L10n::class); $this->l10n = \Mockery::mock(L10n::class);
$this->httpRequest = \Mockery::mock(HTTPClient::class); $this->httpRequest = \Mockery::mock(HttpClientCan::class);
} }
protected function tearDown(): void protected function tearDown(): void

View file

@ -1,6 +1,6 @@
<?php <?php
namespace Friendica\Test\src\Network; namespace Friendica\Test\src\Network\HTTPClient\Client;
use Friendica\DI; use Friendica\DI;
use Friendica\Test\DiceHttpMockHandlerTrait; use Friendica\Test\DiceHttpMockHandlerTrait;

View file

@ -19,11 +19,11 @@
* *
*/ */
namespace Friendica\Test\src\Network; namespace Friendica\Test\src\Network\HTTPClient\Response;
use Dice\Dice; use Dice\Dice;
use Friendica\DI; use Friendica\DI;
use Friendica\Network\CurlResult; use Friendica\Network\HTTPClient\Response\CurlResult;
use Mockery\MockInterface; use Mockery\MockInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -37,7 +37,7 @@ class CurlResultTest extends TestCase
/** @var Dice|MockInterface $dice */ /** @var Dice|MockInterface $dice */
$dice = \Mockery::mock(Dice::class)->makePartial(); $dice = \Mockery::mock(Dice::class)->makePartial();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php'); $dice = $dice->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php');
$logger = new NullLogger(); $logger = new NullLogger();
$dice->shouldReceive('create') $dice->shouldReceive('create')
@ -52,12 +52,12 @@ class CurlResultTest extends TestCase
*/ */
public function testNormal() public function testNormal()
{ {
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php'); $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
$curlResult = new CurlResult('https://test.local', $header . $body, [ $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
'http_code' => 200, 'http_code' => 200,
'content_type' => 'text/html; charset=utf-8', 'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local' 'url' => 'https://test.local'
@ -80,12 +80,12 @@ class CurlResultTest extends TestCase
*/ */
public function testRedirect() public function testRedirect()
{ {
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php'); $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
$curlResult = new CurlResult('https://test.local/test/it', $header . $body, [ $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local/test/it', $header . $body, [
'http_code' => 301, 'http_code' => 301,
'content_type' => 'text/html; charset=utf-8', 'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local/test/it', 'url' => 'https://test.local/test/it',
@ -107,12 +107,12 @@ class CurlResultTest extends TestCase
*/ */
public function testTimeout() public function testTimeout()
{ {
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php'); $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
$curlResult = new CurlResult('https://test.local/test/it', $header . $body, [ $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local/test/it', $header . $body, [
'http_code' => 500, 'http_code' => 500,
'content_type' => 'text/html; charset=utf-8', 'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local/test/it', 'url' => 'https://test.local/test/it',
@ -136,9 +136,9 @@ class CurlResultTest extends TestCase
*/ */
public function testRedirectHeader() public function testRedirectHeader()
{ {
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect'); $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.redirect');
$headerArray = include(__DIR__ . '/../../datasets/curl/about.redirect.php'); $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.redirect.php');
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
$curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [ $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
@ -162,10 +162,10 @@ class CurlResultTest extends TestCase
*/ */
public function testInHeader() public function testInHeader()
{ {
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
$curlResult = new CurlResult('https://test.local', $header . $body, [ $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
'http_code' => 200, 'http_code' => 200,
'content_type' => 'text/html; charset=utf-8', 'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local' 'url' => 'https://test.local'
@ -179,10 +179,10 @@ class CurlResultTest extends TestCase
*/ */
public function testGetHeaderArray() public function testGetHeaderArray()
{ {
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
$curlResult = new CurlResult('https://test.local', $header . $body, [ $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
'http_code' => 200, 'http_code' => 200,
'content_type' => 'text/html; charset=utf-8', 'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local' 'url' => 'https://test.local'
@ -199,8 +199,8 @@ class CurlResultTest extends TestCase
*/ */
public function testGetHeaderWithParam() public function testGetHeaderWithParam()
{ {
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
$curlResult = new CurlResult('https://test.local', $header . $body, [ $curlResult = new CurlResult('https://test.local', $header . $body, [
'http_code' => 200, 'http_code' => 200,