Refactor HTTPClient::get() / ::head()

This commit is contained in:
Philipp Holzer 2021-08-23 13:44:46 +02:00
parent 52c7948526
commit 736277dcf0
No known key found for this signature in database
GPG key ID: 9A28B7D4FF5667BD
2 changed files with 28 additions and 23 deletions

View file

@ -54,6 +54,12 @@ class HTTPClientFactory extends BaseFactory
$logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri]); $logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri]);
}; };
$userAgent = FRIENDICA_PLATFORM . " '" .
FRIENDICA_CODENAME . "' " .
FRIENDICA_VERSION . '-' .
DB_UPDATE_VERSION . '; ' .
$this->baseUrl->get();
$guzzle = new Client([ $guzzle = new Client([
RequestOptions::ALLOW_REDIRECTS => [ RequestOptions::ALLOW_REDIRECTS => [
'max' => 8, 'max' => 8,
@ -72,16 +78,13 @@ class HTTPClientFactory extends BaseFactory
RequestOptions::TIMEOUT => $this->config->get('system', 'curl_timeout', 60), RequestOptions::TIMEOUT => $this->config->get('system', 'curl_timeout', 60),
// by default we will allow self-signed certs // by default we will allow self-signed certs
// but you can override this // but you can override this
RequestOptions::VERIFY => (bool)$this->config->get('system', 'verifyssl'), RequestOptions::VERIFY => (bool)$this->config->get('system', 'verifyssl'),
RequestOptions::PROXY => $proxy, RequestOptions::PROXY => $proxy,
RequestOptions::HEADERS => [
'User-Agent' => $userAgent,
],
]); ]);
$userAgent = FRIENDICA_PLATFORM . " '" .
FRIENDICA_CODENAME . "' " .
FRIENDICA_VERSION . '-' .
DB_UPDATE_VERSION . '; ' .
$this->baseUrl->get();
return new HTTPClient($logger, $this->profiler, $this->config, $userAgent, $guzzle); return new HTTPClient($logger, $this->profiler, $this->config, $userAgent, $guzzle);
} }
} }

View file

@ -60,7 +60,10 @@ class HTTPClient implements IHTTPClient
$this->client = $client; $this->client = $client;
} }
protected function request(string $method, string $url, array $opts = []) /**
* @throws HTTPException\InternalServerErrorException
*/
protected function request(string $method, string $url, array $opts = []): IHTTPResult
{ {
$this->profiler->startRecording('network'); $this->profiler->startRecording('network');
@ -96,30 +99,32 @@ class HTTPClient implements IHTTPClient
$conf = []; $conf = [];
if (!empty($opts['cookiejar'])) { if (!empty($opts['cookiejar'])) {
$jar = new FileCookieJar($opts['cookiejar']); $jar = new FileCookieJar($opts['cookiejar']);
$conf[RequestOptions::COOKIES] = $jar; $conf[RequestOptions::COOKIES] = $jar;
} }
$header = [];
if (!empty($opts['accept_content'])) { if (!empty($opts['accept_content'])) {
array_push($curlOptions[CURLOPT_HTTPHEADER], 'Accept: ' . $opts['accept_content']); array_push($header, 'Accept: ' . $opts['accept_content']);
} }
if (!empty($opts['header'])) { if (!empty($opts['header'])) {
$curlOptions[CURLOPT_HTTPHEADER] = array_merge($opts['header'], $curlOptions[CURLOPT_HTTPHEADER]); $header = array_merge($opts['header'], $header);
} }
$curlOptions[CURLOPT_USERAGENT] = $this->userAgent;
if (!empty($opts['headers'])) { if (!empty($opts['headers'])) {
$this->logger->notice('Wrong option \'headers\' used.'); $this->logger->notice('Wrong option \'headers\' used.');
$curlOptions[CURLOPT_HTTPHEADER] = array_merge($opts['headers'], $curlOptions[CURLOPT_HTTPHEADER]); $header = array_merge($opts['headers'], $header);
} }
$conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $header);
if (!empty($opts['timeout'])) { if (!empty($opts['timeout'])) {
$curlOptions[CURLOPT_TIMEOUT] = $opts['timeout']; $conf[RequestOptions::TIMEOUT] = $opts['timeout'];
} }
$onHeaders = function (ResponseInterface $response) use ($opts) { $conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
if (!empty($opts['content_length']) && if (!empty($opts['content_length']) &&
$response->getHeaderLine('Content-Length') > $opts['content_length']) { $response->getHeaderLine('Content-Length') > $opts['content_length']) {
throw new TransferException('The file is too big!'); throw new TransferException('The file is too big!');
@ -127,10 +132,7 @@ class HTTPClient implements IHTTPClient
}; };
try { try {
$response = $this->client->$method($url, [ $response = $this->client->$method($url, $conf);
'on_headers' => $onHeaders,
'curl' => $curlOptions,
]);
return new GuzzleResponse($response, $url); return new GuzzleResponse($response, $url);
} catch (TransferException $exception) { } catch (TransferException $exception) {
if ($exception instanceof RequestException && if ($exception instanceof RequestException &&
@ -148,7 +150,7 @@ class HTTPClient implements IHTTPClient
* *
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
public function head(string $url, array $opts = []) public function head(string $url, array $opts = []): IHTTPResult
{ {
return $this->request('head', $url, $opts); return $this->request('head', $url, $opts);
} }
@ -156,7 +158,7 @@ class HTTPClient implements IHTTPClient
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function get(string $url, array $opts = []) public function get(string $url, array $opts = []): IHTTPResult
{ {
return $this->request('get', $url, $opts); return $this->request('get', $url, $opts);
} }