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
80 * @throws InternalServerErrorException
82 public static function createErrorCurl($url = '')
84 return new CurlResult($url, '', ['http_code' => 0]);
89 * @param string $url the URL which was called
90 * @param string $result the result of the curl execution
91 * @param array $info an additional info array
92 * @param int $errorNumber the error number or 0 (zero) if no error
93 * @param string $error the error message or '' (the empty string) if no
95 * @throws InternalServerErrorException when HTTP code of the CURL response is missing
97 public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
99 if (!array_key_exists('http_code', $info)) {
100 throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
103 $this->returnCode = $info['http_code'];
106 $this->errorNumber = $errorNumber;
107 $this->error = $error;
109 Logger::log($url . ': ' . $this->returnCode . " " . $result, Logger::DATA);
111 $this->parseBodyHeader($result);
112 $this->checkSuccess();
113 $this->checkRedirect();
117 private function parseBodyHeader($result)
119 // Pull out multiple headers, e.g. proxy and continuation headers
120 // allow for HTTP/2.x without fixing code
124 while (preg_match('/^HTTP\/.+? \d+/', $base)) {
125 $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
127 $base = substr($base, strlen($chunk));
130 $this->body = substr($result, strlen($header));
131 $this->header = $header;
134 private function checkSuccess()
136 $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
138 // Everything higher or equal 400 is not a success
139 if ($this->returnCode >= 400) {
140 $this->isSuccess = false;
143 if (!$this->isSuccess) {
144 Logger::log('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, Logger::INFO);
145 Logger::log('debug: ' . print_r($this->info, true), Logger::DATA);
148 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
149 $this->isTimeout = true;
151 $this->isTimeout = false;
155 private function checkRedirect()
157 if (!array_key_exists('url', $this->info)) {
158 $this->redirectUrl = '';
160 $this->redirectUrl = $this->info['url'];
163 if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
164 $redirect_parts = parse_url(defaults($this->info, 'redirect_url', ''));
165 if (empty($redirect_parts)) {
166 $redirect_parts = [];
169 if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
170 $redirect_parts2 = parse_url(trim(array_pop($matches)));
171 if (!empty($redirect_parts2)) {
172 $redirect_parts = array_merge($redirect_parts, $redirect_parts2);
176 $parts = parse_url(defaults($this->info, 'url', ''));
181 /// @todo Checking the corresponding RFC which parts of a redirect can be ommitted.
182 $components = ['scheme', 'host', 'path', 'query', 'fragment'];
183 foreach ($components as $component) {
184 if (empty($redirect_parts[$component]) && !empty($parts[$component])) {
185 $redirect_parts[$component] = $parts[$component];
189 $this->redirectUrl = Network::unparseURL($redirect_parts);
191 $this->isRedirectUrl = true;
193 $this->isRedirectUrl = false;
197 private function checkInfo()
199 if (isset($this->info['content_type'])) {
200 $this->contentType = $this->info['content_type'];
202 $this->contentType = '';
209 * @return string The Curl Code
211 public function getReturnCode()
213 return $this->returnCode;
217 * Returns the Curl Content Type
219 * @return string the Curl Content Type
221 public function getContentType()
223 return $this->contentType;
227 * Returns the Curl headers
229 * @return string the Curl headers
231 public function getHeader()
233 return $this->header;
239 public function isSuccess()
241 return $this->isSuccess;
247 public function getUrl()
255 public function getRedirectUrl()
257 return $this->redirectUrl;
263 public function getBody()
271 public function getInfo()
279 public function isRedirectUrl()
281 return $this->isRedirectUrl;
287 public function getErrorNumber()
289 return $this->errorNumber;
295 public function getError()
303 public function isTimeout()
305 return $this->isTimeout;