]> git.mxchange.org Git - friendica.git/commitdiff
Introduce interface "IHTTPRequest" (rely on abstractions, not on concrete implementat...
authornupplaPhil <admin+github@philipp.info>
Wed, 4 Mar 2020 21:56:16 +0000 (22:56 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 21 Jul 2020 07:17:05 +0000 (03:17 -0400)
src/DI.php
src/Network/HTTPRequest.php
src/Network/IHTTPRequest.php [new file with mode: 0644]
static/dependencies.config.php

index 5986ca96152d7a599f82b9a73d9bc407ab9ba6ab..39e892adcb26e12121bea51991d409af5d4397d4 100644 (file)
@@ -328,11 +328,11 @@ abstract class DI
        //
 
        /**
-        * @return Network\HTTPRequest
+        * @return Network\IHTTPRequest
         */
        public static function httpRequest()
        {
-               return self::$dice->create(Network\HTTPRequest::class);
+               return self::$dice->create(Network\IHTTPRequest::class);
        }
 
        //
index c751406c1bf911de72f62c84a3e3ce66e0222f01..839586880ca1b9782ddc8090b0b5a2d712d1d352 100644 (file)
@@ -33,7 +33,7 @@ use Psr\Log\LoggerInterface;
 /**
  * Performs HTTP requests to a given URL
  */
-class HTTPRequest
+class HTTPRequest implements IHTTPRequest
 {
        /** @var LoggerInterface */
        private $logger;
@@ -53,22 +53,10 @@ class HTTPRequest
        }
 
        /**
-        * fetches an URL.
+        * {@inheritDoc}
         *
-        * @param string $url        URL to fetch
-        * @param bool   $binary     default false
-        *                           TRUE if asked to return binary results (file download)
-        * @param array  $opts       (optional parameters) assoziative array with:
-        *                           'accept_content' => supply Accept: header with 'accept_content' as the value
-        *                           'timeout' => int Timeout in seconds, default system config value or 60 seconds
-        *                           'http_auth' => username:password
-        *                           'novalidate' => do not validate SSL certs, default is to validate using our CA list
-        *                           'nobody' => only return the header
-        *                           'cookiejar' => path to cookie jar file
-        *                           'header' => header array
-        * @param int    $redirects  The recursion counter for internal use - default 0
+        * @param int $redirects The recursion counter for internal use - default 0
         *
-        * @return CurlResult
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public function get(string $url, bool $binary = false, array $opts = [], int &$redirects = 0)
@@ -217,15 +205,10 @@ class HTTPRequest
        }
 
        /**
-        * Send POST request to $url
+        * {@inheritDoc}
         *
-        * @param string $url       URL to post
-        * @param mixed  $params    array of POST variables
-        * @param array  $headers   HTTP headers
-        * @param int    $redirects Recursion counter for internal use - default = 0
-        * @param int    $timeout   The timeout in seconds, default system config value or 60 seconds
+        * @param int $redirects The recursion counter for internal use - default 0
         *
-        * @return CurlResult The content
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public function post(string $url, $params, array $headers = [], int $timeout = 0, int &$redirects = 0)
@@ -325,20 +308,7 @@ class HTTPRequest
        }
 
        /**
-        * Returns the original URL of the provided URL
-        *
-        * This function strips tracking query params and follows redirections, either
-        * through HTTP code or meta refresh tags. Stops after 10 redirections.
-        *
-        * @todo  Remove the $fetchbody parameter that generates an extraneous HEAD request
-        *
-        * @see   ParseUrl::getSiteinfo
-        *
-        * @param string $url       A user-submitted URL
-        * @param int    $depth     The current redirection recursion level (internal)
-        * @param bool   $fetchbody Wether to fetch the body or not after the HEAD requests
-        * @return string A canonical URL
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * {@inheritDoc}
         */
        public function finalUrl(string $url, int $depth = 1, bool $fetchbody = false)
        {
@@ -443,21 +413,10 @@ class HTTPRequest
        }
 
        /**
-        * Curl wrapper
-        *
-        * If binary flag is true, return binary results.
-        * Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt")
-        * to preserve cookies from one request to the next.
+        * {@inheritDoc}
         *
-        * @param string $url             URL to fetch
-        * @param bool   $binary          default false
-        *                                TRUE if asked to return binary results (file download)
-        * @param int    $timeout         Timeout in seconds, default system config value or 60 seconds
-        * @param string $accept_content  supply Accept: header with 'accept_content' as the value
-        * @param string $cookiejar       Path to cookie jar file
-        * @param int    $redirects       The recursion counter for internal use - default 0
+        * @param int $redirects The recursion counter for internal use - default 0
         *
-        * @return string The fetched content
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public function fetch(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0)
@@ -468,20 +427,10 @@ class HTTPRequest
        }
 
        /**
-        * Curl wrapper with array of return values.
+        * {@inheritDoc}
         *
-        * Inner workings and parameters are the same as @ref fetchUrl but returns an array with
-        * all the information collected during the fetch.
+        * @param int $redirects The recursion counter for internal use - default 0
         *
-        * @param string $url             URL to fetch
-        * @param bool   $binary          default false
-        *                                TRUE if asked to return binary results (file download)
-        * @param int    $timeout         Timeout in seconds, default system config value or 60 seconds
-        * @param string $accept_content  supply Accept: header with 'accept_content' as the value
-        * @param string $cookiejar       Path to cookie jar file
-        * @param int    $redirects       The recursion counter for internal use - default 0
-        *
-        * @return CurlResult With all relevant information, 'body' contains the actual fetched content.
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public function fetchFull(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0)
@@ -499,9 +448,7 @@ class HTTPRequest
        }
 
        /**
-        * Returns the current UserAgent as a String
-        *
-        * @return string the UserAgent as a String
+        * {@inheritDoc}
         */
        public function getUserAgent()
        {
diff --git a/src/Network/IHTTPRequest.php b/src/Network/IHTTPRequest.php
new file mode 100644 (file)
index 0000000..3ebcc5d
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU APGL 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/>.
+ *
+ */
+
+namespace Friendica\Network;
+
+/**
+ * Interface for calling HTTP requests and returning their responses
+ */
+interface IHTTPRequest
+{
+       /**
+        * Fetches the content of an URL
+        *
+        * If binary flag is true, return binary results.
+        * Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt")
+        * to preserve cookies from one request to the next.
+        *
+        * @param string $url             URL to fetch
+        * @param bool   $binary          default false
+        *                                TRUE if asked to return binary results (file download)
+        * @param int    $timeout         Timeout in seconds, default system config value or 60 seconds
+        * @param string $accept_content  supply Accept: header with 'accept_content' as the value
+        * @param string $cookiejar       Path to cookie jar file
+        *
+        * @return string The fetched content
+        */
+       public function fetch(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '');
+
+       /**
+        * Fetches the whole response of an URL.
+        *
+        * Inner workings and parameters are the same as @ref fetchUrl but returns an array with
+        * all the information collected during the fetch.
+        *
+        * @param string $url             URL to fetch
+        * @param bool   $binary          default false
+        *                                TRUE if asked to return binary results (file download)
+        * @param int    $timeout         Timeout in seconds, default system config value or 60 seconds
+        * @param string $accept_content  supply Accept: header with 'accept_content' as the value
+        * @param string $cookiejar       Path to cookie jar file
+        *
+        * @return CurlResult With all relevant information, 'body' contains the actual fetched content.
+        */
+       public function fetchFull(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '');
+
+       /**
+        * Send a GET to an URL.
+        *
+        * @param string $url        URL to fetch
+        * @param bool   $binary     default false
+        *                           TRUE if asked to return binary results (file download)
+        * @param array  $opts       (optional parameters) assoziative array with:
+        *                           'accept_content' => supply Accept: header with 'accept_content' as the value
+        *                           'timeout' => int Timeout in seconds, default system config value or 60 seconds
+        *                           'http_auth' => username:password
+        *                           'novalidate' => do not validate SSL certs, default is to validate using our CA list
+        *                           'nobody' => only return the header
+        *                           'cookiejar' => path to cookie jar file
+        *                           'header' => header array
+        *
+        * @return CurlResult
+        */
+       public function get(string $url, bool $binary = false, array $opts = []);
+
+       /**
+        * Send POST request to an URL
+        *
+        * @param string $url     URL to post
+        * @param mixed  $params  array of POST variables
+        * @param array  $headers HTTP headers
+        * @param int    $timeout The timeout in seconds, default system config value or 60 seconds
+        *
+        * @return CurlResult The content
+        */
+       public function post(string $url, $params, array $headers = [], int $timeout = 0);
+
+       /**
+        * Returns the original URL of the provided URL
+        *
+        * This function strips tracking query params and follows redirections, either
+        * through HTTP code or meta refresh tags. Stops after 10 redirections.
+        *
+        * @param string $url       A user-submitted URL
+        * @param int    $depth     The current redirection recursion level (internal)
+        * @param bool   $fetchbody Wether to fetch the body or not after the HEAD requests
+        *
+        * @return string A canonical URL
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @see   ParseUrl::getSiteinfo
+        *
+        * @todo  Remove the $fetchbody parameter that generates an extraneous HEAD request
+        */
+       public function finalUrl(string $url, int $depth = 1, bool $fetchbody = false);
+
+       /**
+        * Returns the current UserAgent as a String
+        *
+        * @return string the UserAgent as a String
+        */
+       public function getUserAgent();
+}
index 84344a60e28a660493540420514b6f9b60437cda..fe8a8caee07925e7c1b5b70b8f60912ee69c9805 100644 (file)
@@ -46,6 +46,7 @@ use Friendica\Database\Database;
 use Friendica\Factory;
 use Friendica\Model\Storage\IStorage;
 use Friendica\Model\User\Cookie;
+use Friendica\Network;
 use Friendica\Util;
 use Psr\Log\LoggerInterface;
 
@@ -219,4 +220,7 @@ return [
                        ['getBackend', [], Dice::CHAIN_CALL],
                ],
        ],
+       Network\IHTTPRequest::class => [
+               'instanceOf' => Network\HTTPRequest::class,
+       ]
 ];