mirror of
https://github.com/friendica/friendica
synced 2024-11-09 23:02:54 +00:00
Restructure HTTPClient for new paradigm
This commit is contained in:
parent
fa55928ea3
commit
409d909d0f
25 changed files with 210 additions and 198 deletions
|
@ -39,7 +39,7 @@ use Friendica\Model\Event;
|
|||
use Friendica\Model\Photo;
|
||||
use Friendica\Model\Post;
|
||||
use Friendica\Model\Tag;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Object\Image;
|
||||
use Friendica\Protocol\Activity;
|
||||
use Friendica\Util\Images;
|
||||
|
@ -1201,7 +1201,7 @@ class BBCode
|
|||
$text = DI::cache()->get($cache_key);
|
||||
|
||||
if (is_null($text)) {
|
||||
$curlResult = DI::httpClient()->head($match[1], [HTTPClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]);
|
||||
$curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]);
|
||||
if ($curlResult->isSuccess()) {
|
||||
$mimetype = $curlResult->getHeader('Content-Type')[0] ?? '';
|
||||
} else {
|
||||
|
@ -1272,7 +1272,7 @@ class BBCode
|
|||
return $text;
|
||||
}
|
||||
|
||||
$curlResult = DI::httpClient()->head($match[1], [HTTPClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]);
|
||||
$curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]);
|
||||
if ($curlResult->isSuccess()) {
|
||||
$mimetype = $curlResult->getHeader('Content-Type')[0] ?? '';
|
||||
} else {
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace Friendica\Core;
|
|||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Object\Search\ContactResult;
|
||||
use Friendica\Object\Search\ResultList;
|
||||
use Friendica\Util\Network;
|
||||
|
@ -228,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 => ['application/json']]);
|
||||
$curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), [HttpClientOptions::ACCEPT_CONTENT => ['application/json']]);
|
||||
if ($curlResult->isSuccess()) {
|
||||
$searchResult = json_decode($curlResult->getBody(), true);
|
||||
if (!empty($searchResult['profiles'])) {
|
||||
|
|
|
@ -415,11 +415,11 @@ abstract class DI
|
|||
//
|
||||
|
||||
/**
|
||||
* @return Network\IHTTPClient
|
||||
* @return Network\HTTPClient\Capability\ICanRequestPerHttp
|
||||
*/
|
||||
public static function httpClient()
|
||||
{
|
||||
return self::$dice->create(Network\IHTTPClient::class);
|
||||
return self::$dice->create(Network\HTTPClient\Capability\ICanRequestPerHttp::class);
|
||||
}
|
||||
|
||||
//
|
||||
|
|
|
@ -32,8 +32,8 @@ use Friendica\Database\Database;
|
|||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Module\Register;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\IHTTPResult;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
|
||||
use Friendica\Protocol\Relay;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\Network;
|
||||
|
@ -315,7 +315,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]);
|
||||
$curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::TIMEOUT => $xrd_timeout]);
|
||||
if ($curlResult->isTimeout()) {
|
||||
self::setFailure($url);
|
||||
return false;
|
||||
|
@ -323,7 +323,7 @@ class GServer
|
|||
|
||||
// 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))) {
|
||||
$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)) {
|
||||
Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]);
|
||||
self::setFailure($url);
|
||||
|
@ -359,7 +359,7 @@ class GServer
|
|||
$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 ((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()]);
|
||||
|
@ -383,7 +383,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]);
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
|
||||
if ($curlResult->isSuccess()) {
|
||||
$urldata = self::analyseRootHeader($curlResult, $serverdata);
|
||||
$urldata = self::analyseRootBody($curlResult, $urldata, $url);
|
||||
|
@ -672,13 +672,13 @@ class GServer
|
|||
/**
|
||||
* Detect server type by using the nodeinfo data
|
||||
*
|
||||
* @param string $url address of the server
|
||||
* @param IHTTPResult $httpResult
|
||||
* @param string $url address of the server
|
||||
* @param ICanHandleHttpResponses $httpResult
|
||||
*
|
||||
* @return array Server data
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
private static function fetchNodeinfo(string $url, IHTTPResult $httpResult)
|
||||
private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult)
|
||||
{
|
||||
if (!$httpResult->isSuccess()) {
|
||||
return [];
|
||||
|
@ -959,7 +959,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]);
|
||||
$curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout]);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -1725,7 +1725,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]]]);
|
||||
$curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
|
||||
|
||||
if ($curlResult->isSuccess()) {
|
||||
$servers = json_decode($curlResult->getBody(), true);
|
||||
|
|
|
@ -26,7 +26,7 @@ use Friendica\Core\System;
|
|||
use Friendica\Database\Database;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Util\Proxy;
|
||||
|
||||
/**
|
||||
|
@ -100,7 +100,7 @@ class Link
|
|||
{
|
||||
$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 (empty($media['mimetype'])) {
|
||||
return $curlResult->getHeader('Content-Type')[0] ?? '';
|
||||
|
|
|
@ -30,7 +30,7 @@ use Friendica\DI;
|
|||
use Friendica\Model\Item;
|
||||
use Friendica\Model\Photo;
|
||||
use Friendica\Model\Post;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Util\Images;
|
||||
use Friendica\Util\Network;
|
||||
use Friendica\Util\ParseUrl;
|
||||
|
@ -168,7 +168,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'], [HTTPClientOptions::TIMEOUT => $timeout]);
|
||||
$curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout]);
|
||||
if ($curlResult->isSuccess()) {
|
||||
if (empty($media['mimetype'])) {
|
||||
$media['mimetype'] = $curlResult->getHeader('Content-Type')[0] ?? '';
|
||||
|
|
|
@ -28,7 +28,7 @@ use Friendica\Database\DBA;
|
|||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Util\HTTPSignature;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
|
@ -102,7 +102,7 @@ class Magic extends BaseModule
|
|||
);
|
||||
|
||||
// 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()) {
|
||||
$j = json_decode($curlResult->getBody(), true);
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Network;
|
||||
namespace Friendica\Network\HTTPClient\Capability;
|
||||
|
||||
use Psr\Http\Message\MessageInterface;
|
||||
|
||||
/**
|
||||
* Temporary class to map Friendica used variables based on PSR-7 HTTPResponse
|
||||
*/
|
||||
interface IHTTPResult
|
||||
interface ICanHandleHttpResponses
|
||||
{
|
||||
/**
|
||||
* Gets the Return Code
|
||||
|
@ -25,13 +25,14 @@ interface IHTTPResult
|
|||
|
||||
/**
|
||||
* Returns the headers
|
||||
* @see MessageInterface::getHeader()
|
||||
*
|
||||
* @param string $header optional header field. Return all fields if empty
|
||||
*
|
||||
* @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
|
|
@ -19,14 +19,14 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Network;
|
||||
namespace Friendica\Network\HTTPClient\Capability;
|
||||
|
||||
use GuzzleHttp\Exception\TransferException;
|
||||
|
||||
/**
|
||||
* Interface for calling HTTP requests and returning their responses
|
||||
*/
|
||||
interface IHTTPClient
|
||||
interface ICanRequestPerHttp
|
||||
{
|
||||
/**
|
||||
* Fetches the content of an URL
|
||||
|
@ -41,7 +41,7 @@ interface IHTTPClient
|
|||
*
|
||||
* @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.
|
||||
|
@ -54,12 +54,12 @@ interface IHTTPClient
|
|||
* @param string $accept_content supply Accept: header with 'accept_content' as the value
|
||||
* @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 array $opts (optional parameters) associative array with:
|
||||
|
@ -68,9 +68,9 @@ interface IHTTPClient
|
|||
* 'cookiejar' => path to cookie jar file
|
||||
* '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.
|
||||
|
@ -83,9 +83,9 @@ interface IHTTPClient
|
|||
* 'header' => header array
|
||||
* '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
|
||||
|
@ -101,9 +101,9 @@ interface IHTTPClient
|
|||
* 'content_length' => int maximum File content length
|
||||
* '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
|
||||
|
@ -113,9 +113,9 @@ interface IHTTPClient
|
|||
* @param array $headers HTTP headers
|
||||
* @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
|
||||
|
@ -129,5 +129,5 @@ interface IHTTPClient
|
|||
*
|
||||
* @throws TransferException In case there's an error during the resolving
|
||||
*/
|
||||
public function finalUrl(string $url);
|
||||
public function finalUrl(string $url): string;
|
||||
}
|
|
@ -19,9 +19,14 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Network;
|
||||
namespace Friendica\Network\HTTPClient\Client;
|
||||
|
||||
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\Profiler;
|
||||
use GuzzleHttp\Client;
|
||||
|
@ -37,7 +42,7 @@ use Psr\Log\LoggerInterface;
|
|||
/**
|
||||
* Performs HTTP requests to a given URL
|
||||
*/
|
||||
class HTTPClient implements IHTTPClient
|
||||
class HttpClientCan implements ICanRequestPerHttp
|
||||
{
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
@ -59,7 +64,7 @@ class HTTPClient implements IHTTPClient
|
|||
/**
|
||||
* {@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->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
|
||||
|
@ -95,43 +100,43 @@ class HTTPClient implements IHTTPClient
|
|||
|
||||
$conf = [];
|
||||
|
||||
if (!empty($opts[HTTPClientOptions::COOKIEJAR])) {
|
||||
$jar = new FileCookieJar($opts[HTTPClientOptions::COOKIEJAR]);
|
||||
if (!empty($opts[HttpClientOptions::COOKIEJAR])) {
|
||||
$jar = new FileCookieJar($opts[HttpClientOptions::COOKIEJAR]);
|
||||
$conf[RequestOptions::COOKIES] = $jar;
|
||||
}
|
||||
|
||||
$headers = [];
|
||||
|
||||
if (!empty($opts[HTTPClientOptions::ACCEPT_CONTENT])) {
|
||||
$headers['Accept'] = $opts[HTTPClientOptions::ACCEPT_CONTENT];
|
||||
if (!empty($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.');
|
||||
$headers = array_merge($opts[HTTPClientOptions::LEGACY_HEADER], $headers);
|
||||
$headers = array_merge($opts[HttpClientOptions::LEGACY_HEADER], $headers);
|
||||
}
|
||||
|
||||
if (!empty($opts[HTTPClientOptions::HEADERS])) {
|
||||
$headers = array_merge($opts[HTTPClientOptions::HEADERS], $headers);
|
||||
if (!empty($opts[HttpClientOptions::HEADERS])) {
|
||||
$headers = array_merge($opts[HttpClientOptions::HEADERS], $headers);
|
||||
}
|
||||
|
||||
$conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers);
|
||||
|
||||
if (!empty($opts[HTTPClientOptions::TIMEOUT])) {
|
||||
$conf[RequestOptions::TIMEOUT] = $opts[HTTPClientOptions::TIMEOUT];
|
||||
if (!empty($opts[HttpClientOptions::TIMEOUT])) {
|
||||
$conf[RequestOptions::TIMEOUT] = $opts[HttpClientOptions::TIMEOUT];
|
||||
}
|
||||
|
||||
if (!empty($opts[HTTPClientOptions::BODY])) {
|
||||
$conf[RequestOptions::BODY] = $opts[HTTPClientOptions::BODY];
|
||||
if (!empty($opts[HttpClientOptions::BODY])) {
|
||||
$conf[RequestOptions::BODY] = $opts[HttpClientOptions::BODY];
|
||||
}
|
||||
|
||||
if (!empty($opts[HTTPClientOptions::AUTH])) {
|
||||
$conf[RequestOptions::AUTH] = $opts[HTTPClientOptions::AUTH];
|
||||
if (!empty($opts[HttpClientOptions::AUTH])) {
|
||||
$conf[RequestOptions::AUTH] = $opts[HttpClientOptions::AUTH];
|
||||
}
|
||||
|
||||
$conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
|
||||
if (!empty($opts[HTTPClientOptions::CONTENT_LENGTH]) &&
|
||||
(int)$response->getHeaderLine('Content-Length') > $opts[HTTPClientOptions::CONTENT_LENGTH]) {
|
||||
if (!empty($opts[HttpClientOptions::CONTENT_LENGTH]) &&
|
||||
(int)$response->getHeaderLine('Content-Length') > $opts[HttpClientOptions::CONTENT_LENGTH]) {
|
||||
throw new TransferException('The file is too big!');
|
||||
}
|
||||
};
|
||||
|
@ -159,7 +164,7 @@ class HTTPClient implements IHTTPClient
|
|||
|
||||
/** {@inheritDoc}
|
||||
*/
|
||||
public function head(string $url, array $opts = []): IHTTPResult
|
||||
public function head(string $url, array $opts = []): ICanHandleHttpResponses
|
||||
{
|
||||
return $this->request('head', $url, $opts);
|
||||
}
|
||||
|
@ -167,7 +172,7 @@ class HTTPClient implements IHTTPClient
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get(string $url, array $opts = []): IHTTPResult
|
||||
public function get(string $url, array $opts = []): ICanHandleHttpResponses
|
||||
{
|
||||
return $this->request('get', $url, $opts);
|
||||
}
|
||||
|
@ -175,18 +180,18 @@ class HTTPClient implements IHTTPClient
|
|||
/**
|
||||
* {@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[HTTPClientOptions::BODY] = $params;
|
||||
$opts[HttpClientOptions::BODY] = $params;
|
||||
|
||||
if (!empty($headers)) {
|
||||
$opts[HTTPClientOptions::HEADERS] = $headers;
|
||||
$opts[HttpClientOptions::HEADERS] = $headers;
|
||||
}
|
||||
|
||||
if (!empty($timeout)) {
|
||||
$opts[HTTPClientOptions::TIMEOUT] = $timeout;
|
||||
$opts[HttpClientOptions::TIMEOUT] = $timeout;
|
||||
}
|
||||
|
||||
return $this->request('post', $url, $opts);
|
||||
|
@ -195,7 +200,7 @@ class HTTPClient implements IHTTPClient
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function finalUrl(string $url)
|
||||
public function finalUrl(string $url): string
|
||||
{
|
||||
$this->profiler->startRecording('network');
|
||||
|
||||
|
@ -229,7 +234,7 @@ class HTTPClient implements IHTTPClient
|
|||
/**
|
||||
* {@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);
|
||||
|
||||
|
@ -239,7 +244,7 @@ class HTTPClient implements IHTTPClient
|
|||
/**
|
||||
* {@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(
|
||||
$url,
|
|
@ -1,13 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Network;
|
||||
namespace Friendica\Network\HTTPClient\Client;
|
||||
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
/**
|
||||
* 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
|
|
@ -1,15 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Factory;
|
||||
namespace Friendica\Network\HTTPClient\Factory;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\BaseFactory;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Network\HTTPClient;
|
||||
use Friendica\Network\IHTTPClient;
|
||||
use Friendica\Network\HTTPClient\Client;
|
||||
use Friendica\Network\HTTPClient\Capability\ICanRequestPerHttp;
|
||||
use Friendica\Util\Profiler;
|
||||
use Friendica\Util\Strings;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use mattwright\URLResolver;
|
||||
|
@ -18,9 +18,9 @@ use Psr\Http\Message\ResponseInterface;
|
|||
use Psr\Http\Message\UriInterface;
|
||||
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 */
|
||||
private $config;
|
||||
|
@ -42,17 +42,17 @@ class HTTPClientFactory extends BaseFactory
|
|||
*
|
||||
* @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');
|
||||
|
||||
if (!empty($proxy)) {
|
||||
$proxyuser = $this->config->get('system', 'proxyuser');
|
||||
$proxyUser = $this->config->get('system', 'proxyuser');
|
||||
|
||||
if (!empty($proxyuser)) {
|
||||
$proxy = $proxyuser . '@' . $proxy;
|
||||
if (!empty($proxyUser)) {
|
||||
$proxy = $proxyUser . '@' . $proxy;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,7 @@ class HTTPClientFactory extends BaseFactory
|
|||
DB_UPDATE_VERSION . '; ' .
|
||||
$this->baseUrl->get();
|
||||
|
||||
$guzzle = new Client([
|
||||
$guzzle = new GuzzleHttp\Client([
|
||||
RequestOptions::ALLOW_REDIRECTS => [
|
||||
'max' => 8,
|
||||
'on_redirect' => $onRedirect,
|
||||
|
@ -88,7 +88,7 @@ class HTTPClientFactory extends BaseFactory
|
|||
RequestOptions::FORCE_IP_RESOLVE => ($this->config->get('system', 'ipv4_resolve') ? 'v4' : null),
|
||||
RequestOptions::CONNECT_TIMEOUT => 10,
|
||||
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
|
||||
RequestOptions::VERIFY => (bool)$this->config->get('system', 'verifyssl'),
|
||||
RequestOptions::PROXY => $proxy,
|
||||
|
@ -108,6 +108,6 @@ class HTTPClientFactory extends BaseFactory
|
|||
// Some websites test the browser for cookie support, so this enhances results.
|
||||
$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);
|
||||
}
|
||||
}
|
|
@ -19,17 +19,17 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Network;
|
||||
namespace Friendica\Network\HTTPClient\Response;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Util\Network;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -101,35 +101,36 @@ class CurlResult implements IHTTPResult
|
|||
*
|
||||
* @param string $url optional URL
|
||||
*
|
||||
* @return IHTTPResult a CURL with error response
|
||||
* @return ICanHandleHttpResponses a CURL with error response
|
||||
* @throws InternalServerErrorException
|
||||
*/
|
||||
public static function createErrorCurl($url = '')
|
||||
public static function createErrorCurl(string $url = '')
|
||||
{
|
||||
return new CurlResult($url, '', ['http_code' => 0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Curl constructor.
|
||||
* @param string $url the URL which was called
|
||||
* @param string $result the result of the curl execution
|
||||
* @param array $info an additional info array
|
||||
* @param int $errorNumber the error number or 0 (zero) if no error
|
||||
* @param string $error the error message or '' (the empty string) if no
|
||||
*
|
||||
* @param string $url the URL which was called
|
||||
* @param string $result the result of the curl execution
|
||||
* @param array $info an additional info array
|
||||
* @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
|
||||
*/
|
||||
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)) {
|
||||
throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
|
||||
}
|
||||
|
||||
$this->returnCode = $info['http_code'];
|
||||
$this->url = $url;
|
||||
$this->info = $info;
|
||||
$this->returnCode = $info['http_code'];
|
||||
$this->url = $url;
|
||||
$this->info = $info;
|
||||
$this->errorNumber = $errorNumber;
|
||||
$this->error = $error;
|
||||
$this->error = $error;
|
||||
|
||||
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
|
||||
|
||||
$header = '';
|
||||
$base = $result;
|
||||
$base = $result;
|
||||
while (preg_match('/^HTTP\/.+? \d+/', $base)) {
|
||||
$chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
|
||||
$header .= $chunk;
|
||||
$base = substr($base, strlen($chunk));
|
||||
}
|
||||
|
||||
$this->body = substr($result, strlen($header));
|
||||
$this->header = $header;
|
||||
$this->body = substr($result, strlen($header));
|
||||
$this->header = $header;
|
||||
$this->header_fields = []; // Is filled on demand
|
||||
}
|
||||
|
||||
|
@ -185,7 +186,7 @@ class CurlResult implements IHTTPResult
|
|||
$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'] ?? '');
|
||||
if (empty($redirect_parts)) {
|
||||
$redirect_parts = [];
|
||||
|
@ -229,19 +230,19 @@ class CurlResult implements IHTTPResult
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getReturnCode()
|
||||
public function getReturnCode(): string
|
||||
{
|
||||
return $this->returnCode;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getContentType()
|
||||
public function getContentType(): string
|
||||
{
|
||||
return $this->contentType;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getHeader($header)
|
||||
public function getHeader(string $header): array
|
||||
{
|
||||
if (empty($header)) {
|
||||
return [];
|
||||
|
@ -259,13 +260,13 @@ class CurlResult implements IHTTPResult
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getHeaders()
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->getHeaderArray();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function inHeader(string $field)
|
||||
public function inHeader(string $field): bool
|
||||
{
|
||||
$field = strtolower(trim($field));
|
||||
|
||||
|
@ -275,7 +276,7 @@ class CurlResult implements IHTTPResult
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getHeaderArray()
|
||||
public function getHeaderArray(): array
|
||||
{
|
||||
if (!empty($this->header_fields)) {
|
||||
return $this->header_fields;
|
||||
|
@ -285,9 +286,9 @@ class CurlResult implements IHTTPResult
|
|||
|
||||
$lines = explode("\n", trim($this->header));
|
||||
foreach ($lines as $line) {
|
||||
$parts = explode(':', $line);
|
||||
$parts = explode(':', $line);
|
||||
$headerfield = strtolower(trim(array_shift($parts)));
|
||||
$headerdata = trim(implode(':', $parts));
|
||||
$headerdata = trim(implode(':', $parts));
|
||||
if (empty($this->header_fields[$headerfield])) {
|
||||
$this->header_fields[$headerfield] = [$headerdata];
|
||||
} elseif (!in_array($headerdata, $this->header_fields[$headerfield])) {
|
||||
|
@ -299,49 +300,49 @@ class CurlResult implements IHTTPResult
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function isSuccess()
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
return $this->isSuccess;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getUrl()
|
||||
public function getUrl(): string
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getRedirectUrl()
|
||||
public function getRedirectUrl(): string
|
||||
{
|
||||
return $this->redirectUrl;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getBody()
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function isRedirectUrl()
|
||||
public function isRedirectUrl(): bool
|
||||
{
|
||||
return $this->isRedirectUrl;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getErrorNumber()
|
||||
public function getErrorNumber(): int
|
||||
{
|
||||
return $this->errorNumber;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getError()
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function isTimeout()
|
||||
public function isTimeout(): bool
|
||||
{
|
||||
return $this->isTimeout;
|
||||
}
|
|
@ -19,10 +19,10 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Network;
|
||||
namespace Friendica\Network\HTTPClient\Response;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
|
||||
use Friendica\Network\HTTPException\NotImplementedException;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
@ -30,7 +30,7 @@ use Psr\Http\Message\ResponseInterface;
|
|||
/**
|
||||
* 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 */
|
||||
private $url;
|
||||
|
@ -79,68 +79,72 @@ class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getReturnCode()
|
||||
public function getReturnCode(): string
|
||||
{
|
||||
return $this->getStatusCode();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getContentType()
|
||||
public function getContentType(): string
|
||||
{
|
||||
$contentTypes = $this->getHeader('Content-Type') ?? [];
|
||||
|
||||
return array_pop($contentTypes) ?? '';
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function inHeader(string $field)
|
||||
public function inHeader(string $field): bool
|
||||
{
|
||||
return $this->hasHeader($field);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getHeaderArray()
|
||||
public function getHeaderArray(): array
|
||||
{
|
||||
return $this->getHeaders();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function isSuccess()
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
return $this->isSuccess;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getUrl()
|
||||
public function getUrl(): string
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getRedirectUrl()
|
||||
public function getRedirectUrl(): string
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function isRedirectUrl()
|
||||
/** {@inheritDoc}
|
||||
*
|
||||
* @throws NotImplementedException
|
||||
*/
|
||||
public function isRedirectUrl(): bool
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getErrorNumber()
|
||||
public function getErrorNumber(): int
|
||||
{
|
||||
return $this->errorNumber;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getError()
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function isTimeout()
|
||||
public function isTimeout(): bool
|
||||
{
|
||||
return $this->isTimeout;
|
||||
}
|
|
@ -34,6 +34,7 @@ use Friendica\Model\Contact;
|
|||
use Friendica\Model\GServer;
|
||||
use Friendica\Model\Profile;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Protocol\ActivityNamespace;
|
||||
use Friendica\Protocol\ActivityPub;
|
||||
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)]);
|
||||
$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);
|
||||
if ($curlResult->isSuccess()) {
|
||||
$xml = $curlResult->getBody();
|
||||
|
@ -187,7 +188,7 @@ class Probe
|
|||
}
|
||||
|
||||
if (!is_object($xrd) && !empty($url)) {
|
||||
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout, HTTPClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]);
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]);
|
||||
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
|
||||
if ($curlResult->isTimeout()) {
|
||||
Logger::info('Probing timeout', ['url' => $url]);
|
||||
|
@ -429,7 +430,7 @@ class Probe
|
|||
*/
|
||||
private static function getHideStatus($url)
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::CONTENT_LENGTH => 1000000]);
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -950,7 +951,7 @@ class Probe
|
|||
{
|
||||
$xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
|
||||
|
||||
$curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout, HTTPClientOptions::ACCEPT_CONTENT => [$type]]);
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => [$type]]);
|
||||
if ($curlResult->isTimeout()) {
|
||||
self::$istimeout = true;
|
||||
return [];
|
||||
|
|
|
@ -38,7 +38,7 @@ use Friendica\Model\ItemURI;
|
|||
use Friendica\Model\Post;
|
||||
use Friendica\Model\Tag;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Network\Probe;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\Images;
|
||||
|
@ -728,7 +728,7 @@ class OStatus
|
|||
|
||||
self::$conv_list[$conversation] = true;
|
||||
|
||||
$curlResult = DI::httpClient()->get($conversation, [HTTPClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]);
|
||||
$curlResult = DI::httpClient()->get($conversation, [HttpClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]);
|
||||
|
||||
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
|
||||
return;
|
||||
|
@ -922,7 +922,7 @@ class OStatus
|
|||
}
|
||||
|
||||
$stored = false;
|
||||
$curlResult = DI::httpClient()->get($related, [HTTPClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]);
|
||||
$curlResult = DI::httpClient()->get($related, [HttpClientOptions::ACCEPT_CONTENT => ['application/atom+xml', 'text/html']]);
|
||||
|
||||
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
|
||||
return;
|
||||
|
|
|
@ -28,9 +28,9 @@ use Friendica\DI;
|
|||
use Friendica\Model\APContact;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Network\CurlResult;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\IHTTPResult;
|
||||
use Friendica\Network\HTTPClient\Response\CurlResult;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
|
||||
|
||||
/**
|
||||
* Implements HTTP Signatures per draft-cavage-http-signatures-07.
|
||||
|
@ -414,7 +414,7 @@ class HTTPSignature
|
|||
* 'nobody' => only return the header
|
||||
* 'cookiejar' => path to cookie jar file
|
||||
*
|
||||
* @return IHTTPResult CurlResult
|
||||
* @return \Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses CurlResult
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
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[HTTPClientOptions::HEADERS] = $header;
|
||||
$curl_opts[HttpClientOptions::HEADERS] = $header;
|
||||
|
||||
if (!empty($opts['nobody'])) {
|
||||
$curlResult = DI::httpClient()->head($request, $curl_opts);
|
||||
|
|
|
@ -30,7 +30,7 @@ use Friendica\Database\Database;
|
|||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
|
||||
/**
|
||||
* Get information about a given URL
|
||||
|
@ -214,7 +214,7 @@ class ParseUrl
|
|||
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())) {
|
||||
return $siteinfo;
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ use Friendica\Model\Contact;
|
|||
use Friendica\Model\Item;
|
||||
use Friendica\Model\Post;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Protocol\Activity;
|
||||
use Friendica\Protocol\ActivityPub;
|
||||
use Friendica\Protocol\Email;
|
||||
|
@ -153,7 +153,7 @@ class 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);
|
||||
|
||||
if ($curlResult->isTimeout()) {
|
||||
|
|
|
@ -224,8 +224,8 @@ return [
|
|||
['getBackend', [], Dice::CHAIN_CALL],
|
||||
],
|
||||
],
|
||||
Network\IHTTPClient::class => [
|
||||
'instanceOf' => Factory\HTTPClientFactory::class,
|
||||
Network\HTTPClient\Capability\ICanRequestPerHttp::class => [
|
||||
'instanceOf' => Network\HTTPClient\Factory\HttpClient::class,
|
||||
'call' => [
|
||||
['createClient', [], Dice::CHAIN_CALL],
|
||||
],
|
||||
|
|
|
@ -23,8 +23,8 @@ namespace Friendica\Test;
|
|||
|
||||
use Dice\Dice;
|
||||
use Friendica\DI;
|
||||
use Friendica\Factory\HTTPClientFactory;
|
||||
use Friendica\Network\IHTTPClient;
|
||||
use Friendica\Network\HTTPClient\Factory\HttpClient;
|
||||
use Friendica\Network\HTTPClient\Capability\ICanRequestPerHttp;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
|
||||
/**
|
||||
|
@ -49,8 +49,8 @@ trait DiceHttpMockHandlerTrait
|
|||
|
||||
$dice = DI::getDice();
|
||||
// addRule() clones the current instance and returns a new one, so no concurrency problems :-)
|
||||
$newDice = $dice->addRule(IHTTPClient::class, [
|
||||
'instanceOf' => HTTPClientFactory::class,
|
||||
$newDice = $dice->addRule(ICanRequestPerHttp::class, [
|
||||
'instanceOf' => HttpClient::class,
|
||||
'call' => [
|
||||
['createClient', [$this->httpRequestHandler], Dice::CHAIN_CALL],
|
||||
],
|
||||
|
|
|
@ -25,8 +25,8 @@ namespace Friendica\Core;
|
|||
use Dice\Dice;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\DI;
|
||||
use Friendica\Network\IHTTPResult;
|
||||
use Friendica\Network\IHTTPClient;
|
||||
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
|
||||
use Friendica\Network\HTTPClient\Capability\ICanRequestPerHttp;
|
||||
use Friendica\Test\MockedTest;
|
||||
use Friendica\Test\Util\VFSTrait;
|
||||
use Mockery;
|
||||
|
@ -319,7 +319,7 @@ class InstallerTest extends MockedTest
|
|||
$this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
|
||||
|
||||
// Mocking the CURL Response
|
||||
$IHTTPResult = Mockery::mock(IHTTPResult::class);
|
||||
$IHTTPResult = Mockery::mock(ICanHandleHttpResponses::class);
|
||||
$IHTTPResult
|
||||
->shouldReceive('getReturnCode')
|
||||
->andReturn('404');
|
||||
|
@ -331,7 +331,7 @@ class InstallerTest extends MockedTest
|
|||
->andReturn('test Error');
|
||||
|
||||
// Mocking the CURL Request
|
||||
$networkMock = Mockery::mock(IHTTPClient::class);
|
||||
$networkMock = Mockery::mock(ICanRequestPerHttp::class);
|
||||
$networkMock
|
||||
->shouldReceive('fetchFull')
|
||||
->with('https://test/install/testrewrite')
|
||||
|
@ -342,7 +342,7 @@ class InstallerTest extends MockedTest
|
|||
->andReturn($IHTTPResult);
|
||||
|
||||
$this->dice->shouldReceive('create')
|
||||
->with(IHTTPClient::class)
|
||||
->with(ICanRequestPerHttp::class)
|
||||
->andReturn($networkMock);
|
||||
|
||||
DI::init($this->dice);
|
||||
|
@ -366,19 +366,19 @@ class InstallerTest extends MockedTest
|
|||
$this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
|
||||
|
||||
// Mocking the failed CURL Response
|
||||
$IHTTPResultF = Mockery::mock(IHTTPResult::class);
|
||||
$IHTTPResultF = Mockery::mock(ICanHandleHttpResponses::class);
|
||||
$IHTTPResultF
|
||||
->shouldReceive('getReturnCode')
|
||||
->andReturn('404');
|
||||
|
||||
// Mocking the working CURL Response
|
||||
$IHTTPResultW = Mockery::mock(IHTTPResult::class);
|
||||
$IHTTPResultW = Mockery::mock(ICanHandleHttpResponses::class);
|
||||
$IHTTPResultW
|
||||
->shouldReceive('getReturnCode')
|
||||
->andReturn('204');
|
||||
|
||||
// Mocking the CURL Request
|
||||
$networkMock = Mockery::mock(IHTTPClient::class);
|
||||
$networkMock = Mockery::mock(ICanRequestPerHttp::class);
|
||||
$networkMock
|
||||
->shouldReceive('fetchFull')
|
||||
->with('https://test/install/testrewrite')
|
||||
|
@ -389,7 +389,7 @@ class InstallerTest extends MockedTest
|
|||
->andReturn($IHTTPResultW);
|
||||
|
||||
$this->dice->shouldReceive('create')
|
||||
->with(IHTTPClient::class)
|
||||
->with(ICanRequestPerHttp::class)
|
||||
->andReturn($networkMock);
|
||||
|
||||
DI::init($this->dice);
|
||||
|
|
|
@ -40,7 +40,7 @@ use Friendica\DI;
|
|||
use Friendica\Core\Config\Factory\Config;
|
||||
use Friendica\Core\Config\Repository;
|
||||
use Friendica\Core\Storage\Type;
|
||||
use Friendica\Network\HTTPClient;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientCan;
|
||||
use Friendica\Test\DatabaseTest;
|
||||
use Friendica\Test\Util\Database\StaticDatabase;
|
||||
use Friendica\Test\Util\VFSTrait;
|
||||
|
@ -61,7 +61,7 @@ class StorageManagerTest extends DatabaseTest
|
|||
private $logger;
|
||||
/** @var L10n */
|
||||
private $l10n;
|
||||
/** @var HTTPClient */
|
||||
/** @var HttpClientCan */
|
||||
private $httpRequest;
|
||||
|
||||
protected function setUp(): void
|
||||
|
@ -93,7 +93,7 @@ class StorageManagerTest extends DatabaseTest
|
|||
|
||||
$this->l10n = \Mockery::mock(L10n::class);
|
||||
|
||||
$this->httpRequest = \Mockery::mock(HTTPClient::class);
|
||||
$this->httpRequest = \Mockery::mock(HttpClientCan::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Network;
|
||||
namespace Friendica\Test\src\Network\HTTPClient\Client;
|
||||
|
||||
use Friendica\DI;
|
||||
use Friendica\Test\DiceHttpMockHandlerTrait;
|
|
@ -19,11 +19,11 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Test\src\Network;
|
||||
namespace Friendica\Test\src\Network\HTTPClient\Response;
|
||||
|
||||
use Dice\Dice;
|
||||
use Friendica\DI;
|
||||
use Friendica\Network\CurlResult;
|
||||
use Friendica\Network\HTTPClient\Response\CurlResult;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
@ -37,7 +37,7 @@ class CurlResultTest extends TestCase
|
|||
|
||||
/** @var Dice|MockInterface $dice */
|
||||
$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();
|
||||
$dice->shouldReceive('create')
|
||||
|
@ -52,12 +52,12 @@ class CurlResultTest extends TestCase
|
|||
*/
|
||||
public function testNormal()
|
||||
{
|
||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
||||
$headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
|
||||
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||
$headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
|
||||
$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,
|
||||
'content_type' => 'text/html; charset=utf-8',
|
||||
'url' => 'https://test.local'
|
||||
|
@ -80,12 +80,12 @@ class CurlResultTest extends TestCase
|
|||
*/
|
||||
public function testRedirect()
|
||||
{
|
||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
||||
$headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
|
||||
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||
$headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
|
||||
$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,
|
||||
'content_type' => 'text/html; charset=utf-8',
|
||||
'url' => 'https://test.local/test/it',
|
||||
|
@ -107,12 +107,12 @@ class CurlResultTest extends TestCase
|
|||
*/
|
||||
public function testTimeout()
|
||||
{
|
||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
||||
$headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
|
||||
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||
$headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
|
||||
$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,
|
||||
'content_type' => 'text/html; charset=utf-8',
|
||||
'url' => 'https://test.local/test/it',
|
||||
|
@ -136,9 +136,9 @@ class CurlResultTest extends TestCase
|
|||
*/
|
||||
public function testRedirectHeader()
|
||||
{
|
||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
|
||||
$headerArray = include(__DIR__ . '/../../datasets/curl/about.redirect.php');
|
||||
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.redirect');
|
||||
$headerArray = include(__DIR__ . '/../../../../datasets/curl/about.redirect.php');
|
||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||
|
||||
|
||||
$curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
|
||||
|
@ -162,10 +162,10 @@ class CurlResultTest extends TestCase
|
|||
*/
|
||||
public function testInHeader()
|
||||
{
|
||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
||||
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||
$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,
|
||||
'content_type' => 'text/html; charset=utf-8',
|
||||
'url' => 'https://test.local'
|
||||
|
@ -179,10 +179,10 @@ class CurlResultTest extends TestCase
|
|||
*/
|
||||
public function testGetHeaderArray()
|
||||
{
|
||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
||||
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||
$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,
|
||||
'content_type' => 'text/html; charset=utf-8',
|
||||
'url' => 'https://test.local'
|
||||
|
@ -199,8 +199,8 @@ class CurlResultTest extends TestCase
|
|||
*/
|
||||
public function testGetHeaderWithParam()
|
||||
{
|
||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
||||
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||
|
||||
$curlResult = new CurlResult('https://test.local', $header . $body, [
|
||||
'http_code' => 200,
|
Loading…
Reference in a new issue