Merge remote-tracking branch 'upstream/develop' into error-handling

This commit is contained in:
Michael 2021-10-31 05:25:39 +00:00
commit 516018861e
235 changed files with 10885 additions and 10716 deletions

View file

@ -0,0 +1,105 @@
<?php
namespace Friendica\Network\HTTPClient\Capability;
use Psr\Http\Message\MessageInterface;
/**
* Temporary class to map Friendica used variables based on PSR-7 HTTPResponse
*/
interface ICanHandleHttpResponses
{
/**
* Gets the Return Code
*
* @return string The Return Code
*/
public function getReturnCode();
/**
* Returns the Content Type
*
* @return string the Content Type
*/
public function getContentType();
/**
* Returns the headers
*
* @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(string $header);
/**
* Returns all headers
* @see MessageInterface::getHeaders()
*
* @return string[][]
*/
public function getHeaders();
/**
* Check if a specified header exists
* @see MessageInterface::hasHeader()
*
* @param string $field header field
*
* @return boolean "true" if header exists
*/
public function inHeader(string $field);
/**
* Returns the headers as an associated array
* @see MessageInterface::getHeaders()
* @deprecated
*
* @return string[][] associated header array
*/
public function getHeaderArray();
/**
* @return bool
*/
public function isSuccess();
/**
* @return string
*/
public function getUrl();
/**
* @return string
*/
public function getRedirectUrl();
/**
* @see MessageInterface::getBody()
*
* @return string
*/
public function getBody();
/**
* @return boolean
*/
public function isRedirectUrl();
/**
* @return integer
*/
public function getErrorNumber();
/**
* @return string
*/
public function getError();
/**
* @return boolean
*/
public function isTimeout();
}

View file

@ -0,0 +1,133 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU APGL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Network\HTTPClient\Capability;
use GuzzleHttp\Exception\TransferException;
/**
* Interface for calling HTTP requests and returning their responses
*/
interface ICanSendHttpRequests
{
/**
* Fetches the content of an URL
*
* Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt")
* to preserve cookies from one request to the next.
*
* @param string $url URL to fetch
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
* @param string $accept_content supply Accept: header with 'accept_content' as the value
* @param string $cookiejar Path to cookie jar file
*
* @return string The fetched content
*/
public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string;
/**
* Fetches the whole response of an URL.
*
* Inner workings and parameters are the same as @ref fetchUrl but returns an array with
* all the information collected during the fetch.
*
* @param string $url URL to fetch
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
* @param string $accept_content supply Accept: header with 'accept_content' as the value
* @param string $cookiejar Path to cookie jar file
*
* @return ICanHandleHttpResponses With all relevant information, 'body' contains the actual fetched content.
*/
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses;
/**
* Send a HEAD to a URL.
*
* @param string $url URL to fetch
* @param array $opts (optional parameters) associative array with:
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
* 'cookiejar' => path to cookie jar file
* 'header' => header array
*
* @return ICanHandleHttpResponses
*/
public function head(string $url, array $opts = []): ICanHandleHttpResponses;
/**
* Send a GET to an URL.
*
* @param string $url URL to fetch
* @param array $opts (optional parameters) associative array with:
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
* 'cookiejar' => path to cookie jar file
* 'header' => header array
* 'content_length' => int maximum File content length
*
* @return ICanHandleHttpResponses
*/
public function get(string $url, array $opts = []): ICanHandleHttpResponses;
/**
* Sends a HTTP request to a given url
*
* @param string $method A HTTP request
* @param string $url Url to send to
* @param array $opts (optional parameters) associative array with:
* 'body' => (mixed) setting the body for sending data
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
* 'cookiejar' => path to cookie jar file
* 'header' => header array
* 'content_length' => int maximum File content length
* 'auth' => array authentication settings
*
* @return ICanHandleHttpResponses
*/
public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses;
/**
* Send POST request to an URL
*
* @param string $url URL to post
* @param mixed $params array of POST variables
* @param array $headers HTTP headers
* @param int $timeout The timeout in seconds, default system config value or 60 seconds
*
* @return ICanHandleHttpResponses The content
*/
public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses;
/**
* Returns the original URL of the provided URL
*
* This function strips tracking query params and follows redirections, either
* through HTTP code or meta refresh tags. Stops after 10 redirections.
*
* @param string $url A user-submitted URL
*
* @return string A canonical URL
*
* @throws TransferException In case there's an error during the resolving
*/
public function finalUrl(string $url): string;
}

View file

