]> git.mxchange.org Git - friendica.git/blob - src/Factory/HTTPClientFactory.php
Merge pull request #10807 from annando/emoji-media
[friendica.git] / src / Factory / HTTPClientFactory.php
1 <?php
2
3 namespace Friendica\Factory;
4
5 use Friendica\App;
6 use Friendica\BaseFactory;
7 use Friendica\Core\Config\IConfig;
8 use Friendica\Core\System;
9 use Friendica\Network\HTTPClient;
10 use Friendica\Network\IHTTPClient;
11 use Friendica\Util\Crypto;
12 use Friendica\Util\Profiler;
13 use Friendica\Util\Strings;
14 use GuzzleHttp\Client;
15 use GuzzleHttp\HandlerStack;
16 use GuzzleHttp\RequestOptions;
17 use mattwright\URLResolver;
18 use Psr\Http\Message\RequestInterface;
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\UriInterface;
21 use Psr\Log\LoggerInterface;
22
23 class HTTPClientFactory extends BaseFactory
24 {
25         /** @var IConfig */
26         private $config;
27         /** @var Profiler */
28         private $profiler;
29         /** @var App\BaseURL */
30         private $baseUrl;
31
32         public function __construct(LoggerInterface $logger, IConfig $config, Profiler $profiler, App\BaseURL $baseUrl)
33         {
34                 parent::__construct($logger);
35                 $this->config   = $config;
36                 $this->profiler = $profiler;
37                 $this->baseUrl  = $baseUrl;
38         }
39
40         /**
41          * Creates a IHTTPClient for communications with HTTP endpoints
42          *
43          * @param HandlerStack|null $handlerStack (optional) A handler replacement (just usefull at test environments)
44          *
45          * @return IHTTPClient
46          */
47         public function createClient(HandlerStack $handlerStack = null): IHTTPClient
48         {
49                 $proxy = $this->config->get('system', 'proxy');
50
51                 if (!empty($proxy)) {
52                         $proxyuser = $this->config->get('system', 'proxyuser');
53
54                         if (!empty($proxyuser)) {
55                                 $proxy = $proxyuser . '@' . $proxy;
56                         }
57                 }
58
59                 $logger = $this->logger;
60
61                 $onRedirect = function (
62                         RequestInterface $request,
63                         ResponseInterface $response,
64                         UriInterface $uri
65                 ) use ($logger) {
66                         $logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri, 'method' => $request->getMethod()]);
67                 };
68
69                 $userAgent = FRIENDICA_PLATFORM . " '" .
70                                          FRIENDICA_CODENAME . "' " .
71                                          FRIENDICA_VERSION . '-' .
72                                          DB_UPDATE_VERSION . '; ' .
73                                          $this->baseUrl->get();
74
75                 $guzzle = new Client([
76                         RequestOptions::ALLOW_REDIRECTS => [
77                                 'max'            => 8,
78                                 'on_redirect'    => $onRedirect,
79                                 'track_redirect' => true,
80                                 'strict'         => true,
81                                 'referer'        => true,
82                         ],
83                         RequestOptions::HTTP_ERRORS => false,
84                         // Without this setting it seems as if some webservers send compressed content
85                         // This seems to confuse curl so that it shows this uncompressed.
86                         /// @todo  We could possibly set this value to "gzip" or something similar
87                         RequestOptions::DECODE_CONTENT   => '',
88                         RequestOptions::FORCE_IP_RESOLVE => ($this->config->get('system', 'ipv4_resolve') ? 'v4' : null),
89                         RequestOptions::CONNECT_TIMEOUT  => 10,
90                         RequestOptions::TIMEOUT          => $this->config->get('system', 'curl_timeout', 60),
91                         // by default we will allow self-signed certs
92                         // but it can be overridden
93                         RequestOptions::VERIFY  => (bool)$this->config->get('system', 'verifyssl'),
94                         RequestOptions::PROXY   => $proxy,
95                         RequestOptions::HEADERS => [
96                                 'User-Agent' => $userAgent,
97                         ],
98                         'handler' => $handlerStack ?? HandlerStack::create(),
99                 ]);
100
101                 $resolver = new URLResolver();
102                 $resolver->setUserAgent($userAgent);
103                 $resolver->setMaxRedirects(10);
104                 $resolver->setRequestTimeout(10);
105                 // if the file is too large then exit
106                 $resolver->setMaxResponseDataSize(1000000);
107                 // Designate a temporary file that will store cookies during the session.
108                 // Some websites test the browser for cookie support, so this enhances results.
109                 $resolver->setCookieJar(get_temppath() .'/resolver-cookie-' . Strings::getRandomName(10));
110
111                 return new HTTPClient($logger, $this->profiler, $guzzle, $resolver);
112         }
113 }