]> git.mxchange.org Git - friendica.git/commitdiff
Curl Response Refactoring
authorPhilipp Holzer <admin@philipp.info>
Wed, 10 Oct 2018 19:20:30 +0000 (21:20 +0200)
committerPhilipp Holzer <admin@philipp.info>
Wed, 10 Oct 2018 19:50:13 +0000 (21:50 +0200)
- refactored Network::getCurl()
- replaced every Network::getCur() execution with a Curl Response

mod/pubsubhubbub.php
src/Core/Install.php
src/Module/Proxy.php
src/Network/Curl.php
src/Object/Image.php
src/Protocol/Diaspora.php
src/Protocol/PortableContact.php
src/Protocol/Salmon.php
src/Util/Network.php

index 5697be8305bdb1c94e6f7a3be99a656bbff2b25f..d7b204e89c4f9622e081fd30507673eb729b94e2 100644 (file)
@@ -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) {
index ba767f3f9b9d33bff2382171bce32ad0dd4ac5b9..3ba683a56fb4540a4cc11e06ee09bb5a6225abaa 100644 (file)
@@ -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 {
index 14e842562f559b52947dfb6a3079f89ab99539a2..776dcccb35b63276ed4be75bcf1b1f89a52423f1 100644 (file)
@@ -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
index f67104bc0dd5d57a14fe48852678dad10ad6d53b..7cab25ee877333c2caa8f72b6c29ef6cb67e24de 100644 (file)
@@ -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;
 
index 166e150bf56366a44696d20b86e024596f77d17e..a76599223d0bbad165f72ee3611f5452869002c1 100644 (file)
@@ -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) {
index d3cda6c2da4c5333e5b08458d1916db6bf599751..eb52c05037d1f6db0c1d897f01105dc248d2a061 100644 (file)
@@ -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
index 8ce732bfcc6d53bb78deab410ae8032f3177e3b0..0e4d58d1c19348f67aa2af88510bcd2aa265c908 100644 (file)
@@ -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;
                }
 
index 02d08c3d4e99722c0208f32a8ed0cd54c7468a58..e3407844a66590b2e2e82c35f177c4285779b5ab 100644 (file)
@@ -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;
                }
 
index f8e4ee9e5bfe246292e40cb0d60175bb5ad03c92..00eaa80fa6f018045ddde723eae7b7382082ec06 100644 (file)
@@ -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()) {