]> git.mxchange.org Git - friendica.git/blob - src/Factory/HTTPClientFactory.php
Use mattwright/urlresolver for HTTPClient::finalUrl()
[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\Network\HTTPClient;
9 use Friendica\Network\IHTTPClient;
10 use Friendica\Util\Profiler;
11 use GuzzleHttp\Client;
12 use GuzzleHttp\RequestOptions;
13 use mattwright\URLResolver;
14 use Psr\Http\Message\RequestInterface;
15 use Psr\Http\Message\ResponseInterface;
16 use Psr\Http\Message\UriInterface;
17 use Psr\Log\LoggerInterface;
18
19 class HTTPClientFactory extends BaseFactory
20 {
21         /** @var IConfig */
22         private $config;
23         /** @var Profiler */
24         private $profiler;
25         /** @var App\BaseURL */
26         private $baseUrl;
27
28         public function __construct(LoggerInterface $logger, IConfig $config, Profiler $profiler, App\BaseURL $baseUrl)
29         {
30                 parent::__construct($logger);
31                 $this->config   = $config;
32                 $this->profiler = $profiler;
33                 $this->baseUrl  = $baseUrl;
34         }
35
36         public function createClient(): IHTTPClient
37         {
38                 $proxy = $this->config->get('system', 'proxy');
39
40                 if (!empty($proxy)) {
41                         $proxyuser = $this->config->get('system', 'proxyuser');
42
43                         if (!empty($proxyuser)) {
44                                 $proxy = $proxyuser . '@' . $proxy;
45                         }
46                 }
47
48                 $logger = $this->logger;
49
50                 $onRedirect = function (
51                         RequestInterface $request,
52                         ResponseInterface $response,
53                         UriInterface $uri
54                 ) use ($logger) {
55                         $logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri, 'method' => $request->getMethod()]);
56                 };
57
58                 $userAgent = FRIENDICA_PLATFORM . " '" .
59                                          FRIENDICA_CODENAME . "' " .
60                                          FRIENDICA_VERSION . '-' .
61                                          DB_UPDATE_VERSION . '; ' .
62                                          $this->baseUrl->get();
63
64                 $guzzle = new Client([
65                         RequestOptions::ALLOW_REDIRECTS => [
66                                 'max'            => 8,
67                                 'on_redirect'    => $onRedirect,
68                                 'track_redirect' => true,
69                                 'strict'         => true,
70                                 'referer'        => true,
71                         ],
72                         RequestOptions::HTTP_ERRORS => false,
73                         // Without this setting it seems as if some webservers send compressed content
74                         // This seems to confuse curl so that it shows this uncompressed.
75                         /// @todo  We could possibly set this value to "gzip" or something similar
76                         RequestOptions::DECODE_CONTENT   => '',
77                         RequestOptions::FORCE_IP_RESOLVE => ($this->config->get('system', 'ipv4_resolve') ? 'v4' : null),
78                         RequestOptions::CONNECT_TIMEOUT  => 10,
79                         RequestOptions::TIMEOUT          => $this->config->get('system', 'curl_timeout', 60),
80                         // by default we will allow self-signed certs
81                         // but you can override this
82                         RequestOptions::VERIFY  => (bool)$this->config->get('system', 'verifyssl'),
83                         RequestOptions::PROXY   => $proxy,
84                         RequestOptions::HEADERS => [
85                                 'User-Agent' => $userAgent,
86                         ],
87                 ]);
88
89                 $resolver = new URLResolver();
90                 $resolver->setUserAgent($userAgent);
91                 $resolver->setMaxRedirects(10);
92                 $resolver->setRequestTimeout(10);
93                 // if the file is too large then exit
94                 $resolver->setMaxResponseDataSize(1000000);
95
96                 return new HTTPClient($logger, $this->profiler, $guzzle, $resolver);
97         }
98 }