]> git.mxchange.org Git - friendica.git/blobdiff - src/Factory/HTTPClientFactory.php
Merge pull request #10807 from annando/emoji-media
[friendica.git] / src / Factory / HTTPClientFactory.php
index 604b6fd62cf49f09d4bf82ae1e8ee4b9a18561f8..7db5ee7b921123b31b961f48d811b2703742ac2d 100644 (file)
@@ -5,11 +5,16 @@ namespace Friendica\Factory;
 use Friendica\App;
 use Friendica\BaseFactory;
 use Friendica\Core\Config\IConfig;
+use Friendica\Core\System;
 use Friendica\Network\HTTPClient;
 use Friendica\Network\IHTTPClient;
+use Friendica\Util\Crypto;
 use Friendica\Util\Profiler;
+use Friendica\Util\Strings;
 use GuzzleHttp\Client;
+use GuzzleHttp\HandlerStack;
 use GuzzleHttp\RequestOptions;
+use mattwright\URLResolver;
 use Psr\Http\Message\RequestInterface;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\UriInterface;
@@ -32,7 +37,14 @@ class HTTPClientFactory extends BaseFactory
                $this->baseUrl  = $baseUrl;
        }
 
-       public function createClient(): IHTTPClient
+       /**
+        * Creates a IHTTPClient for communications with HTTP endpoints
+        *
+        * @param HandlerStack|null $handlerStack (optional) A handler replacement (just usefull at test environments)
+        *
+        * @return IHTTPClient
+        */
+       public function createClient(HandlerStack $handlerStack = null): IHTTPClient
        {
                $proxy = $this->config->get('system', 'proxy');
 
@@ -51,9 +63,15 @@ class HTTPClientFactory extends BaseFactory
                        ResponseInterface $response,
                        UriInterface $uri
                ) use ($logger) {
-                       $logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri]);
+                       $logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri, 'method' => $request->getMethod()]);
                };
 
+               $userAgent = FRIENDICA_PLATFORM . " '" .
+                                        FRIENDICA_CODENAME . "' " .
+                                        FRIENDICA_VERSION . '-' .
+                                        DB_UPDATE_VERSION . '; ' .
+                                        $this->baseUrl->get();
+
                $guzzle = new Client([
                        RequestOptions::ALLOW_REDIRECTS => [
                                'max'            => 8,
@@ -71,17 +89,25 @@ class HTTPClientFactory extends BaseFactory
                        RequestOptions::CONNECT_TIMEOUT  => 10,
                        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,
+                       // but it can be overridden
+                       RequestOptions::VERIFY  => (bool)$this->config->get('system', 'verifyssl'),
+                       RequestOptions::PROXY   => $proxy,
+                       RequestOptions::HEADERS => [
+                               'User-Agent' => $userAgent,
+                       ],
+                       'handler' => $handlerStack ?? HandlerStack::create(),
                ]);
 
-               $userAgent = FRIENDICA_PLATFORM . " '" .
-                       FRIENDICA_CODENAME . "' " .
-                       FRIENDICA_VERSION . '-' .
-                       DB_UPDATE_VERSION . '; ' .
-                       $this->baseUrl->get();
+               $resolver = new URLResolver();
+               $resolver->setUserAgent($userAgent);
+               $resolver->setMaxRedirects(10);
+               $resolver->setRequestTimeout(10);
+               // if the file is too large then exit
+               $resolver->setMaxResponseDataSize(1000000);
+               // Designate a temporary file that will store cookies during the session.
+               // Some websites test the browser for cookie support, so this enhances results.
+               $resolver->setCookieJar(get_temppath() .'/resolver-cookie-' . Strings::getRandomName(10));
 
-               return new HTTPClient($logger, $this->profiler, $this->config, $userAgent, $guzzle);
+               return new HTTPClient($logger, $this->profiler, $guzzle, $resolver);
        }
 }