2 * @copyright Copyright (C) 2020, Friendica
4 * @license GNU AGPL version 3 or any later version
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 use Friendica\Core\Logger;
22 use Friendica\Network\HTTPException\InternalServerErrorException;
23 use Friendica\Util\Network;
26 * A content class for Curl call results
31 * @var int HTTP return code or 0 if timeout or failure
36 * @var string the content type of the Curl call
41 * @var string the HTTP headers of the Curl call
46 * @var array the HTTP headers of the Curl call
48 private $header_fields;
51 * @var boolean true (if HTTP 2xx result) or false
56 * @var string the URL which was called
61 * @var string in case of redirect, content was finally retrieved from this URL
66 * @var string fetched content
71 * @var array some informations about the fetched data
76 * @var boolean true if the URL has a redirect
78 private $isRedirectUrl;
81 * @var boolean true if the curl request timed out
86 * @var int the error number or 0 (zero) if no error
91 * @var string the error message or '' (the empty string) if no
96 * Creates an errored CURL response
98 * @param string $url optional URL
100 * @return CurlResult a CURL with error response
101 * @throws InternalServerErrorException
103 public static function createErrorCurl($url = '')
105 return new CurlResult($url, '', ['http_code' => 0]);
110 * @param string $url the URL which was called
111 * @param string $result the result of the curl execution
112 * @param array $info an additional info array
113 * @param int $errorNumber the error number or 0 (zero) if no error
114 * @param string $error the error message or '' (the empty string) if no
116 * @throws InternalServerErrorException when HTTP code of the CURL response is missing
118 public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
120 if (!array_key_exists('http_code', $info)) {
121 throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
124 $this->returnCode = $info['http_code'];
127 $this->errorNumber = $errorNumber;
128 $this->error = $error;
130 Logger::log($url . ': ' . $this->returnCode . " " . $result, Logger::DATA);
132 $this->parseBodyHeader($result);
133 $this->checkSuccess();
134 $this->checkRedirect();
138 private function parseBodyHeader($result)
140 // Pull out multiple headers, e.g. proxy and continuation headers
141 // allow for HTTP/2.x without fixing code
145 while (preg_match('/^HTTP\/.+? \d+/', $base)) {
146 $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
148 $base = substr($base, strlen($chunk));
151 $this->body = substr($result, strlen($header));
152 $this->header = $header;
153 $this->header_fields = []; // Is filled on demand
156 private function checkSuccess()
158 $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
160 // Everything higher or equal 400 is not a success
161 if ($this->returnCode >= 400) {
162 $this->isSuccess = false;
165 if (!$this->isSuccess) {
166 Logger::log('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, Logger::INFO);
167 Logger::log('debug: ' . print_r($this->info, true), Logger::DATA);
170 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
171 $this->isTimeout = true;
173 $this->isTimeout = false;
177 private function checkRedirect()
179 if (!array_key_exists('url', $this->info)) {
180 $this->redirectUrl = '';
182 $this->redirectUrl = $this->info['url'];
185 if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
186 $redirect_parts = parse_url($this->info['redirect_url'] ?? '');
187 if (empty($redirect_parts)) {
188 $redirect_parts = [];
191 if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
192 $redirect_parts2 = parse_url(trim(array_pop($matches)));
193 if (!empty($redirect_parts2)) {
194 $redirect_parts = array_merge($redirect_parts, $redirect_parts2);
198 $parts = parse_url($this->info['url'] ?? '');
203 /// @todo Checking the corresponding RFC which parts of a redirect can be ommitted.
204 $components = ['scheme', 'host', 'path', 'query', 'fragment'];
205 foreach ($components as $component) {
206 if (empty($redirect_parts[$component]) && !empty($parts[$component])) {
207 $redirect_parts[$component] = $parts[$component];
211 $this->redirectUrl = Network::unparseURL($redirect_parts);
213 $this->isRedirectUrl = true;
215 $this->isRedirectUrl = false;
219 private function checkInfo()
221 if (isset($this->info['content_type'])) {
222 $this->contentType = $this->info['content_type'];
224 $this->contentType = '';
231 * @return string The Curl Code
233 public function getReturnCode()
235 return $this->returnCode;
239 * Returns the Curl Content Type
241 * @return string the Curl Content Type
243 public function getContentType()
245 return $this->contentType;
249 * Returns the Curl headers
251 * @param string $field optional header field. Return all fields if empty
253 * @return string the Curl headers or the specified content of the header variable
255 public function getHeader(string $field = '')
258 return $this->header;
261 $field = strtolower(trim($field));
263 $headers = $this->getHeaderArray();
265 if (isset($headers[$field])) {
266 return $headers[$field];
273 * Check if a specified header exists
275 * @param string $field header field
277 * @return boolean "true" if header exists
279 public function inHeader(string $field)
281 $field = strtolower(trim($field));
283 $headers = $this->getHeaderArray();
285 return array_key_exists($field, $headers);
289 * Returns the Curl headers as an associated array
291 * @return array associated header array
293 public function getHeaderArray()
295 if (!empty($this->header_fields)) {
296 return $this->header_fields;
299 $this->header_fields = [];
301 $lines = explode("\n", trim($this->header));
302 foreach ($lines as $line) {
303 $parts = explode(':', $line);
304 $headerfield = strtolower(trim(array_shift($parts)));
305 $headerdata = trim(implode(':', $parts));
306 $this->header_fields[$headerfield] = $headerdata;
309 return $this->header_fields;
315 public function isSuccess()
317 return $this->isSuccess;
323 public function getUrl()
331 public function getRedirectUrl()
333 return $this->redirectUrl;
339 public function getBody()
347 public function getInfo()
355 public function isRedirectUrl()
357 return $this->isRedirectUrl;
363 public function getErrorNumber()
365 return $this->errorNumber;
371 public function getError()
379 public function isTimeout()
381 return $this->isTimeout;