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;
137 $this->header_fields = []; // Is filled on demand
140 private function checkSuccess()
142 $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
144 // Everything higher or equal 400 is not a success
145 if ($this->returnCode >= 400) {
146 $this->isSuccess = false;
149 if (!$this->isSuccess) {
150 Logger::log('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, Logger::INFO);
151 Logger::log('debug: ' . print_r($this->info, true), Logger::DATA);
154 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
155 $this->isTimeout = true;
157 $this->isTimeout = false;
161 private function checkRedirect()
163 if (!array_key_exists('url', $this->info)) {
164 $this->redirectUrl = '';
166 $this->redirectUrl = $this->info['url'];
169 if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
170 $redirect_parts = parse_url(defaults($this->info, 'redirect_url', ''));
171 if (empty($redirect_parts)) {
172 $redirect_parts = [];
175 if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
176 $redirect_parts2 = parse_url(trim(array_pop($matches)));
177 if (!empty($redirect_parts2)) {
178 $redirect_parts = array_merge($redirect_parts, $redirect_parts2);
182 $parts = parse_url(defaults($this->info, 'url', ''));
187 /// @todo Checking the corresponding RFC which parts of a redirect can be ommitted.
188 $components = ['scheme', 'host', 'path', 'query', 'fragment'];
189 foreach ($components as $component) {
190 if (empty($redirect_parts[$component]) && !empty($parts[$component])) {
191 $redirect_parts[$component] = $parts[$component];
195 $this->redirectUrl = Network::unparseURL($redirect_parts);
197 $this->isRedirectUrl = true;
199 $this->isRedirectUrl = false;
203 private function checkInfo()
205 if (isset($this->info['content_type'])) {
206 $this->contentType = $this->info['content_type'];
208 $this->contentType = '';
215 * @return string The Curl Code
217 public function getReturnCode()
219 return $this->returnCode;
223 * Returns the Curl Content Type
225 * @return string the Curl Content Type
227 public function getContentType()
229 return $this->contentType;
233 * Returns the Curl headers
235 * @param string $field optional header field. Return all fields if empty
237 * @return string the Curl headers or the specified content of the header variable
239 public function getHeader(string $field = '')
242 return $this->header;
245 $field = strtolower(trim($field));
247 $headers = $this->getHeaderArray();
249 if (isset($headers[$field])) {
250 return $headers[$field];
255 * Check if a specified header exists
257 * @param string $field header field
259 * @return boolean "true" if header exists
261 public function headerExists(string $field)
263 $field = strtolower(trim($field));
265 $headers = $this->getHeaderArray();
267 return array_key_exists($field, $headers);
271 * Returns the Curl headers as an associated array
273 * @return array associated header array
275 public function getHeaderArray()
277 if (!empty($this->header_fields)) {
278 return $this->header_fields;
281 $this->header_fields = [];
283 $lines = explode("\n", trim($this->header));
284 foreach ($lines as $line) {
285 $parts = explode(':', $line);
286 $headerfield = strtolower(trim(array_shift($parts)));
287 $headerdata = trim(implode(':', $parts));
288 $this->header_fields[$headerfield] = $headerdata;
291 return $this->header_fields;
297 public function isSuccess()
299 return $this->isSuccess;
305 public function getUrl()
313 public function getRedirectUrl()
315 return $this->redirectUrl;
321 public function getBody()
329 public function getInfo()
337 public function isRedirectUrl()
339 return $this->isRedirectUrl;
345 public function getErrorNumber()
347 return $this->errorNumber;
353 public function getError()
361 public function isTimeout()
363 return $this->isTimeout;