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 boolean true (if HTTP 2xx result) or false
35 * @var string the URL which was called
40 * @var string in case of redirect, content was finally retrieved from this URL
45 * @var string fetched content
50 * @var array some informations about the fetched data
55 * @var boolean true if the URL has a redirect
57 private $isRedirectUrl;
60 * @var boolean true if the curl request timed out
65 * @var int the error number or 0 (zero) if no error
70 * @var string the error message or '' (the empty string) if no
75 * Creates an errored CURL response
77 * @param string $url optional URL
79 * @return CurlResult a CURL with error response
81 public static function createErrorCurl($url = '')
83 return new CurlResult($url, '', ['http_code' => 0]);
88 * @param string $url the URL which was called
89 * @param string $result the result of the curl execution
90 * @param array $info an additional info array
91 * @param int $errorNumber the error number or 0 (zero) if no error
92 * @param string $error the error message or '' (the empty string) if no
94 * @throws InternalServerErrorException when HTTP code of the CURL response is missing
96 public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
98 if (!array_key_exists('http_code', $info)) {
99 throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
102 $this->returnCode = $info['http_code'];
105 $this->errorNumber = $errorNumber;
106 $this->error = $error;
108 Logger::log($url . ': ' . $this->returnCode . " " . $result, Logger::DATA);
110 $this->parseBodyHeader($result);
111 $this->checkSuccess();
112 $this->checkRedirect();
116 private function parseBodyHeader($result)
118 // Pull out multiple headers, e.g. proxy and continuation headers
119 // allow for HTTP/2.x without fixing code
123 while (preg_match('/^HTTP\/.+? \d+/', $base)) {
124 $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
126 $base = substr($base, strlen($chunk));
129 $this->body = substr($result, strlen($header));
130 $this->header = $header;
133 private function checkSuccess()
135 $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
137 // Everything higher or equal 400 is not a success
138 if ($this->returnCode >= 400) {
139 $this->isSuccess = false;
142 if (!$this->isSuccess) {
143 Logger::log('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, Logger::INFO);
144 Logger::log('debug: ' . print_r($this->info, true), Logger::DATA);
147 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
148 $this->isTimeout = true;
150 $this->isTimeout = false;
154 private function checkRedirect()
156 if (!array_key_exists('url', $this->info)) {
157 $this->redirectUrl = '';
159 $this->redirectUrl = $this->info['url'];
162 if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
163 $redirect_parts = parse_url($this->info['redirect_url']);
164 if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
165 $redirect_parts = array_merge($redirect_parts, parse_url(trim(array_pop($matches))));
168 $parts = parse_url($this->info['url']);
170 if (empty($redirect_parts['scheme']) && !empty($parts['scheme'])) {
171 $redirect_parts['scheme'] = $parts['scheme'];
174 if (empty($redirect_parts['host']) && !empty($parts['host'])) {
175 $redirect_parts['host'] = $parts['host'];
178 $this->redirectUrl = Network::unparseURL($redirect_parts);
180 $this->isRedirectUrl = filter_var($this->redirectUrl, FILTER_VALIDATE_URL) !== false;
182 $this->isRedirectUrl = false;
186 private function checkInfo()
188 if (isset($this->info['content_type'])) {
189 $this->contentType = $this->info['content_type'];
191 $this->contentType = '';
198 * @return string The Curl Code
200 public function getReturnCode()
202 return $this->returnCode;
206 * Returns the Curl Content Type
208 * @return string the Curl Content Type
210 public function getContentType()
212 return $this->contentType;
216 * Returns the Curl headers
218 * @return string the Curl headers
220 public function getHeader()
222 return $this->header;
228 public function isSuccess()
230 return $this->isSuccess;
236 public function getUrl()
244 public function getRedirectUrl()
246 return $this->redirectUrl;
252 public function getBody()
260 public function getInfo()
268 public function isRedirectUrl()
270 return $this->isRedirectUrl;
276 public function getErrorNumber()
278 return $this->errorNumber;
284 public function getError()
292 public function isTimeout()
294 return $this->isTimeout;