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\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;
20 class HTTPClientFactory extends BaseFactory
26 /** @var App\BaseURL */
29 public function __construct(LoggerInterface $logger, IConfig $config, Profiler $profiler, App\BaseURL $baseUrl)
31 parent::__construct($logger);
32 $this->config = $config;
33 $this->profiler = $profiler;
34 $this->baseUrl = $baseUrl;
38 * Creates a IHTTPClient for communications with HTTP endpoints
40 * @param HandlerStack|null $handlerStack (optional) A handler replacement (just usefull at test environments)
44 public function createClient(HandlerStack $handlerStack = null): IHTTPClient
46 $proxy = $this->config->get('system', 'proxy');
49 $proxyuser = $this->config->get('system', 'proxyuser');
51 if (!empty($proxyuser)) {
52 $proxy = $proxyuser . '@' . $proxy;
56 $logger = $this->logger;
58 $onRedirect = function (
59 RequestInterface $request,
60 ResponseInterface $response,
63 $logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri, 'method' => $request->getMethod()]);
66 $userAgent = FRIENDICA_PLATFORM . " '" .
67 FRIENDICA_CODENAME . "' " .
68 FRIENDICA_VERSION . '-' .
69 DB_UPDATE_VERSION . '; ' .
70 $this->baseUrl->get();
72 $guzzle = new Client([
73 RequestOptions::ALLOW_REDIRECTS => [
75 'on_redirect' => $onRedirect,
76 'track_redirect' => true,
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,
95 'handler' => $handlerStack ?? HandlerStack::create(),
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);
105 return new HTTPClient($logger, $this->profiler, $guzzle, $resolver);