3 namespace Friendica\Network;
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 Curl a CURL with error response
80 public static function createErrorCurl($url = '')
91 * @param string $url the URL which was called
92 * @param string $result the result of the curl execution
93 * @param array $info an additional info array
94 * @param int $errorNumber the error number or 0 (zero) if no error
95 * @param string $error the error message or '' (the empty string) if no
97 * @throws InternalServerErrorException when HTTP code of the CURL response is missing
99 public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
101 if (!array_key_exists('http_code', $info)) {
102 throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
105 $this->returnCode = $info['http_code'];
108 $this->errorNumber = $errorNumber;
109 $this->error = $error;
111 logger($url . ': ' . $this->returnCode . " " . $result, LOGGER_DATA);
113 $this->parseBodyHeader($result);
114 $this->checkSuccess();
115 $this->checkRedirect();
119 private function parseBodyHeader($result)
121 // Pull out multiple headers, e.g. proxy and continuation headers
122 // allow for HTTP/2.x without fixing code
126 while (preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/', $base)) {
127 $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
129 $base = substr($base, strlen($chunk));
132 $this->body = substr($result, strlen($header));
133 $this->header = $header;
136 private function checkSuccess()
138 $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
140 if (!$this->isSuccess) {
141 logger('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, LOGGER_INFO);
142 logger('debug: ' . print_r($this->info, true), LOGGER_DATA);
145 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
146 $this->isTimeout = true;
148 $this->isTimeout = false;
152 private function checkRedirect()
154 if (!array_key_exists('url', $this->info)) {
155 $this->redirectUrl = '';
157 $this->redirectUrl = $this->info['url'];
160 if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
161 $new_location_info = (!array_key_exists('redirect_url', $this->info) ? '' : @parse_url($this->info['redirect_url']));
162 $old_location_info = (!array_key_exists('url', $this->info) ? '' : @parse_url($this->info['url']));
164 $this->redirectUrl = $new_location_info;
166 if (empty($new_location_info['path']) && !empty($new_location_info['host'])) {
167 $this->redirectUrl = $new_location_info['scheme'] . '://' . $new_location_info['host'] . $old_location_info['path'];
172 if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
173 $this->redirectUrl = trim(array_pop($matches));
175 if (strpos($this->redirectUrl, '/') === 0) {
176 $this->redirectUrl = $old_location_info["scheme"] . "://" . $old_location_info["host"] . $this->redirectUrl;
178 $old_location_query = @parse_url($this->url, PHP_URL_QUERY);
180 if ($old_location_query != '') {
181 $this->redirectUrl .= '?' . $old_location_query;
184 $this->isRedirectUrl = filter_var($this->redirectUrl, FILTER_VALIDATE_URL);
186 $this->isRedirectUrl = false;
190 private function checkInfo()
192 if (isset($this->info['content_type'])) {
193 $this->contentType = $this->info['content_type'];
195 $this->contentType = '';
202 * @return string The Curl Code
204 public function getReturnCode()
206 return $this->returnCode;
210 * Returns the Curl Content Type
212 * @return string the Curl Content Type
214 public function getContentType()
216 return $this->contentType;
220 * Returns the Curl headers
222 * @return string the Curl headers
224 public function getHeader()
226 return $this->header;
232 public function isSuccess()
234 return $this->isSuccess;
240 public function getUrl()
248 public function getRedirectUrl()
250 return $this->redirectUrl;
256 public function getBody()
264 public function getInfo()
272 public function isRedirectUrl()
274 return $this->isRedirectUrl;
280 public function getErrorNumber()
282 return $this->errorNumber;
288 public function getError()
296 public function isTimeout()
298 return $this->isTimeout;