@ -0,0 +1,258 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU APGL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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\ICanSendHttpRequests;
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\Network;
use Friendica\Util\Profiler;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\FileCookieJar;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\RequestOptions;
use mattwright\URLResolver;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
/**
* Performs HTTP requests to a given URL
*/
class HttpClient implements ICanSendHttpRequests
{
/** @var LoggerInterface */
private $logger;
/** @var Profiler */
private $profiler;
/** @var Client */
private $client;
/** @var URLResolver */
private $resolver;
public function __construct(LoggerInterface $logger, Profiler $profiler, Client $client, URLResolver $resolver)
{
$this->logger = $logger;
$this->profiler = $profiler;
$this->client = $client;
$this->resolver = $resolver;
}
/**
* {@inheritDoc}
*/
public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses
{
$this->profiler->startRecording('network');
$this->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
if (Network::isLocalLink($url)) {
$this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
}
if (strlen($url) > 1000) {
$this->logger->debug('URL is longer than 1000 characters.', ['url' => $url, 'callstack' => System::callstack(20)]);
$this->profiler->stopRecording();
return CurlResult::createErrorCurl(substr($url, 0, 200));
}
$parts2 = [];
$parts = parse_url($url);
$path_parts = explode('/', $parts['path'] ?? '');
foreach ($path_parts as $part) {
if (strlen($part) <> mb_strlen($part)) {
$parts2[] = rawurlencode($part);
} else {
$parts2[] = $part;
}
}
$parts['path'] = implode('/', $parts2);
$url = Network::unparseURL($parts);
if (Network::isUrlBlocked($url)) {
$this->logger->info('Domain is blocked.', ['url' => $url]);
$this->profiler->stopRecording();
return CurlResult::createErrorCurl($url);
}
$conf = [];
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::LEGACY_HEADER])) {
$this->logger->notice('Wrong option \'headers\' used.');
$headers = array_merge($opts[HttpClientOptions::LEGACY_HEADER], $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::BODY])) {
$conf[RequestOptions::BODY] = $opts[HttpClientOptions::BODY];
}
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]) {
throw new TransferException('The file is too big!');
}
};
try {
$this->logger->debug('http request config.', ['url' => $url, 'method' => $method, 'options' => $conf]);
$response = $this->client->request($method, $url, $conf);
return new GuzzleResponse($response, $url);
} catch (TransferException $exception) {
if ($exception instanceof RequestException &&
$exception->hasResponse()) {
return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), '');
} else {
return new CurlResult($url, '', ['http_code' => 500], $exception->getCode(), '');
}
} catch (InvalidArgumentException | \InvalidArgumentException $argumentException) {
$this->logger->info('Invalid Argument for HTTP call.', ['url' => $url, 'method' => $method, 'exception' => $argumentException]);
return new CurlResult($url, '', ['http_code' => 500], $argumentException->getCode(), $argumentException->getMessage());
} finally {
$this->logger->debug('Request stop.', ['url' => $url, 'method' => $method]);
$this->profiler->stopRecording();
}
}
/** {@inheritDoc}
*/
public function head(string $url, array $opts = []): ICanHandleHttpResponses
{
return $this->request('head', $url, $opts);
}
/**
* {@inheritDoc}
*/
public function get(string $url, array $opts = []): ICanHandleHttpResponses
{
return $this->request('get', $url, $opts);
}
/**
* {@inheritDoc}
*/
public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses
{
$opts = [];
$opts[HttpClientOptions::BODY] = $params;
if (!empty($headers)) {
$opts[HttpClientOptions::HEADERS] = $headers;
}
if (!empty($timeout)) {
$opts[HttpClientOptions::TIMEOUT] = $timeout;
}
return $this->request('post', $url, $opts);
}
/**
* {@inheritDoc}
*/
public function finalUrl(string $url): string
{
$this->profiler->startRecording('network');
if (Network::isLocalLink($url)) {
$this->logger->debug('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
}
if (Network::isUrlBlocked($url)) {
$this->logger->info('Domain is blocked.', ['url' => $url]);
return $url;
}
if (Network::isRedirectBlocked($url)) {
$this->logger->info('Domain should not be redirected.', ['url' => $url]);
return $url;
}
$url = Network::stripTrackingQueryParams($url);
$url = trim($url, "'");
$urlResult = $this->resolver->resolveURL($url);
if ($urlResult->didErrorOccur()) {
throw new TransferException($urlResult->getErrorMessageString(), $urlResult->getHTTPStatusCode());
}
return $urlResult->getURL();
}
/**
* {@inheritDoc}
*/
public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string
{
$ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar);
return $ret->getBody();
}
/**
* {@inheritDoc}
*/
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses
{
return $this->get(
$url,
[
'timeout' => $timeout,
'accept_content' => $accept_content,
'cookiejar' => $cookiejar
]
);
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Friendica\Network\HTTPClient\Client;
use GuzzleHttp\RequestOptions;
/**
* This class contains a list of possible HTTPClient request options.
*/
class HttpClientOptions
{
/**
* accept_content: (array) supply Accept: header with 'accept_content' as the value
*/
const ACCEPT_CONTENT = 'accept_content';
/**
* timeout: (int) out in seconds, default system config value or 60 seconds
*/
const TIMEOUT = RequestOptions::TIMEOUT;
/**
* cookiejar: (string) path to cookie jar file
*/
const COOKIEJAR = 'cookiejar';
/**
* headers: (array) header array
*/
const HEADERS = RequestOptions::HEADERS;
/**
* header: (array) header array (legacy version)
*/
const LEGACY_HEADER = 'header';
/**
* content_length: (int) maximum File content length
*/
const CONTENT_LENGTH = 'content_length';
/**
* body: (mixed) Setting the body for sending data
*/
const BODY = RequestOptions::BODY;
/**
* auth: (array) Authentication settings for specific requests
*/
const AUTH = RequestOptions::AUTH;
}

View file

@ -0,0 +1,113 @@
<?php
namespace Friendica\Network\HTTPClient\Factory;
use Friendica\App;
use Friendica\BaseFactory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Network\HTTPClient\Client;
use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
use Friendica\Util\Profiler;
use Friendica\Util\Strings;
use GuzzleHttp;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\RequestOptions;
use mattwright\URLResolver;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
require_once __DIR__ . '/../../../../static/dbstructure.config.php';
class HttpClient extends BaseFactory
{
/** @var IManageConfigValues */
private $config;
/** @var Profiler */
private $profiler;
/** @var App\BaseURL */
private $baseUrl;
public function __construct(LoggerInterface $logger, IManageConfigValues $config, Profiler $profiler, App\BaseURL $baseUrl)
{
parent::__construct($logger);
$this->config = $config;
$this->profiler = $profiler;
$this->baseUrl = $baseUrl;
}
/**
* Creates a IHTTPClient for communications with HTTP endpoints
*
* @param HandlerStack|null $handlerStack (optional) A handler replacement (just usefull at test environments)
*
* @return ICanSendHttpRequests
*/
public function createClient(HandlerStack $handlerStack = null): ICanSendHttpRequests
{
$proxy = $this->config->get('system', 'proxy');
if (!empty($proxy)) {
$proxyUser = $this->config->get('system', 'proxyuser');
if (!empty($proxyUser)) {
$proxy = $proxyUser . '@' . $proxy;
}
}
$logger = $this->logger;
$onRedirect = function (
RequestInterface $request,
ResponseInterface $response,
UriInterface $uri
) use ($logger) {
$logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri, 'method' => $request->getMethod()]);
};
$userAgent = FRIENDICA_PLATFORM . " '" .
FRIENDICA_CODENAME . "' " .
FRIENDICA_VERSION . '-' .
DB_UPDATE_VERSION . '; ' .
$this->baseUrl->get();
$guzzle = new GuzzleHttp\Client([
RequestOptions::ALLOW_REDIRECTS => [
'max' => 8,
'on_redirect' => $onRedirect,
'track_redirect' => true,
'strict' => true,
'referer' => true,
],
RequestOptions::HTTP_ERRORS => false,
// Without this setting it seems as if some webservers send compressed content
// This seems to confuse curl so that it shows this uncompressed.
/// @todo We could possibly set this value to "gzip" or something similar
RequestOptions::DECODE_CONTENT => '',
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,
// but it can be overridden
RequestOptions::VERIFY => (bool)$this->config->get('system', 'verifyssl'),
RequestOptions::PROXY => $proxy,
RequestOptions::HEADERS => [
'User-Agent' => $userAgent,
],
'handler' => $handlerStack ?? HandlerStack::create(),
]);
$resolver = new URLResolver();
$resolver->setUserAgent($userAgent);
$resolver->setMaxRedirects(10);
$resolver->setRequestTimeout(10);
// if the file is too large then exit
$resolver->setMaxResponseDataSize(1000000);
// Designate a temporary file that will store cookies during the session.
// Some websites test the browser for cookie support, so this enhances results.
$resolver->setCookieJar(get_temppath() .'/resolver-cookie-' . Strings::getRandomName(10));
return new Client\HttpClient($logger, $this->profiler, $guzzle, $resolver);
}
}

