3 namespace Friendica\Factory;
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;
19 class HTTPClientFactory extends BaseFactory
25 /** @var App\BaseURL */
28 public function __construct(LoggerInterface $logger, IConfig $config, Profiler $profiler, App\BaseURL $baseUrl)
30 parent::__construct($logger);
31 $this->config = $config;
32 $this->profiler = $profiler;
33 $this->baseUrl = $baseUrl;
36 public function createClient(): IHTTPClient
38 $proxy = $this->config->get('system', 'proxy');
41 $proxyuser = $this->config->get('system', 'proxyuser');
43 if (!empty($proxyuser)) {
44 $proxy = $proxyuser . '@' . $proxy;
48 $logger = $this->logger;
50 $onRedirect = function (
51 RequestInterface $request,
52 ResponseInterface $response,
55 $logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri, 'method' => $request->getMethod()]);
58 $userAgent = FRIENDICA_PLATFORM . " '" .
59 FRIENDICA_CODENAME . "' " .
60 FRIENDICA_VERSION . '-' .
61 DB_UPDATE_VERSION . '; ' .
62 $this->baseUrl->get();
64 $guzzle = new Client([
65 RequestOptions::ALLOW_REDIRECTS => [
67 'on_redirect' => $onRedirect,
68 'track_redirect' => true,
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,
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);
96 return new HTTPClient($logger, $this->profiler, $guzzle, $resolver);