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