friendica/src/Network/HTTPClient/Response/GuzzleResponse.php

172 lines
4.1 KiB
PHP
Raw Normal View History

2021-08-20 19:48:14 +02:00
<?php
/**
2022-01-02 10:49:50 +01:00
* @copyright Copyright (C) 2010-2022, the Friendica project
2021-08-20 19:48:14 +02:00
*
* @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;
2021-08-20 19:48:14 +02:00
use Friendica\Core\Logger;
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
2021-08-20 19:48:14 +02:00
use GuzzleHttp\Psr7\Response;
2021-12-09 14:59:25 +01:00
use GuzzleHttp\RedirectMiddleware;
2021-08-20 19:48:14 +02:00
use Psr\Http\Message\ResponseInterface;
/**
* A content wrapper class for Guzzle call results
*/
class GuzzleResponse extends Response implements ICanHandleHttpResponses, ResponseInterface
2021-08-20 19:48:14 +02:00
{
/** @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;
2021-12-09 14:59:25 +01:00
/** @var string */
private $redirectUrl = '';
/** @var bool */
private $isRedirectUrl = false;
2021-08-20 19:48:14 +02:00
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();
2021-12-09 14:59:25 +01:00
$this->checkRedirect($response);
2021-08-20 19:48:14 +02:00
}
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;
}
}
2021-12-09 14:59:25 +01:00
private function checkRedirect(ResponseInterface $response)
{
$headersRedirect = $response->getHeader(RedirectMiddleware::HISTORY_HEADER) ?? [];
if (count($headersRedirect) > 0) {
$this->redirectUrl = $headersRedirect[0];
$this->isRedirectUrl = true;
}
}
2021-08-20 19:48:14 +02:00
/** {@inheritDoc} */
public function getReturnCode(): string
2021-08-20 19:48:14 +02:00
{
return $this->getStatusCode();
}
/** {@inheritDoc} */
public function getContentType(): string
2021-08-20 19:48:14 +02:00
{
$contentTypes = $this->getHeader('Content-Type') ?? [];
2021-08-20 19:48:23 +02:00
return array_pop($contentTypes) ?? '';
2021-08-20 19:48:14 +02:00
}
/** {@inheritDoc} */
public function inHeader(string $field): bool
2021-08-20 19:48:14 +02:00
{
return $this->hasHeader($field);
}
/** {@inheritDoc} */
public function getHeaderArray(): array
2021-08-20 19:48:14 +02:00
{
return $this->getHeaders();
}
/** {@inheritDoc} */
public function isSuccess(): bool
2021-08-20 19:48:14 +02:00
{
return $this->isSuccess;
}
/** {@inheritDoc} */
public function getUrl(): string
2021-08-20 19:48:14 +02:00
{
return $this->url;
}
/** {@inheritDoc} */
public function getRedirectUrl(): string
2021-08-20 19:48:14 +02:00
{
2021-12-09 14:59:25 +01:00
return $this->redirectUrl;
2021-08-20 19:48:14 +02:00
}
/** {@inheritDoc}
*/
public function isRedirectUrl(): bool
2021-08-20 19:48:14 +02:00
{
2021-12-09 14:59:25 +01:00
return $this->isRedirectUrl;
2021-08-20 19:48:14 +02:00
}
/** {@inheritDoc} */
public function getErrorNumber(): int
2021-08-20 19:48:14 +02:00
{
return $this->errorNumber;
}
/** {@inheritDoc} */
public function getError(): string
2021-08-20 19:48:14 +02:00
{
return $this->error;
}
/** {@inheritDoc} */
public function isTimeout(): bool
2021-08-20 19:48:14 +02:00
{
return $this->isTimeout;
}
2021-08-20 20:05:41 +02:00
/// @todo - fix mismatching use of "getBody()" as string here and parent "getBody()" as streaminterface
public function getBody(): string
2021-08-20 20:05:41 +02:00
{
return (string) parent::getBody();
2021-08-20 20:05:41 +02:00
}
2021-08-20 19:48:14 +02:00
}