From ffbc6887977c34072e2a3043486a1b0e768a9844 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Wed, 10 Oct 2018 21:20:30 +0200 Subject: [PATCH] Curl Response Refactoring - refactored Network::getCurl() - replaced every Network::getCur() execution with a Curl Response --- mod/pubsubhubbub.php | 5 +++-- src/Core/Install.php | 12 ++++++------ src/Module/Proxy.php | 5 +++-- src/Network/Curl.php | 18 +++++++++--------- src/Object/Image.php | 5 +++-- src/Protocol/Diaspora.php | 2 +- src/Protocol/PortableContact.php | 7 ++++--- src/Protocol/Salmon.php | 2 +- src/Util/Network.php | 4 ---- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/mod/pubsubhubbub.php b/mod/pubsubhubbub.php index 5697be8305..d7b204e89c 100644 --- a/mod/pubsubhubbub.php +++ b/mod/pubsubhubbub.php @@ -104,8 +104,9 @@ function pubsubhubbub_init(App $a) { // we don't actually enforce the lease time because GNU // Social/StatusNet doesn't honour it (yet) - $body = Network::fetchUrl($hub_callback . "?" . $params); - $ret = Network::getCurl()->getCode(); + $fetchResult = Network::fetchUrlFull($hub_callback . "?" . $params); + $body = $fetchResult->getBody(); + $ret = $fetchResult->getReturnCode(); // give up if the HTTP return code wasn't a success (2xx) if ($ret < 200 || $ret > 299) { diff --git a/src/Core/Install.php b/src/Core/Install.php index ba767f3f9b..3ba683a56f 100644 --- a/src/Core/Install.php +++ b/src/Core/Install.php @@ -345,20 +345,20 @@ class Install extends BaseObject $help = ""; $error_msg = ""; 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"); - if ($test['body'] != "ok") { - $test = Network::fetchUrlFull($url); + if ($fetchResult->getBody() != "ok") { + $fetchResult = Network::fetchUrlFull($url); } - if ($test['body'] != "ok") { + if ($fetchResult->getBody() != "ok") { $status = false; $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.'); $error_msg = []; $error_msg['head'] = L10n::t('Error message from Curl when fetching'); - $error_msg['url'] = $test['redirect_url']; - $error_msg['msg'] = defaults($test, 'error', ''); + $error_msg['url'] = $fetchResult->getRedirectUrl(); + $error_msg['msg'] = $fetchResult->getError(); } self::addCheck($checks, L10n::t('Url rewrite is working'), $status, true, $help, $error_msg); } else { diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index 14e842562f..776dcccb35 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -188,7 +188,8 @@ class Proxy extends BaseModule // It shouldn't happen but it does - spaces in URL $_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']); $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'); file_put_contents($tempfile, $img_str); @@ -196,7 +197,7 @@ class Proxy extends BaseModule unlink($tempfile); // 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'); $mime = 'image/png'; $cachefile = ''; // Clear the cachefile so that the dummy isn't stored diff --git a/src/Network/Curl.php b/src/Network/Curl.php index f67104bc0d..7cab25ee87 100644 --- a/src/Network/Curl.php +++ b/src/Network/Curl.php @@ -61,12 +61,12 @@ class Curl private $isTimeout; /** - * @var int optional error numer + * @var int the error number or 0 (zero) if no error */ private $errorNumber; /** - * @var string optional error message + * @var string the error message or '' (the empty string) if no */ private $error; @@ -82,7 +82,7 @@ class Curl return new Curl( $url, '', - ['http_code' => 0] + [ 'http_code' => 0 ] ); } @@ -98,7 +98,7 @@ class Curl */ 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'); } @@ -135,10 +135,10 @@ class Curl 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) { - 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); } @@ -151,15 +151,15 @@ class Curl private function checkRedirect() { - if (empty($this->info['url'])) { + if (!array_key_exists('url', $this->info)) { $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'])); + $new_location_info = (!array_key_exists('redirect_url', $this->info) ? '' : @parse_url($this->info['redirect_url'])); + $old_location_info = (!array_key_exists('url', $this->info) ? '' : @parse_url($this->info['url'])); $this->redirectUrl = $new_location_info; diff --git a/src/Object/Image.php b/src/Object/Image.php index 166e150bf5..a76599223d 100644 --- a/src/Object/Image.php +++ b/src/Object/Image.php @@ -720,17 +720,18 @@ class Image * * @param string $filename Image filename * @param boolean $fromcurl Check Content-Type header from curl request + * @param string $header passed headers to take into account * * @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); $type = null; if ($fromcurl) { $a = get_app(); $headers=[]; - $h = explode("\n", Network::getCurl()->getHeaders()); + $h = explode("\n", $header); foreach ($h as $l) { $data = array_map("trim", explode(":", trim($l), 2)); if (count($data) > 1) { diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index d3cda6c2da..eb52c05037 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3089,7 +3089,7 @@ class Diaspora 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)) { logger("queue message"); // queue message for redelivery diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index 8ce732bfcc..0e4d58d1c1 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -86,13 +86,14 @@ class PortableContact logger('load: ' . $url, LOGGER_DEBUG); - $s = Network::fetchUrl($url); + $fetchresult = Network::fetchUrlFull($url); + $s = $fetchresult->getBody(); 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; } diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php index 02d08c3d4e..e3407844a6 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -193,7 +193,7 @@ class Salmon return -1; } - if (($return_code == 503) && (stristr(Network::getCurl()->getHeaders(), 'retry-after'))) { + if (($return_code == 503) && (stristr($postResult->getHeader(), 'retry-after'))) { return -1; } diff --git a/src/Util/Network.php b/src/Util/Network.php index f8e4ee9e5b..00eaa80fa6 100644 --- a/src/Util/Network.php +++ b/src/Util/Network.php @@ -208,10 +208,6 @@ class Network $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)); if ($curlResponse->isRedirectUrl()) {