2020-02-09 21:48:04 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 14:45:36 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2020-02-09 21:48:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Network;
|
2018-10-09 17:58:58 +00:00
|
|
|
|
2018-10-29 21:20:46 +00:00
|
|
|
use Friendica\Core\Logger;
|
2021-10-29 23:21:07 +00:00
|
|
|
use Friendica\Network\HTTPException\UnprocessableEntityException;
|
2018-11-24 00:27:00 +00:00
|
|
|
use Friendica\Util\Network;
|
2018-10-10 19:08:43 +00:00
|
|
|
|
2018-10-09 17:58:58 +00:00
|
|
|
/**
|
|
|
|
* A content class for Curl call results
|
|
|
|
*/
|
2021-08-20 17:48:13 +00:00
|
|
|
class CurlResult implements IHTTPResult
|
2018-10-09 17:58:58 +00:00
|
|
|
{
|
|
|
|
/**
|
2018-10-10 19:08:43 +00:00
|
|
|
* @var int HTTP return code or 0 if timeout or failure
|
2018-10-09 17:58:58 +00:00
|
|
|
*/
|
2018-10-10 19:08:43 +00:00
|
|
|
private $returnCode;
|
2018-10-09 17:58:58 +00:00
|
|
|
|
|
|
|
/**
|
2021-08-20 17:48:21 +00:00
|
|
|
* @var string the content type of the Curl call
|
2018-10-09 17:58:58 +00:00
|
|
|
*/
|
|
|
|
private $contentType;
|
|
|
|
|
|
|
|
/**
|
2018-10-10 19:08:43 +00:00
|
|
|
* @var string the HTTP headers of the Curl call
|
2018-10-09 17:58:58 +00:00
|
|
|
*/
|
2018-10-10 19:08:43 +00:00
|
|
|
private $header;
|
2018-10-09 17:58:58 +00:00
|
|
|
|
2019-10-02 03:45:32 +00:00
|
|
|
/**
|
|
|
|
* @var array the HTTP headers of the Curl call
|
|
|
|
*/
|
|
|
|
private $header_fields;
|
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
/**
|
|
|
|
* @var boolean true (if HTTP 2xx result) or false
|
|
|
|
*/
|
|
|
|
private $isSuccess;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string the URL which was called
|
|
|
|
*/
|
|
|
|
private $url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string in case of redirect, content was finally retrieved from this URL
|
|
|
|
*/
|
|
|
|
private $redirectUrl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string fetched content
|
|
|
|
*/
|
|
|
|
private $body;
|
2018-10-09 17:58:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-10 19:08:43 +00:00
|
|
|
* @var array some informations about the fetched data
|
|
|
|
*/
|
|
|
|
private $info;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean true if the URL has a redirect
|
|
|
|
*/
|
|
|
|
private $isRedirectUrl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean true if the curl request timed out
|
|
|
|
*/
|
|
|
|
private $isTimeout;
|
|
|
|
|
|
|
|
/**
|
2018-10-10 19:20:30 +00:00
|
|
|
* @var int the error number or 0 (zero) if no error
|
2018-10-10 19:08:43 +00:00
|
|
|
*/
|
|
|
|
private $errorNumber;
|
|
|
|
|
|
|
|
/**
|
2018-10-10 19:20:30 +00:00
|
|
|
* @var string the error message or '' (the empty string) if no
|
2018-10-10 19:08:43 +00:00
|
|
|
*/
|
|
|
|
private $error;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an errored CURL response
|
2018-10-09 17:58:58 +00:00
|
|
|
*
|
2018-10-10 19:08:43 +00:00
|
|
|
* @param string $url optional URL
|
|
|
|
*
|
2021-08-20 17:48:14 +00:00
|
|
|
* @return IHTTPResult a CURL with error response
|
2021-10-29 23:21:07 +00:00
|
|
|
* @throws UnprocessableEntityException
|
2018-10-09 17:58:58 +00:00
|
|
|
*/
|
2018-10-10 19:08:43 +00:00
|
|
|
public static function createErrorCurl($url = '')
|
2018-10-09 17:58:58 +00:00
|
|
|
{
|
2018-10-10 20:51:26 +00:00
|
|
|
return new CurlResult($url, '', ['http_code' => 0]);
|
2018-10-09 17:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-10 19:08:43 +00:00
|
|
|
* Curl constructor.
|
|
|
|
* @param string $url the URL which was called
|
|
|
|
* @param string $result the result of the curl execution
|
|
|
|
* @param array $info an additional info array
|
|
|
|
* @param int $errorNumber the error number or 0 (zero) if no error
|
|
|
|
* @param string $error the error message or '' (the empty string) if no
|
2018-10-09 17:58:58 +00:00
|
|
|
*
|
2021-10-29 23:21:07 +00:00
|
|
|
* @throws UnprocessableEntityException when HTTP code of the CURL response is missing
|
2018-10-09 17:58:58 +00:00
|
|
|
*/
|
2018-10-10 19:08:43 +00:00
|
|
|
public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
|
|
|
|
{
|
2018-10-10 19:20:30 +00:00
|
|
|
if (!array_key_exists('http_code', $info)) {
|
2021-10-29 23:21:07 +00:00
|
|
|
throw new UnprocessableEntityException('CURL response doesn\'t contains a response HTTP code');
|
2018-10-10 19:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->returnCode = $info['http_code'];
|
|
|
|
$this->url = $url;
|
|
|
|
$this->info = $info;
|
|
|
|
$this->errorNumber = $errorNumber;
|
|
|
|
$this->error = $error;
|
|
|
|
|
2020-06-29 20:22:00 +00:00
|
|
|
Logger::debug('construct', ['url' => $url, 'returncode' => $this->returnCode, 'result' => $result]);
|
2018-10-10 19:08:43 +00:00
|
|
|
|
|
|
|
$this->parseBodyHeader($result);
|
|
|
|
$this->checkSuccess();
|
|
|
|
$this->checkRedirect();
|
|
|
|
$this->checkInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseBodyHeader($result)
|
|
|
|
{
|
|
|
|
// Pull out multiple headers, e.g. proxy and continuation headers
|
|
|
|
// allow for HTTP/2.x without fixing code
|
|
|
|
|
|
|
|
$header = '';
|
|
|
|
$base = $result;
|
2018-10-11 20:18:27 +00:00
|
|
|
while (preg_match('/^HTTP\/.+? \d+/', $base)) {
|
2018-10-10 19:08:43 +00:00
|
|
|
$chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
|
|
|
|
$header .= $chunk;
|
|
|
|
$base = substr($base, strlen($chunk));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->body = substr($result, strlen($header));
|
|
|
|
$this->header = $header;
|
2019-10-02 04:10:36 +00:00
|
|
|
$this->header_fields = []; // Is filled on demand
|
2018-10-10 19:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function checkSuccess()
|
2018-10-09 17:58:58 +00:00
|
|
|
{
|
2018-10-10 19:20:30 +00:00
|
|
|
$this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
|
2018-10-10 19:08:43 +00:00
|
|
|
|
2018-11-12 04:12:36 +00:00
|
|
|
// Everything higher or equal 400 is not a success
|
|
|
|
if ($this->returnCode >= 400) {
|
2018-11-11 21:54:50 +00:00
|
|
|
$this->isSuccess = false;
|
|
|
|
}
|
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if (!$this->isSuccess) {
|
2020-06-02 06:47:44 +00:00
|
|
|
Logger::debug('debug', ['info' => $this->info]);
|
2018-10-10 19:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
|
|
|
|
$this->isTimeout = true;
|
|
|
|
} else {
|
|
|
|
$this->isTimeout = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkRedirect()
|
|
|
|
{
|
2018-10-10 19:20:30 +00:00
|
|
|
if (!array_key_exists('url', $this->info)) {
|
2018-10-10 19:08:43 +00:00
|
|
|
$this->redirectUrl = '';
|
|
|
|
} else {
|
|
|
|
$this->redirectUrl = $this->info['url'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
|
2019-10-16 12:35:14 +00:00
|
|
|
$redirect_parts = parse_url($this->info['redirect_url'] ?? '');
|
2018-12-05 06:01:58 +00:00
|
|
|
if (empty($redirect_parts)) {
|
|
|
|
$redirect_parts = [];
|
|
|
|
}
|
|
|
|
|
2018-11-24 00:27:00 +00:00
|
|
|
if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
|
2018-12-05 06:01:58 +00:00
|
|
|
$redirect_parts2 = parse_url(trim(array_pop($matches)));
|
|
|
|
if (!empty($redirect_parts2)) {
|
|
|
|
$redirect_parts = array_merge($redirect_parts, $redirect_parts2);
|
|
|
|
}
|
2018-10-10 19:08:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 12:35:14 +00:00
|
|
|
$parts = parse_url($this->info['url'] ?? '');
|
2018-12-05 06:01:58 +00:00
|
|
|
if (empty($parts)) {
|
|
|
|
$parts = [];
|
|
|
|
}
|
2018-10-10 19:08:43 +00:00
|
|
|
|
2018-11-24 09:01:10 +00:00
|
|
|
/// @todo Checking the corresponding RFC which parts of a redirect can be ommitted.
|
|
|
|
$components = ['scheme', 'host', 'path', 'query', 'fragment'];
|
|
|
|
foreach ($components as $component) {
|
|
|
|
if (empty($redirect_parts[$component]) && !empty($parts[$component])) {
|
|
|
|
$redirect_parts[$component] = $parts[$component];
|
|
|
|
}
|
2018-10-10 19:08:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 00:27:00 +00:00
|
|
|
$this->redirectUrl = Network::unparseURL($redirect_parts);
|
|
|
|
|
2018-12-04 07:12:55 +00:00
|
|
|
$this->isRedirectUrl = true;
|
2018-10-10 19:08:43 +00:00
|
|
|
} else {
|
|
|
|
$this->isRedirectUrl = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkInfo()
|
|
|
|
{
|
|
|
|
if (isset($this->info['content_type'])) {
|
2021-08-20 17:48:21 +00:00
|
|
|
$this->contentType = $this->info['content_type'];
|
2018-10-10 19:08:43 +00:00
|
|
|
} else {
|
2021-08-20 17:48:21 +00:00
|
|
|
$this->contentType = '';
|
2018-10-10 19:08:43 +00:00
|
|
|
}
|
2018-10-09 17:58:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function getReturnCode()
|
2018-10-09 17:58:58 +00:00
|
|
|
{
|
2018-10-10 19:08:43 +00:00
|
|
|
return $this->returnCode;
|
2018-10-09 17:58:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-09 17:58:58 +00:00
|
|
|
public function getContentType()
|
|
|
|
{
|
|
|
|
return $this->contentType;
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2021-08-20 17:48:14 +00:00
|
|
|
public function getHeader($header)
|
2018-10-09 17:58:58 +00:00
|
|
|
{
|
2021-08-20 17:48:14 +00:00
|
|
|
if (empty($header)) {
|
2021-08-20 17:48:20 +00:00
|
|
|
return [];
|
2019-10-01 16:33:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:14 +00:00
|
|
|
$header = strtolower(trim($header));
|
2019-10-01 21:46:18 +00:00
|
|
|
|
2019-10-02 03:31:58 +00:00
|
|
|
$headers = $this->getHeaderArray();
|
2019-10-01 21:46:18 +00:00
|
|
|
|
2021-08-20 17:48:14 +00:00
|
|
|
if (isset($headers[$header])) {
|
|
|
|
return $headers[$header];
|
2019-10-01 16:33:11 +00:00
|
|
|
}
|
2019-10-02 09:37:05 +00:00
|
|
|
|
2021-08-20 17:48:20 +00:00
|
|
|
return [];
|
2019-10-01 21:46:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:14 +00:00
|
|
|
/** {@inheritDoc} */
|
|
|
|
public function getHeaders()
|
|
|
|
{
|
|
|
|
return $this->getHeaderArray();
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2019-10-02 06:24:29 +00:00
|
|
|
public function inHeader(string $field)
|
2019-10-01 21:46:18 +00:00
|
|
|
{
|
|
|
|
$field = strtolower(trim($field));
|
|
|
|
|
2019-10-02 03:31:58 +00:00
|
|
|
$headers = $this->getHeaderArray();
|
2019-10-01 16:33:11 +00:00
|
|
|
|
2019-10-01 21:46:18 +00:00
|
|
|
return array_key_exists($field, $headers);
|
2018-10-09 17:58:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2019-10-01 18:22:33 +00:00
|
|
|
public function getHeaderArray()
|
|
|
|
{
|
2019-10-02 03:45:32 +00:00
|
|
|
if (!empty($this->header_fields)) {
|
|
|
|
return $this->header_fields;
|
|
|
|
}
|
2019-10-01 18:22:33 +00:00
|
|
|
|
2019-10-02 03:45:32 +00:00
|
|
|
$this->header_fields = [];
|
|
|
|
|
|
|
|
$lines = explode("\n", trim($this->header));
|
2019-10-01 18:22:33 +00:00
|
|
|
foreach ($lines as $line) {
|
|
|
|
$parts = explode(':', $line);
|
|
|
|
$headerfield = strtolower(trim(array_shift($parts)));
|
|
|
|
$headerdata = trim(implode(':', $parts));
|
2021-08-20 17:48:20 +00:00
|
|
|
if (empty($this->header_fields[$headerfield])) {
|
|
|
|
$this->header_fields[$headerfield] = [$headerdata];
|
|
|
|
} elseif (!in_array($headerdata, $this->header_fields[$headerfield])) {
|
|
|
|
$this->header_fields[$headerfield][] = $headerdata;
|
|
|
|
}
|
2019-10-01 18:22:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 03:45:32 +00:00
|
|
|
return $this->header_fields;
|
2019-10-01 18:22:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function isSuccess()
|
|
|
|
{
|
|
|
|
return $this->isSuccess;
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function getRedirectUrl()
|
|
|
|
{
|
|
|
|
return $this->redirectUrl;
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function getBody()
|
|
|
|
{
|
|
|
|
return $this->body;
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function isRedirectUrl()
|
|
|
|
{
|
|
|
|
return $this->isRedirectUrl;
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function getErrorNumber()
|
|
|
|
{
|
|
|
|
return $this->errorNumber;
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function getError()
|
|
|
|
{
|
|
|
|
return $this->error;
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:48:13 +00:00
|
|
|
/** {@inheritDoc} */
|
2018-10-10 19:08:43 +00:00
|
|
|
public function isTimeout()
|
2018-10-09 17:58:58 +00:00
|
|
|
{
|
2018-10-10 19:08:43 +00:00
|
|
|
return $this->isTimeout;
|
2018-10-09 17:58:58 +00:00
|
|
|
}
|
|
|
|
}
|