3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Network\HTTPClient\Response;
24 use Friendica\Core\Logger;
25 use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
26 use GuzzleHttp\Psr7\Response;
27 use GuzzleHttp\RedirectMiddleware;
28 use Psr\Http\Message\ResponseInterface;
31 * A content wrapper class for Guzzle call results
33 class GuzzleResponse extends Response implements ICanHandleHttpResponses, ResponseInterface
35 /** @var string The URL */
42 * @var int the error number or 0 (zero) if no error
47 * @var string the error message or '' (the empty string) if no
52 private $redirectUrl = '';
54 private $isRedirectUrl = false;
56 public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '')
58 parent::__construct($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase());
60 $this->error = $error;
61 $this->errorNumber = $errorNumber;
63 $this->checkSuccess();
64 $this->checkRedirect($response);
67 private function checkSuccess()
69 $this->isSuccess = ($this->getStatusCode() >= 200 && $this->getStatusCode() <= 299) || $this->errorNumber == 0;
71 // Everything higher or equal 400 is not a success
72 if ($this->getReturnCode() >= 400) {
73 $this->isSuccess = false;
76 if (!$this->isSuccess) {
77 Logger::debug('debug', ['info' => $this->getHeaders()]);
80 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
81 $this->isTimeout = true;
83 $this->isTimeout = false;
87 private function checkRedirect(ResponseInterface $response)
89 $headersRedirect = $response->getHeader(RedirectMiddleware::HISTORY_HEADER) ?? [];
91 if (count($headersRedirect) > 0) {
92 $this->redirectUrl = $headersRedirect[0];
93 $this->isRedirectUrl = true;
98 public function getReturnCode(): string
100 return $this->getStatusCode();
104 public function getContentType(): string
106 $contentTypes = $this->getHeader('Content-Type') ?? [];
108 return array_pop($contentTypes) ?? '';
112 public function inHeader(string $field): bool
114 return $this->hasHeader($field);
118 public function getHeaderArray(): array
120 return $this->getHeaders();
124 public function isSuccess(): bool
126 return $this->isSuccess;
130 public function getUrl(): string
136 public function getRedirectUrl(): string
138 return $this->redirectUrl;
143 public function isRedirectUrl(): bool
145 return $this->isRedirectUrl;
149 public function getErrorNumber(): int
151 return $this->errorNumber;
155 public function getError(): string
161 public function isTimeout(): bool
163 return $this->isTimeout;
166 /// @todo - fix mismatching use of "getBody()" as string here and parent "getBody()" as streaminterface
167 public function getBody(): string
169 return (string) parent::getBody();