Curl Response Refactoring

- refactored Network::getCurl()
- replaced every Network::getCur() execution with a Curl Response
This commit is contained in:
Philipp Holzer 2018-10-10 21:20:30 +02:00
parent 7c73e8634c
commit ffbc688797
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
9 changed files with 30 additions and 30 deletions

View file

@ -104,8 +104,9 @@ function pubsubhubbub_init(App $a) {
// we don't actually enforce the lease time because GNU // we don't actually enforce the lease time because GNU
// Social/StatusNet doesn't honour it (yet) // Social/StatusNet doesn't honour it (yet)
$body = Network::fetchUrl($hub_callback . "?" . $params); $fetchResult = Network::fetchUrlFull($hub_callback . "?" . $params);
$ret = Network::getCurl()->getCode(); $body = $fetchResult->getBody();
$ret = $fetchResult->getReturnCode();
// give up if the HTTP return code wasn't a success (2xx) // give up if the HTTP return code wasn't a success (2xx)
if ($ret < 200 || $ret > 299) { if ($ret < 200 || $ret > 299) {

View file

@ -345,20 +345,20 @@ class Install extends BaseObject
$help = ""; $help = "";
$error_msg = ""; $error_msg = "";
if (function_exists('curl_init')) { if (function_exists('curl_init')) {
$test = Network::fetchUrlFull(System::baseUrl() . "/install/testrewrite"); $fetchResult = Network::fetchUrlFull(System::baseUrl() . "/install/testrewrite");
$url = normalise_link(System::baseUrl() . "/install/testrewrite"); $url = normalise_link(System::baseUrl() . "/install/testrewrite");
if ($test['body'] != "ok") { if ($fetchResult->getBody() != "ok") {
$test = Network::fetchUrlFull($url); $fetchResult = Network::fetchUrlFull($url);
} }
if ($test['body'] != "ok") { if ($fetchResult->getBody() != "ok") {
$status = false; $status = false;
$help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.'); $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.');
$error_msg = []; $error_msg = [];
$error_msg['head'] = L10n::t('Error message from Curl when fetching'); $error_msg['head'] = L10n::t('Error message from Curl when fetching');
$error_msg['url'] = $test['redirect_url']; $error_msg['url'] = $fetchResult->getRedirectUrl();
$error_msg['msg'] = defaults($test, 'error', ''); $error_msg['msg'] = $fetchResult->getError();
} }
self::addCheck($checks, L10n::t('Url rewrite is working'), $status, true, $help, $error_msg); self::addCheck($checks, L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
} else { } else {

View file

@ -188,7 +188,8 @@ class Proxy extends BaseModule
// It shouldn't happen but it does - spaces in URL // It shouldn't happen but it does - spaces in URL
$_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']); $_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']);
$redirects = 0; $redirects = 0;
$img_str = Network::fetchUrl($_REQUEST['url'], true, $redirects, 10); $fetchResult = Network::fetchUrlFull($_REQUEST['url'], true, $redirects, 10);
$img_str = $fetchResult->getBody();
$tempfile = tempnam(get_temppath(), 'cache'); $tempfile = tempnam(get_temppath(), 'cache');
file_put_contents($tempfile, $img_str); file_put_contents($tempfile, $img_str);
@ -196,7 +197,7 @@ class Proxy extends BaseModule
unlink($tempfile); unlink($tempfile);
// If there is an error then return a blank image // If there is an error then return a blank image
if ((substr(Network::getCurl()->getCode(), 0, 1) == '4') || (!$img_str)) { if ((substr($fetchResult->getReturnCode(), 0, 1) == '4') || (!$img_str)) {
$img_str = file_get_contents('images/blank.png'); $img_str = file_get_contents('images/blank.png');
$mime = 'image/png'; $mime = 'image/png';
$cachefile = ''; // Clear the cachefile so that the dummy isn't stored $cachefile = ''; // Clear the cachefile so that the dummy isn't stored

View file

@ -61,12 +61,12 @@ class Curl
private $isTimeout; private $isTimeout;
/** /**
* @var int optional error numer * @var int the error number or 0 (zero) if no error
*/ */
private $errorNumber; private $errorNumber;
/** /**
* @var string optional error message * @var string the error message or '' (the empty string) if no
*/ */
private $error; private $error;
@ -98,7 +98,7 @@ class Curl
*/ */
public function __construct($url, $result, $info, $errorNumber = 0, $error = '') public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
{ {
if (empty($info['http_code'])) { if (!array_key_exists('http_code', $info)) {
throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code'); throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
} }
@ -135,10 +135,10 @@ class Curl
private function checkSuccess() private function checkSuccess()
{ {
$this->isSuccess = ((($this->returnCode >= 200 && $this->returnCode <= 299) || !empty($this->errorNumber)) ? true : false); $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
if (!$this->isSuccess) { if (!$this->isSuccess) {
logger('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, LOGGER_DEBUG); logger('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, LOGGER_INFO);
logger('debug: ' . print_r($this->info, true), LOGGER_DATA); logger('debug: ' . print_r($this->info, true), LOGGER_DATA);
} }
@ -151,15 +151,15 @@ class Curl
private function checkRedirect() private function checkRedirect()
{ {
if (empty($this->info['url'])) { if (!array_key_exists('url', $this->info)) {
$this->redirectUrl = ''; $this->redirectUrl = '';
} else { } else {
$this->redirectUrl = $this->info['url']; $this->redirectUrl = $this->info['url'];
} }
if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) { 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'])); $new_location_info = (!array_key_exists('redirect_url', $this->info) ? '' : @parse_url($this->info['redirect_url']));
$old_location_info = (empty($this->info['url'] ? '' : @parse_url($this->info['url'])); $old_location_info = (!array_key_exists('url', $this->info) ? '' : @parse_url($this->info['url']));
$this->redirectUrl = $new_location_info; $this->redirectUrl = $new_location_info;

View file

@ -720,17 +720,18 @@ class Image
* *
* @param string $filename Image filename * @param string $filename Image filename
* @param boolean $fromcurl Check Content-Type header from curl request * @param boolean $fromcurl Check Content-Type header from curl request
* @param string $header passed headers to take into account
* *
* @return object * @return object
*/ */
public static function guessType($filename, $fromcurl = false) public static function guessType($filename, $fromcurl = false, $header = '')
{ {
logger('Image: guessType: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG); logger('Image: guessType: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
$type = null; $type = null;
if ($fromcurl) { if ($fromcurl) {
$a = get_app(); $a = get_app();
$headers=[]; $headers=[];
$h = explode("\n", Network::getCurl()->getHeaders()); $h = explode("\n", $header);
foreach ($h as $l) { foreach ($h as $l) {
$data = array_map("trim", explode(":", trim($l), 2)); $data = array_map("trim", explode(":", trim($l), 2));
if (count($data) > 1) { if (count($data) > 1) {

View file

@ -3089,7 +3089,7 @@ class Diaspora
logger("transmit: ".$logid."-".$guid." to ".$dest_url." returns: ".$return_code); logger("transmit: ".$logid."-".$guid." to ".$dest_url." returns: ".$return_code);
if (!$return_code || (($return_code == 503) && (stristr($postResult->getHeaders(), "retry-after")))) { if (!$return_code || (($return_code == 503) && (stristr($postResult->getHeader(), "retry-after")))) {
if (!$no_queue && !empty($contact['contact-type']) && ($contact['contact-type'] != Contact::ACCOUNT_TYPE_RELAY)) { if (!$no_queue && !empty($contact['contact-type']) && ($contact['contact-type'] != Contact::ACCOUNT_TYPE_RELAY)) {
logger("queue message"); logger("queue message");
// queue message for redelivery // queue message for redelivery

View file

@ -86,13 +86,14 @@ class PortableContact
logger('load: ' . $url, LOGGER_DEBUG); logger('load: ' . $url, LOGGER_DEBUG);
$s = Network::fetchUrl($url); $fetchresult = Network::fetchUrlFull($url);
$s = $fetchresult->getBody();
logger('load: returns ' . $s, LOGGER_DATA); logger('load: returns ' . $s, LOGGER_DATA);
logger('load: return code: ' . Network::getCurl()->getCode(), LOGGER_DEBUG); logger('load: return code: ' . $fetchresult->getReturnCode(), LOGGER_DEBUG);
if ((Network::getCurl()->getCode() > 299) || (! $s)) { if (($fetchresult->getReturnCode() > 299) || (! $s)) {
return; return;
} }

View file

@ -193,7 +193,7 @@ class Salmon
return -1; return -1;
} }
if (($return_code == 503) && (stristr(Network::getCurl()->getHeaders(), 'retry-after'))) { if (($return_code == 503) && (stristr($postResult->getHeader(), 'retry-after'))) {
return -1; return -1;
} }

View file

@ -208,10 +208,6 @@ class Network
$curl_info = @curl_getinfo($ch); $curl_info = @curl_getinfo($ch);
} }
if (curl_errno($ch) !== CURLE_OK) {
logger('error fetching ' . $url . ': ' . curl_error($ch), LOGGER_INFO);
}
$curlResponse = new Curl($url, $s, $curl_info, curl_errno($ch), curl_error($ch)); $curlResponse = new Curl($url, $s, $curl_info, curl_errno($ch), curl_error($ch));
if ($curlResponse->isRedirectUrl()) { if ($curlResponse->isRedirectUrl()) {