View file

@ -0,0 +1,349 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Network\HTTPClient\Response;
use Friendica\Core\Logger;
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Network\HTTPException\UnprocessableEntityException;
use Friendica\Util\Network;
/**
* A content class for Curl call results
*/
class CurlResult implements ICanHandleHttpResponses
{
/**
* @var int HTTP return code or 0 if timeout or failure
*/
private $returnCode;
/**
* @var string the content type of the Curl call
*/
private $contentType;
/**
* @var string the HTTP headers of the Curl call
*/
private $header;
/**
* @var array the HTTP headers of the Curl call
*/
private $header_fields;
/**
* @var boolean true (if HTTP 2xx result) or false
*/
private $isSuccess;
/**
* @var string the URL which was called
*/
private $url;
/**
* @var string in case of redirect, content was finally retrieved from this URL
*/
private $redirectUrl;
/**
* @var string fetched content
*/
private $body;
/**
* @var array some informations about the fetched data
*/
private $info;
/**
* @var boolean true if the URL has a redirect
*/
private $isRedirectUrl;
/**
* @var boolean true if the curl request timed out
*/
private $isTimeout;
/**
* @var int the error number or 0 (zero) if no error
*/
private $errorNumber;
/**
* @var string the error message or '' (the empty string) if no
*/
private $error;
/**
* Creates an errored CURL response
*
* @param string $url optional URL
*
* @return ICanHandleHttpResponses a CURL with error response
* @throws UnprocessableEntityException
*/
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
*
* @throws UnprocessableEntityException when HTTP code of the CURL response is missing
*/
public function __construct(string $url, string $result, array $info, int $errorNumber = 0, string $error = '')
{
if (!array_key_exists('http_code', $info)) {
throw new UnprocessableEntityException('CURL response doesn\'t contains a response HTTP code');
}
$this->returnCode = $info['http_code'];
$this->url = $url;
$this->info = $info;
$this->errorNumber = $errorNumber;
$this->error = $error;
Logger::debug('construct', ['url' => $url, 'returncode' => $this->returnCode, 'result' => $result]);
$this->parseBodyHeader($result);
$this->checkSuccess();
$this->checkRedirect();
$this->checkInfo();
}
private function parseBodyHeader($result)
{
// Pull out multiple headers, e.g. proxy and continuation headers
// allow for HTTP/2.x without fixing code
$header = '';
$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->header_fields = []; // Is filled on demand
}
private function checkSuccess()
{
$this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
// Everything higher or equal 400 is not a success
if ($this->returnCode >= 400) {
$this->isSuccess = false;
}
if (!$this->isSuccess) {
Logger::debug('debug', ['info' => $this->info]);
}
if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
$this->isTimeout = true;
} else {
$this->isTimeout = false;
}
}
private function checkRedirect()
{
if (!array_key_exists('url', $this->info)) {
$this->redirectUrl = '';
} else {
$this->redirectUrl = $this->info['url'];
}
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 = [];
}
if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
$redirect_parts2 = parse_url(trim(array_pop($matches)));
if (!empty($redirect_parts2)) {
$redirect_parts = array_merge($redirect_parts, $redirect_parts2);
}
}
$parts = parse_url($this->info['url'] ?? '');
if (empty($parts)) {
$parts = [];
}
/// @todo Checking the corresponding RFC which parts of a redirect can be ommitted.
$components = ['scheme', 'host', 'path', 'query', 'fragment'];
foreach ($components as $component) {
if (empty($redirect_parts[$component]) && !empty($parts[$component])) {
$redirect_parts[$component] = $parts[$component];
}
}
$this->redirectUrl = Network::unparseURL($redirect_parts);
$this->isRedirectUrl = true;
} else {
$this->isRedirectUrl = false;
}
}
private function checkInfo()
{
if (isset($this->info['content_type'])) {
$this->contentType = $this->info['content_type'];
} else {
$this->contentType = '';
}
}
/** {@inheritDoc} */
public function getReturnCode(): string
{
return $this->returnCode;
}
/** {@inheritDoc} */
public function getContentType(): string
{
return $this->contentType;
}
/** {@inheritDoc} */
public function getHeader(string $header): array
{
if (empty($header)) {
return [];
}
$header = strtolower(trim($header));
$headers = $this->getHeaderArray();
if (isset($headers[$header])) {
return $headers[$header];
}
return [];
}
/** {@inheritDoc} */
public function getHeaders(): array
{
return $this->getHeaderArray();
}
/** {@inheritDoc} */
public function inHeader(string $field): bool
{
$field = strtolower(trim($field));
$headers = $this->getHeaderArray();
return array_key_exists($field, $headers);
}
/** {@inheritDoc} */
public function getHeaderArray(): array
{
if (!empty($this->header_fields)) {
return $this->header_fields;
}
$this->header_fields = [];
$lines = explode("\n", trim($this->header));
foreach ($lines as $line) {
$parts = explode(':', $line);
$headerfield = strtolower(trim(array_shift($parts)));
$headerdata = trim(implode(':', $parts));
if (empty($this->header_fields[$headerfield])) {
$this->header_fields[$headerfield] = [$headerdata];
} elseif (!in_array($headerdata, $this->header_fields[$headerfield])) {
$this->header_fields[$headerfield][] = $headerdata;
}
}
return $this->header_fields;
}
/** {@inheritDoc} */
public function isSuccess(): bool
{
return $this->isSuccess;
}
/** {@inheritDoc} */
public function getUrl(): string
{
return $this->url;
}
/** {@inheritDoc} */
public function getRedirectUrl(): string
{
return $this->redirectUrl;
}
/** {@inheritDoc} */
public function getBody(): string
{
return $this->body;
}
/** {@inheritDoc} */
public function isRedirectUrl(): bool
{
return $this->isRedirectUrl;
}
/** {@inheritDoc} */
public function getErrorNumber(): int
{
return $this->errorNumber;
}
/** {@inheritDoc} */
public function getError(): string
{
return $this->error;
}
/** {@inheritDoc} */
public function isTimeout(): bool
{
return $this->isTimeout;
}
}

View file

@ -0,0 +1,157 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Network\HTTPClient\Response;
use Friendica\Core\Logger;
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use Friendica\Network\HTTPException\NotImplementedException;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
/**
* A content wrapper class for Guzzle call results
*/
class GuzzleResponse extends Response implements ICanHandleHttpResponses, ResponseInterface
{
/** @var string The URL */
private $url;
/** @var boolean */
private $isTimeout;
/** @var boolean */
private $isSuccess;
/**
* @var int the error number or 0 (zero) if no error
*/
private $errorNumber;
/**
* @var string the error message or '' (the empty string) if no
*/
private $error;
public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '')
{
parent::__construct($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase());
$this->url = $url;
$this->error = $error;
$this->errorNumber = $errorNumber;
$this->checkSuccess();
}
private function checkSuccess()
{
$this->isSuccess = ($this->getStatusCode() >= 200 && $this->getStatusCode() <= 299) || $this->errorNumber == 0;
// Everything higher or equal 400 is not a success
if ($this->getReturnCode() >= 400) {
$this->isSuccess = false;
}
if (!$this->isSuccess) {
Logger::debug('debug', ['info' => $this->getHeaders()]);
}
if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
$this->isTimeout = true;
} else {
$this->isTimeout = false;
}
}
/** {@inheritDoc} */
public function getReturnCode(): string
{
return $this->getStatusCode();
}
/** {@inheritDoc} */
public function getContentType(): string
{
$contentTypes = $this->getHeader('Content-Type') ?? [];
return array_pop($contentTypes) ?? '';
}
/** {@inheritDoc} */
public function inHeader(string $field): bool
{
return $this->hasHeader($field);
}
/** {@inheritDoc} */
public function getHeaderArray(): array
{
return $this->getHeaders();
}
/** {@inheritDoc} */
public function isSuccess(): bool
{
return $this->isSuccess;
}
/** {@inheritDoc} */
public function getUrl(): string
{
return $this->url;
}
/** {@inheritDoc} */
public function getRedirectUrl(): string
{
return $this->url;
}
/** {@inheritDoc}
*
* @throws NotImplementedException
*/
public function isRedirectUrl(): bool
{
throw new NotImplementedException();
}
/** {@inheritDoc} */
public function getErrorNumber(): int
{
return $this->errorNumber;
}
/** {@inheritDoc} */
public function getError(): string
{
return $this->error;
}
/** {@inheritDoc} */
public function isTimeout(): bool
{
return $this->isTimeout;
}
/// @todo - fix mismatching use of "getBody()" as string here and parent "getBody()" as streaminterface
public function getBody(): string
{
return (string) parent::getBody();
}
}