From 9d00e4f1bc8f6ec95bd4ab450676ab039b2ee2f9 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Wed, 4 Mar 2020 22:11:01 +0100 Subject: [PATCH] Introduce HTPPRequest DI call and constructor --- src/DI.php | 12 +++++++ src/Network/HTTPRequest.php | 65 ++++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/DI.php b/src/DI.php index 9ed0c5b24..5986ca961 100644 --- a/src/DI.php +++ b/src/DI.php @@ -323,6 +323,18 @@ abstract class DI return self::$dice->create(Model\Storage\IStorage::class); } + // + // "Network" namespace + // + + /** + * @return Network\HTTPRequest + */ + public static function httpRequest() + { + return self::$dice->create(Network\HTTPRequest::class); + } + // // "Repository" namespace // diff --git a/src/Network/HTTPRequest.php b/src/Network/HTTPRequest.php index 6986a9359..7d7d59a6d 100644 --- a/src/Network/HTTPRequest.php +++ b/src/Network/HTTPRequest.php @@ -21,23 +21,44 @@ namespace Friendica\Network; +use Friendica\App; +use Friendica\Core\Config\IConfig; use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\DI; use Friendica\Util\Network; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; /** * Performs HTTP requests to a given URL */ class HTTPRequest { + /** @var LoggerInterface */ + private $logger; + /** @var Profiler */ + private $profiler; + /** @var IConfig */ + private $config; + /** @var string */ + private $userAgent; + + public function __construct(LoggerInterface $logger, Profiler $profiler, IConfig $config, App $a) + { + $this->logger = $logger; + $this->profiler = $profiler; + $this->config = $config; + $this->userAgent = $a->getUserAgent(); + } + /** * fetches an URL. * - * @param string $url URL to fetch - * @param bool $binary default false + * @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: + * @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 @@ -45,7 +66,7 @@ class HTTPRequest * '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 @@ -54,8 +75,6 @@ class HTTPRequest { $stamp1 = microtime(true); - $a = DI::app(); - if (strlen($url) > 1000) { Logger::log('URL is longer than 1000 characters. Callstack: ' . System::callstack(20), Logger::DEBUG); return CurlResult::createErrorCurl(substr($url, 0, 200)); @@ -200,11 +219,11 @@ class HTTPRequest /** * Send POST request to $url * - * @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 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 * * @return CurlResult The content * @throws \Friendica\Network\HTTPException\InternalServerErrorException @@ -313,13 +332,13 @@ class HTTPRequest * 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 + * @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 $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 string The fetched content * @throws \Friendica\Network\HTTPException\InternalServerErrorException @@ -337,13 +356,13 @@ class HTTPRequest * 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 + * @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 $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