3 namespace Friendica\Network;
5 use Friendica\Core\Logger;
6 use Friendica\Network\HTTPException\InternalServerErrorException;
7 use Friendica\Util\Network;
10 * A content class for Curl call results
15 * @var int HTTP return code or 0 if timeout or failure
20 * @var string the content type of the Curl call
25 * @var string the HTTP headers of the Curl call
30 * @var array the HTTP headers of the Curl call
32 private $header_fields;
35 * @var boolean true (if HTTP 2xx result) or false
40 * @var string the URL which was called
45 * @var string in case of redirect, content was finally retrieved from this URL
50 * @var string fetched content
55 * @var array some informations about the fetched data
60 * @var boolean true if the URL has a redirect
62 private $isRedirectUrl;
65 * @var boolean true if the curl request timed out
70 * @var int the error number or 0 (zero) if no error
75 * @var string the error message or '' (the empty string) if no
80 * Creates an errored CURL response
82 * @param string $url optional URL
84 * @return CurlResult a CURL with error response
85 * @throws InternalServerErrorException
87 public static function createErrorCurl($url = '')
89 return new CurlResult($url, '', ['http_code' => 0]);
94 * @param string $url the URL which was called
95 * @param string $result the result of the curl execution
96 * @param array $info an additional info array
97 * @param int $errorNumber the error number or 0 (zero) if no error
98 * @param string $error the error message or '' (the empty string) if no
100 * @throws InternalServerErrorException when HTTP code of the CURL response is missing
102 public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
104 if (!array_key_exists('http_code', $info)) {
105 throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
108 $this->returnCode = $info['http_code'];
111 $this->errorNumber = $errorNumber;
112 $this->error = $error;
114 Logger::log($url . ': ' . $this->returnCode . " " . $result, Logger::DATA);
116 $this->parseBodyHeader($result);
117 $this->checkSuccess();
118 $this->checkRedirect();
122 private function parseBodyHeader($result)
124 // Pull out multiple headers, e.g. proxy and continuation headers
125 // allow for HTTP/2.x without fixing code
129 while (preg_match('/^HTTP\/.+? \d+/', $base)) {
130 $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
132 $base = substr($base, strlen($chunk));
135 $this->body = substr($result, strlen($header));
136 $this->header = $header;
139 private function checkSuccess()
141 $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
143 // Everything higher or equal 400 is not a success
144 if ($this->returnCode >= 400) {
145 $this->isSuccess = false;
148 if (!$this->isSuccess) {
149 Logger::log('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, Logger::INFO);
150 Logger::log('debug: ' . print_r($this->info, true), Logger::DATA);
153 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
154 $this->isTimeout = true;
156 $this->isTimeout = false;
160 private function checkRedirect()
162 if (!array_key_exists('url', $this->info)) {
163 $this->redirectUrl = '';
165 $this->redirectUrl = $this->info['url'];
168 if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
169 $redirect_parts = parse_url(defaults($this->info, 'redirect_url', ''));
170 if (empty($redirect_parts)) {
171 $redirect_parts = [];
174 if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
175 $redirect_parts2 = parse_url(trim(array_pop($matches)));
176 if (!empty($redirect_parts2)) {
177 $redirect_parts = array_merge($redirect_parts, $redirect_parts2);
181 $parts = parse_url(defaults($this->info, 'url', ''));
186 /// @todo Checking the corresponding RFC which parts of a redirect can be ommitted.
187 $components = ['scheme', 'host', 'path', 'query', 'fragment'];
188 foreach ($components as $component) {
189 if (empty($redirect_parts[$component]) && !empty($parts[$component])) {
190 $redirect_parts[$component] = $parts[$component];
194 $this->redirectUrl = Network::unparseURL($redirect_parts);
196 $this->isRedirectUrl = true;
198 $this->isRedirectUrl = false;
202 private function checkInfo()
204 if (isset($this->info['content_type'])) {
205 $this->contentType = $this->info['content_type'];
207 $this->contentType = '';
214 * @return string The Curl Code
216 public function getReturnCode()
218 return $this->returnCode;
222 * Returns the Curl Content Type
224 * @return string the Curl Content Type
226 public function getContentType()
228 return $this->contentType;
232 * Returns the Curl headers
234 * @param string $field optional header field. Return all fields if empty
236 * @return string the Curl headers or the specified content of the header variable
238 public function getHeader(string $field = '')
241 return $this->header;
244 $field = strtolower(trim($field));
246 $headers = $this->getHeaderArray();
248 if (isset($headers[$field])) {
249 return $headers[$field];
254 * Check if a specified header exists
256 * @param string $field header field
258 * @return boolean "true" if header exists
260 public function headerExists(string $field)
262 $field = strtolower(trim($field));
264 $headers = $this->getHeaderArray();
266 return array_key_exists($field, $headers);
270 * Returns the Curl headers as an associated array
272 * @return array associated header array
274 public function getHeaderArray()
276 if (!empty($this->header_fields)) {
277 return $this->header_fields;
280 $this->header_fields = [];
282 $lines = explode("\n", trim($this->header));
283 foreach ($lines as $line) {
284 $parts = explode(':', $line);
285 $headerfield = strtolower(trim(array_shift($parts)));
286 $headerdata = trim(implode(':', $parts));
287 $this->header_fields[$headerfield] = $headerdata;
290 return $this->header_fields;
296 public function isSuccess()
298 return $this->isSuccess;
304 public function getUrl()
312 public function getRedirectUrl()
314 return $this->redirectUrl;
320 public function getBody()
328 public function getInfo()
336 public function isRedirectUrl()
338 return $this->isRedirectUrl;
344 public function getErrorNumber()
346 return $this->errorNumber;
352 public function getError()
360 public function isTimeout()
362 return $this->isTimeout;