2021-08-22 22:14:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Factory;
|
|
|
|
|
|
|
|
use Friendica\App;
|
|
|
|
use Friendica\BaseFactory;
|
|
|
|
use Friendica\Core\Config\IConfig;
|
|
|
|
use Friendica\Network\HTTPClient;
|
|
|
|
use Friendica\Network\IHTTPClient;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use GuzzleHttp\Client;
|
2021-08-24 22:13:50 +00:00
|
|
|
use GuzzleHttp\HandlerStack;
|
2021-08-22 22:14:18 +00:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2021-08-23 12:28:25 +00:00
|
|
|
use mattwright\URLResolver;
|
2021-08-22 22:14:18 +00:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\UriInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
class HTTPClientFactory extends BaseFactory
|
|
|
|
{
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var Profiler */
|
|
|
|
private $profiler;
|
|
|
|
/** @var App\BaseURL */
|
|
|
|
private $baseUrl;
|
|
|
|
|
|
|
|
public function __construct(LoggerInterface $logger, IConfig $config, Profiler $profiler, App\BaseURL $baseUrl)
|
|
|
|
{
|
|
|
|
parent::__construct($logger);
|
|
|
|
$this->config = $config;
|
|
|
|
$this->profiler = $profiler;
|
|
|
|
$this->baseUrl = $baseUrl;
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:13:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a IHTTPClient for communications with HTTP endpoints
|
|
|
|
*
|
|
|
|
* @param HandlerStack|null $handlerStack (optional) A handler replacement (just usefull at test environments)
|
|
|
|
*
|
|
|
|
* @return IHTTPClient
|
|
|
|
*/
|
|
|
|
public function createClient(HandlerStack $handlerStack = null): IHTTPClient
|
2021-08-22 22:14:18 +00:00
|
|
|
{
|
|
|
|
$proxy = $this->config->get('system', 'proxy');
|
|
|
|
|
|
|
|
if (!empty($proxy)) {
|
|
|
|
$proxyuser = $this->config->get('system', 'proxyuser');
|
|
|
|
|
|
|
|
if (!empty($proxyuser)) {
|
|
|
|
$proxy = $proxyuser . '@' . $proxy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$logger = $this->logger;
|
|
|
|
|
|
|
|
$onRedirect = function (
|
|
|
|
RequestInterface $request,
|
|
|
|
ResponseInterface $response,
|
|
|
|
UriInterface $uri
|
|
|
|
) use ($logger) {
|
2021-08-23 12:02:52 +00:00
|
|
|
$logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri, 'method' => $request->getMethod()]);
|
2021-08-22 22:14:18 +00:00
|
|
|
};
|
|
|
|
|
2021-08-23 11:44:46 +00:00
|
|
|
$userAgent = FRIENDICA_PLATFORM . " '" .
|
|
|
|
FRIENDICA_CODENAME . "' " .
|
|
|
|
FRIENDICA_VERSION . '-' .
|
|
|
|
DB_UPDATE_VERSION . '; ' .
|
|
|
|
$this->baseUrl->get();
|
|
|
|
|
2021-08-22 22:14:18 +00:00
|
|
|
$guzzle = new Client([
|
|
|
|
RequestOptions::ALLOW_REDIRECTS => [
|
|
|
|
'max' => 8,
|
|
|
|
'on_redirect' => $onRedirect,
|
|
|
|
'track_redirect' => true,
|
|
|
|
'strict' => true,
|
|
|
|
'referer' => true,
|
|
|
|
],
|
|
|
|
RequestOptions::HTTP_ERRORS => false,
|
|
|
|
// Without this setting it seems as if some webservers send compressed content
|
|
|
|
// This seems to confuse curl so that it shows this uncompressed.
|
|
|
|
/// @todo We could possibly set this value to "gzip" or something similar
|
|
|
|
RequestOptions::DECODE_CONTENT => '',
|
|
|
|
RequestOptions::FORCE_IP_RESOLVE => ($this->config->get('system', 'ipv4_resolve') ? 'v4' : null),
|
|
|
|
RequestOptions::CONNECT_TIMEOUT => 10,
|
|
|
|
RequestOptions::TIMEOUT => $this->config->get('system', 'curl_timeout', 60),
|
|
|
|
// by default we will allow self-signed certs
|
2021-08-25 11:59:37 +00:00
|
|
|
// but it can be overridden
|
2021-08-23 11:44:46 +00:00
|
|
|
RequestOptions::VERIFY => (bool)$this->config->get('system', 'verifyssl'),
|
|
|
|
RequestOptions::PROXY => $proxy,
|
|
|
|
RequestOptions::HEADERS => [
|
|
|
|
'User-Agent' => $userAgent,
|
|
|
|
],
|
2021-08-24 22:13:50 +00:00
|
|
|
'handler' => $handlerStack ?? HandlerStack::create(),
|
2021-08-22 22:14:18 +00:00
|
|
|
]);
|
|
|
|
|
2021-08-23 12:28:25 +00:00
|
|
|
$resolver = new URLResolver();
|
|
|
|
$resolver->setUserAgent($userAgent);
|
|
|
|
$resolver->setMaxRedirects(10);
|
|
|
|
$resolver->setRequestTimeout(10);
|
|
|
|
// if the file is too large then exit
|
|
|
|
$resolver->setMaxResponseDataSize(1000000);
|
2021-08-25 16:01:07 +00:00
|
|
|
// Designate a temporary file that will store cookies during the session.
|
|
|
|
// Some websites test the browser for cookie support, so this enhances results.
|
|
|
|
$resolver->setCookieJar(get_temppath() . '/url_resolver.cookie', true);
|
2021-08-23 12:28:25 +00:00
|
|
|
|
|
|
|
return new HTTPClient($logger, $this->profiler, $guzzle, $resolver);
|
2021-08-22 22:14:18 +00:00
|
|
|
}
|
|
|
|
}
|