Curl Response Refactoring
- extended Curl to parse Curl Response - refactored Network::curl() - replaced every Network::curl() execution with the new Curl container
This commit is contained in:
parent
904fee3bed
commit
2dec8895a9
20 changed files with 466 additions and 382 deletions
|
@ -3,15 +3,17 @@
|
|||
namespace Friendica\Network;
|
||||
|
||||
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
|
||||
/**
|
||||
* A content class for Curl call results
|
||||
*/
|
||||
class Curl
|
||||
{
|
||||
/**
|
||||
* @var string the Code of the Curl call
|
||||
* @var int HTTP return code or 0 if timeout or failure
|
||||
*/
|
||||
private $code;
|
||||
private $returnCode;
|
||||
|
||||
/**
|
||||
* @var string the content type of the Curl call
|
||||
|
@ -19,25 +21,179 @@ class Curl
|
|||
private $contentType;
|
||||
|
||||
/**
|
||||
* @var string the headers of the Curl call
|
||||
* @var string the HTTP headers of the Curl call
|
||||
*/
|
||||
private $headers;
|
||||
private $header;
|
||||
|
||||
public function __construct($code = '', $contentType = '', $headers = '')
|
||||
/**
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* @var int optional error numer
|
||||
*/
|
||||
private $errorNumber;
|
||||
|
||||
/**
|
||||
* @var string optional error message
|
||||
*/
|
||||
private $error;
|
||||
|
||||
/**
|
||||
* Creates an errored CURL response
|
||||
*
|
||||
* @param string $url optional URL
|
||||
*
|
||||
* @return Curl a CURL with error response
|
||||
*/
|
||||
public static function createErrorCurl($url = '')
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->contentType = $contentType;
|
||||
$this->headers = $headers;
|
||||
return new Curl(
|
||||
$url,
|
||||
'',
|
||||
['http_code' => 0]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Curl Code
|
||||
* 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
|
||||
*
|
||||
* @param string $code The Curl Code
|
||||
* @throws InternalServerErrorException when HTTP code of the CURL response is missing
|
||||
*/
|
||||
public function setCode($code)
|
||||
public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
|
||||
{
|
||||
$this->code = $code;
|
||||
if (empty($info['http_code'])) {
|
||||
throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
|
||||
}
|
||||
|
||||
$this->returnCode = $info['http_code'];
|
||||
$this->url = $url;
|
||||
$this->info = $info;
|
||||
$this->errorNumber = $errorNumber;
|
||||
$this->error = $error;
|
||||
|
||||
logger($url . ': ' . $this->returnCode . " " . $result, LOGGER_DATA);
|
||||
|
||||
$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;
|
||||
while (preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/', $base)) {
|
||||
$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;
|
||||
}
|
||||
|
||||
private function checkSuccess()
|
||||
{
|
||||
$this->isSuccess = ((($this->returnCode >= 200 && $this->returnCode <= 299) || !empty($this->errorNumber)) ? true : false);
|
||||
|
||||
if (!$this->isSuccess) {
|
||||
logger('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, LOGGER_DEBUG);
|
||||
logger('debug: ' . print_r($this->info, true), LOGGER_DATA);
|
||||
}
|
||||
|
||||
if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
|
||||
$this->isTimeout = true;
|
||||
} else {
|
||||
$this->isTimeout = false;
|
||||
}
|
||||
}
|
||||
|
||||
private function checkRedirect()
|
||||
{
|
||||
if (empty($this->info['url'])) {
|
||||
$this->redirectUrl = '';
|
||||
} else {
|
||||
$this->redirectUrl = $this->info['url'];
|
||||
}
|
||||
|
||||
if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
|
||||
$new_location_info = (empty($this->info['redirect_url']) ? '' : @parse_url($this->info['redirect_url']));
|
||||
$old_location_info = (empty($this->info['url'] ? '' : @parse_url($this->info['url']));
|
||||
|
||||
$this->redirectUrl = $new_location_info;
|
||||
|
||||
if (empty($new_location_info['path']) && !empty($new_location_info['host'])) {
|
||||
$this->redirectUrl = $new_location_info['scheme'] . '://' . $new_location_info['host'] . $old_location_info['path'];
|
||||
}
|
||||
|
||||
$matches = [];
|
||||
|
||||
if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
|
||||
$this->redirectUrl = trim(array_pop($matches));
|
||||
}
|
||||
if (strpos($this->redirectUrl, '/') === 0) {
|
||||
$this->redirectUrl = $old_location_info["scheme"] . "://" . $old_location_info["host"] . $this->redirectUrl;
|
||||
}
|
||||
$old_location_query = @parse_url($this->url, PHP_URL_QUERY);
|
||||
|
||||
if ($old_location_query != '') {
|
||||
$this->redirectUrl .= '?' . $old_location_query;
|
||||
}
|
||||
|
||||
$this->isRedirectUrl = filter_var($this->redirectUrl, FILTER_VALIDATE_URL);
|
||||
} else {
|
||||
$this->isRedirectUrl = false;
|
||||
}
|
||||
}
|
||||
|
||||
private function checkInfo()
|
||||
{
|
||||
if (isset($this->info['content_type'])) {
|
||||
$this->contentType = $this->info['content_type'];
|
||||
} else {
|
||||
$this->contentType = '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -45,19 +201,9 @@ class Curl
|
|||
*
|
||||
* @return string The Curl Code
|
||||
*/
|
||||
public function getCode()
|
||||
public function getReturnCode()
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Curl Content Type
|
||||
*
|
||||
* @param string $content_type The Curl Content Type
|
||||
*/
|
||||
public function setContentType($content_type)
|
||||
{
|
||||
$this->contentType = $content_type;
|
||||
return $this->returnCode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,23 +216,85 @@ class Curl
|
|||
return $this->contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Curl headers
|
||||
*
|
||||
* @param string $headers the Curl headers
|
||||
*/
|
||||
public function setHeaders($headers)
|
||||
{
|
||||
$this->headers = $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Curl headers
|
||||
*
|
||||
* @return string the Curl headers
|
||||
*/
|
||||
public function getHeaders()
|
||||
public function getHeader()
|
||||
{
|
||||
return $this->headers;
|
||||
return $this->header;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isSuccess()
|
||||
{
|
||||
return $this->isSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRedirectUrl()
|
||||
{
|
||||
return $this->redirectUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getInfo()
|
||||
{
|
||||
return $this->info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRedirectUrl()
|
||||
{
|
||||
return $this->isRedirectUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getErrorNumber()
|
||||
{
|
||||
return $this->errorNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isTimeout()
|
||||
{
|
||||
return $this->isTimeout;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue