mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-04-12 01:48:48 +00:00
refactor: extract CurlHttpClient (#3532)
* refactor: extract CurlHttpClient * refactor * interface
This commit is contained in:
parent
7b46b97abd
commit
a59793e8d6
@ -92,7 +92,12 @@ class AO3Bridge extends BridgeAbstract
|
||||
private function collectWork($id)
|
||||
{
|
||||
$url = self::URI . "/works/$id/navigate";
|
||||
$response = _http_request($url, ['useragent' => 'rss-bridge bot (https://github.com/RSS-Bridge/rss-bridge)']);
|
||||
$httpClient = RssBridge::getHttpClient();
|
||||
|
||||
$response = $httpClient->request($url, [
|
||||
'useragent' => 'rss-bridge bot (https://github.com/RSS-Bridge/rss-bridge)',
|
||||
]);
|
||||
|
||||
$html = \str_get_html($response['body']);
|
||||
$html = defaultLinkTo($html, self::URI);
|
||||
|
||||
|
@ -14,7 +14,8 @@ while ($next) { /* Collect all contributors */
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => 'RSS-Bridge',
|
||||
];
|
||||
$result = _http_request($url, ['headers' => $headers]);
|
||||
$httpClient = new CurlHttpClient();
|
||||
$result = $httpClient->request($url, ['headers' => $headers]);
|
||||
|
||||
foreach (json_decode($result['body']) as $contributor) {
|
||||
$contributors[] = $contributor;
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
final class RssBridge
|
||||
{
|
||||
private static HttpClient $httpClient;
|
||||
private static CacheInterface $cache;
|
||||
|
||||
public function main(array $argv = [])
|
||||
@ -71,9 +72,10 @@ final class RssBridge
|
||||
// Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
|
||||
date_default_timezone_set(Configuration::getConfig('system', 'timezone'));
|
||||
|
||||
// Create cache
|
||||
$cacheFactory = new CacheFactory();
|
||||
self::setCache($cacheFactory->create());
|
||||
|
||||
self::$httpClient = new CurlHttpClient();
|
||||
self::$cache = $cacheFactory->create();
|
||||
|
||||
if (Configuration::getConfig('authentication', 'enable')) {
|
||||
$authenticationMiddleware = new AuthenticationMiddleware();
|
||||
@ -105,13 +107,13 @@ final class RssBridge
|
||||
}
|
||||
}
|
||||
|
||||
public static function getHttpClient(): HttpClient
|
||||
{
|
||||
return self::$httpClient;
|
||||
}
|
||||
|
||||
public static function getCache(): CacheInterface
|
||||
{
|
||||
return self::$cache;
|
||||
}
|
||||
|
||||
public static function setCache(CacheInterface $cache): void
|
||||
{
|
||||
self::$cache = $cache;
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +99,7 @@ function getContents(
|
||||
array $curlOptions = [],
|
||||
bool $returnFull = false
|
||||
) {
|
||||
$httpClient = RssBridge::getHttpClient();
|
||||
$cache = RssBridge::getCache();
|
||||
$cache->setScope('server');
|
||||
$cache->setKey([$url]);
|
||||
@ -141,20 +142,14 @@ function getContents(
|
||||
$config['if_not_modified_since'] = $cache->getTime();
|
||||
}
|
||||
|
||||
$result = _http_request($url, $config);
|
||||
$response = [
|
||||
'code' => $result['code'],
|
||||
'status_lines' => $result['status_lines'],
|
||||
'header' => $result['headers'],
|
||||
'content' => $result['body'],
|
||||
];
|
||||
$response = $httpClient->request($url, $config);
|
||||
|
||||
switch ($result['code']) {
|
||||
switch ($response['code']) {
|
||||
case 200:
|
||||
case 201:
|
||||
case 202:
|
||||
if (isset($result['headers']['cache-control'])) {
|
||||
$cachecontrol = $result['headers']['cache-control'];
|
||||
if (isset($response['headers']['cache-control'])) {
|
||||
$cachecontrol = $response['headers']['cache-control'];
|
||||
$lastValue = array_pop($cachecontrol);
|
||||
$directives = explode(',', $lastValue);
|
||||
$directives = array_map('trim', $directives);
|
||||
@ -163,7 +158,7 @@ function getContents(
|
||||
break;
|
||||
}
|
||||
}
|
||||
$cache->saveData($result['body']);
|
||||
$cache->saveData($response['body']);
|
||||
break;
|
||||
case 301:
|
||||
case 302:
|
||||
@ -172,16 +167,16 @@ function getContents(
|
||||
break;
|
||||
case 304:
|
||||
// Not Modified
|
||||
$response['content'] = $cache->loadData();
|
||||
$response['body'] = $cache->loadData();
|
||||
break;
|
||||
default:
|
||||
$exceptionMessage = sprintf(
|
||||
'%s resulted in %s %s %s',
|
||||
$url,
|
||||
$result['code'],
|
||||
Response::STATUS_CODES[$result['code']] ?? '',
|
||||
$response['code'],
|
||||
Response::STATUS_CODES[$response['code']] ?? '',
|
||||
// If debug, include a part of the response body in the exception message
|
||||
Debug::isEnabled() ? mb_substr($result['body'], 0, 500) : '',
|
||||
Debug::isEnabled() ? mb_substr($response['body'], 0, 500) : '',
|
||||
);
|
||||
|
||||
// The following code must be extracted if it grows too much
|
||||
@ -192,26 +187,29 @@ function getContents(
|
||||
'<title>Security | Glassdoor',
|
||||
];
|
||||
foreach ($cloudflareTitles as $cloudflareTitle) {
|
||||
if (str_contains($result['body'], $cloudflareTitle)) {
|
||||
throw new CloudFlareException($exceptionMessage, $result['code']);
|
||||
if (str_contains($response['body'], $cloudflareTitle)) {
|
||||
throw new CloudFlareException($exceptionMessage, $response['code']);
|
||||
}
|
||||
}
|
||||
|
||||
throw new HttpException(trim($exceptionMessage), $result['code']);
|
||||
}
|
||||
if ($returnFull === true) {
|
||||
// For legacy reasons, use content instead of body
|
||||
$response['content'] = $response['body'];
|
||||
unset($response['body']);
|
||||
return $response;
|
||||
}
|
||||
return $response['content'];
|
||||
return $response['body'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch content from url
|
||||
*
|
||||
* @internal Private function used internally
|
||||
* @throws HttpException
|
||||
*/
|
||||
function _http_request(string $url, array $config = []): array
|
||||
interface HttpClient
|
||||
{
|
||||
public function request(string $url, array $config = []): array;
|
||||
}
|
||||
|
||||
final class CurlHttpClient implements HttpClient
|
||||
{
|
||||
public function request(string $url, array $config = []): array
|
||||
{
|
||||
$defaults = [
|
||||
'useragent' => null,
|
||||
@ -324,6 +322,7 @@ function _http_request(string $url, array $config = []): array
|
||||
'body' => $data,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets contents from the Internet as simplhtmldom object.
|
||||
|
Loading…
Reference in New Issue
Block a user