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