]> git.mxchange.org Git - friendica.git/commitdiff
Refactor HTTPClient::get() / ::head()
authorPhilipp <admin@philipp.info>
Mon, 23 Aug 2021 11:44:46 +0000 (13:44 +0200)
committerPhilipp <admin@philipp.info>
Wed, 25 Aug 2021 12:22:42 +0000 (14:22 +0200)
src/Factory/HTTPClientFactory.php
src/Network/HTTPClient.php

index 604b6fd62cf49f09d4bf82ae1e8ee4b9a18561f8..b17e065326b53640801e576af173f61a5e079ec5 100644 (file)
@@ -54,6 +54,12 @@ class HTTPClientFactory extends BaseFactory
                        $logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri]);
                };
 
+               $userAgent = FRIENDICA_PLATFORM . " '" .
+                                        FRIENDICA_CODENAME . "' " .
+                                        FRIENDICA_VERSION . '-' .
+                                        DB_UPDATE_VERSION . '; ' .
+                                        $this->baseUrl->get();
+
                $guzzle = new Client([
                        RequestOptions::ALLOW_REDIRECTS => [
                                'max'            => 8,
@@ -72,16 +78,13 @@ class HTTPClientFactory extends BaseFactory
                        RequestOptions::TIMEOUT          => $this->config->get('system', 'curl_timeout', 60),
                        // by default we will allow self-signed certs
                        // but you can override this
-                       RequestOptions::VERIFY => (bool)$this->config->get('system', 'verifyssl'),
-                       RequestOptions::PROXY  => $proxy,
+                       RequestOptions::VERIFY  => (bool)$this->config->get('system', 'verifyssl'),
+                       RequestOptions::PROXY   => $proxy,
+                       RequestOptions::HEADERS => [
+                               'User-Agent' => $userAgent,
+                       ],
                ]);
 
-               $userAgent = FRIENDICA_PLATFORM . " '" .
-                       FRIENDICA_CODENAME . "' " .
-                       FRIENDICA_VERSION . '-' .
-                       DB_UPDATE_VERSION . '; ' .
-                       $this->baseUrl->get();
-
                return new HTTPClient($logger, $this->profiler, $this->config, $userAgent, $guzzle);
        }
 }
index e20ddd3c9ed343a0a3511520d88431357e957c9a..3011696b8bdfa5291c1116010667e245d4f37bec 100644 (file)
@@ -60,7 +60,10 @@ class HTTPClient implements IHTTPClient
                $this->client    = $client;
        }
 
-       protected function request(string $method, string $url, array $opts = [])
+       /**
+        * @throws HTTPException\InternalServerErrorException
+        */
+       protected function request(string $method, string $url, array $opts = []): IHTTPResult
        {
                $this->profiler->startRecording('network');
 
@@ -96,30 +99,32 @@ class HTTPClient implements IHTTPClient
                $conf = [];
 
                if (!empty($opts['cookiejar'])) {
-                       $jar = new FileCookieJar($opts['cookiejar']);
+                       $jar                           = new FileCookieJar($opts['cookiejar']);
                        $conf[RequestOptions::COOKIES] = $jar;
                }
 
+               $header = [];
+
                if (!empty($opts['accept_content'])) {
-                       array_push($curlOptions[CURLOPT_HTTPHEADER], 'Accept: ' . $opts['accept_content']);
+                       array_push($header, 'Accept: ' . $opts['accept_content']);
                }
 
                if (!empty($opts['header'])) {
-                       $curlOptions[CURLOPT_HTTPHEADER] = array_merge($opts['header'], $curlOptions[CURLOPT_HTTPHEADER]);
+                       $header = array_merge($opts['header'], $header);
                }
 
-               $curlOptions[CURLOPT_USERAGENT] = $this->userAgent;
-
                if (!empty($opts['headers'])) {
                        $this->logger->notice('Wrong option \'headers\' used.');
-                       $curlOptions[CURLOPT_HTTPHEADER] = array_merge($opts['headers'], $curlOptions[CURLOPT_HTTPHEADER]);
+                       $header = array_merge($opts['headers'], $header);
                }
 
+               $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $header);
+
                if (!empty($opts['timeout'])) {
-                       $curlOptions[CURLOPT_TIMEOUT] = $opts['timeout'];
+                       $conf[RequestOptions::TIMEOUT] = $opts['timeout'];
                }
 
-               $onHeaders = function (ResponseInterface $response) use ($opts) {
+               $conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
                        if (!empty($opts['content_length']) &&
                                $response->getHeaderLine('Content-Length') > $opts['content_length']) {
                                throw new TransferException('The file is too big!');
@@ -127,10 +132,7 @@ class HTTPClient implements IHTTPClient
                };
 
                try {
-                       $response = $this->client->$method($url, [
-                               'on_headers' => $onHeaders,
-                               'curl'       => $curlOptions,
-                       ]);
+                       $response = $this->client->$method($url, $conf);
                        return new GuzzleResponse($response, $url);
                } catch (TransferException $exception) {
                        if ($exception instanceof RequestException &&
@@ -148,7 +150,7 @@ class HTTPClient implements IHTTPClient
         *
         * @throws HTTPException\InternalServerErrorException
         */
-       public function head(string $url, array $opts = [])
+       public function head(string $url, array $opts = []): IHTTPResult
        {
                return $this->request('head', $url, $opts);
        }
@@ -156,7 +158,7 @@ class HTTPClient implements IHTTPClient
        /**
         * {@inheritDoc}
         */
-       public function get(string $url, array $opts = [])
+       public function get(string $url, array $opts = []): IHTTPResult
        {
                return $this->request('get', $url, $opts);
        }