Introduce HTPPRequest DI call and constructor

This commit is contained in:
nupplaPhil 2020-03-04 22:11:01 +01:00 committed by Hypolite Petovan
parent 5344efef71
commit 9d00e4f1bc
2 changed files with 54 additions and 23 deletions

View file

@ -323,6 +323,18 @@ abstract class DI
return self::$dice->create(Model\Storage\IStorage::class); 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 // "Repository" namespace
// //

View file

@ -21,23 +21,44 @@
namespace Friendica\Network; namespace Friendica\Network;
use Friendica\App;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\DI; use Friendica\DI;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/** /**
* Performs HTTP requests to a given URL * Performs HTTP requests to a given URL
*/ */
class HTTPRequest 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. * fetches an URL.
* *
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false * @param bool $binary default false
* TRUE if asked to return binary results (file download) * 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 * 'accept_content' => supply Accept: header with 'accept_content' as the value
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
* 'http_auth' => username:password * 'http_auth' => username:password
@ -45,7 +66,7 @@ class HTTPRequest
* 'nobody' => only return the header * 'nobody' => only return the header
* 'cookiejar' => path to cookie jar file * 'cookiejar' => path to cookie jar file
* 'header' => header array * '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 * @return CurlResult
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
@ -54,8 +75,6 @@ class HTTPRequest
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$a = DI::app();
if (strlen($url) > 1000) { if (strlen($url) > 1000) {
Logger::log('URL is longer than 1000 characters. Callstack: ' . System::callstack(20), Logger::DEBUG); Logger::log('URL is longer than 1000 characters. Callstack: ' . System::callstack(20), Logger::DEBUG);
return CurlResult::createErrorCurl(substr($url, 0, 200)); return CurlResult::createErrorCurl(substr($url, 0, 200));
@ -200,11 +219,11 @@ class HTTPRequest
/** /**
* Send POST request to $url * Send POST request to $url
* *
* @param string $url URL to post * @param string $url URL to post
* @param mixed $params array of POST variables * @param mixed $params array of POST variables
* @param array $headers HTTP headers * @param array $headers HTTP headers
* @param int $redirects Recursion counter for internal use - default = 0 * @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 $timeout The timeout in seconds, default system config value or 60 seconds
* *
* @return CurlResult The content * @return CurlResult The content
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
@ -313,13 +332,13 @@ class HTTPRequest
* Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt") * Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt")
* to preserve cookies from one request to the next. * to preserve cookies from one request to the next.
* *
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false * @param bool $binary default false
* TRUE if asked to return binary results (file download) * TRUE if asked to return binary results (file download)
* @param int $timeout Timeout in seconds, default system config value or 60 seconds * @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 $accept_content supply Accept: header with 'accept_content' as the value
* @param string $cookiejar Path to cookie jar file * @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 * @return string The fetched content
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @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 * Inner workings and parameters are the same as @ref fetchUrl but returns an array with
* all the information collected during the fetch. * all the information collected during the fetch.
* *
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false * @param bool $binary default false
* TRUE if asked to return binary results (file download) * TRUE if asked to return binary results (file download)
* @param int $timeout Timeout in seconds, default system config value or 60 seconds * @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 $accept_content supply Accept: header with 'accept_content' as the value
* @param string $cookiejar Path to cookie jar file * @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 CurlResult With all relevant information, 'body' contains the actual fetched content. * @return CurlResult With all relevant information, 'body' contains the actual fetched content.
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException