3 namespace Friendica\Network;
5 use Friendica\Core\Logger;
6 use Friendica\Network\HTTPException\InternalServerErrorException;
9 * A content class for Curl call results
14 * @var int HTTP return code or 0 if timeout or failure
19 * @var string the content type of the Curl call
24 * @var string the HTTP headers of the Curl call
29 * @var boolean true (if HTTP 2xx result) or false
34 * @var string the URL which was called
39 * @var string in case of redirect, content was finally retrieved from this URL
44 * @var string fetched content
49 * @var array some informations about the fetched data
54 * @var boolean true if the URL has a redirect
56 private $isRedirectUrl;
59 * @var boolean true if the curl request timed out
64 * @var int the error number or 0 (zero) if no error
69 * @var string the error message or '' (the empty string) if no
74 * Creates an errored CURL response
76 * @param string $url optional URL
78 * @return CurlResult a CURL with error response
80 public static function createErrorCurl($url = '')
82 return new CurlResult($url, '', ['http_code' => 0]);
87 * @param string $url the URL which was called
88 * @param string $result the result of the curl execution
89 * @param array $info an additional info array
90 * @param int $errorNumber the error number or 0 (zero) if no error
91 * @param string $error the error message or '' (the empty string) if no
93 * @throws InternalServerErrorException when HTTP code of the CURL response is missing
95 public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
97 if (!array_key_exists('http_code', $info)) {
98 throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
101 $this->returnCode = $info['http_code'];
104 $this->errorNumber = $errorNumber;
105 $this->error = $error;
107 Logger::log($url . ': ' . $this->returnCode . " " . $result, Logger::DATA);
109 $this->parseBodyHeader($result);
110 $this->checkSuccess();
111 $this->checkRedirect();
115 private function parseBodyHeader($result)
117 // Pull out multiple headers, e.g. proxy and continuation headers
118 // allow for HTTP/2.x without fixing code
122 while (preg_match('/^HTTP\/.+? \d+/', $base)) {
123 $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
125 $base = substr($base, strlen($chunk));
128 $this->body = substr($result, strlen($header));
129 $this->header = $header;
132 private function checkSuccess()
134 $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
136 // Everything higher or equal 400 is not a success
137 if ($this->returnCode >= 400) {
138 $this->isSuccess = false;
141 if (!$this->isSuccess) {
142 Logger::log('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, Logger::INFO);
143 Logger::log('debug: ' . print_r($this->info, true), Logger::DATA);
146 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
147 $this->isTimeout = true;
149 $this->isTimeout = false;
153 private function checkRedirect()
155 if (!array_key_exists('url', $this->info)) {
156 $this->redirectUrl = '';
158 $this->redirectUrl = $this->info['url'];
161 if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
162 $new_location_info = (!array_key_exists('redirect_url', $this->info) ? '' : @parse_url($this->info['redirect_url']));
163 $old_location_info = (!array_key_exists('url', $this->info) ? '' : @parse_url($this->info['url']));
165 $this->redirectUrl = $new_location_info;
167 if (empty($new_location_info['path']) && !empty($new_location_info['host'])) {
168 $this->redirectUrl = $new_location_info['scheme'] . '://' . $new_location_info['host'] . $old_location_info['path'];
173 if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
174 $this->redirectUrl = trim(array_pop($matches));
176 if (strpos($this->redirectUrl, '/') === 0) {
177 $this->redirectUrl = $old_location_info["scheme"] . "://" . $old_location_info["host"] . $this->redirectUrl;
179 $old_location_query = @parse_url($this->url, PHP_URL_QUERY);
181 if ($old_location_query != '') {
182 $this->redirectUrl .= '?' . $old_location_query;
185 $this->isRedirectUrl = filter_var($this->redirectUrl, FILTER_VALIDATE_URL) !== false;
187 $this->isRedirectUrl = false;
191 private function checkInfo()
193 if (isset($this->info['content_type'])) {
194 $this->contentType = $this->info['content_type'];
196 $this->contentType = '';
203 * @return string The Curl Code
205 public function getReturnCode()
207 return $this->returnCode;
211 * Returns the Curl Content Type
213 * @return string the Curl Content Type
215 public function getContentType()
217 return $this->contentType;
221 * Returns the Curl headers
223 * @return string the Curl headers
225 public function getHeader()
227 return $this->header;
233 public function isSuccess()
235 return $this->isSuccess;
241 public function getUrl()
249 public function getRedirectUrl()
251 return $this->redirectUrl;
257 public function getBody()
265 public function getInfo()
273 public function isRedirectUrl()
275 return $this->isRedirectUrl;
281 public function getErrorNumber()
283 return $this->errorNumber;
289 public function getError()
297 public function isTimeout()
299 return $this->isTimeout;