mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-04-26 01:07:40 -04:00
Include port number for HTTP Retry-After (#7875)
fix https://github.com/FreshRSS/FreshRSS/issues/7870 (partially)
This commit is contained in:
committed by
GitHub
parent
379a387dde
commit
2b02ca59c6
@@ -19,6 +19,9 @@ final class FreshRSS_SimplePieResponse extends \SimplePie\File
|
||||
if ($retryAfter > 0) {
|
||||
$domain = parse_url($this->get_final_requested_uri(), PHP_URL_HOST);
|
||||
if (is_string($domain) && $domain !== '') {
|
||||
if (is_int($port = parse_url($this->get_final_requested_uri(), PHP_URL_PORT))) {
|
||||
$domain .= ':' . $port;
|
||||
}
|
||||
$errorMessage = 'Will retry after ' . date('c', $retryAfter) . ' for domain `' . $domain . '`';
|
||||
Minz_Log::notice($errorMessage);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,18 @@ final class FreshRSS_http_Util {
|
||||
|
||||
private const RETRY_AFTER_PATH = DATA_PATH . '/Retry-After/';
|
||||
|
||||
private static function getRetryAfterFile(string $url): string {
|
||||
$domain = parse_url($url, PHP_URL_HOST);
|
||||
if (!is_string($domain) || $domain === '') {
|
||||
return '';
|
||||
}
|
||||
$port = parse_url($url, PHP_URL_PORT);
|
||||
if (is_int($port)) {
|
||||
$domain .= ':' . $port;
|
||||
}
|
||||
return self::RETRY_AFTER_PATH . urlencode($domain) . '.txt';
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up old Retry-After files
|
||||
*/
|
||||
@@ -31,16 +43,16 @@ final class FreshRSS_http_Util {
|
||||
if (rand(0, 30) === 1) { // Remove old files once in a while
|
||||
self::cleanRetryAfters();
|
||||
}
|
||||
$domain = parse_url($url, PHP_URL_HOST);
|
||||
if (!is_string($domain) || $domain === '') {
|
||||
$txt = self::getRetryAfterFile($url);
|
||||
if ($txt === '') {
|
||||
return 0;
|
||||
}
|
||||
$retryAfter = @filemtime(self::RETRY_AFTER_PATH . $domain . '.txt') ?: 0;
|
||||
$retryAfter = @filemtime($txt) ?: 0;
|
||||
if ($retryAfter <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if ($retryAfter < time()) {
|
||||
@unlink(self::RETRY_AFTER_PATH . $domain . '.txt');
|
||||
@unlink($txt);
|
||||
return 0;
|
||||
}
|
||||
return $retryAfter;
|
||||
@@ -50,8 +62,8 @@ final class FreshRSS_http_Util {
|
||||
* Store the HTTP Retry-After header value of an HTTP `429 Too Many Requests` or `503 Service Unavailable` response.
|
||||
*/
|
||||
public static function setRetryAfter(string $url, string $retryAfter): int {
|
||||
$domain = parse_url($url, PHP_URL_HOST);
|
||||
if (!is_string($domain) || $domain === '') {
|
||||
$txt = self::getRetryAfterFile($url);
|
||||
if ($txt === '') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -65,8 +77,8 @@ final class FreshRSS_http_Util {
|
||||
$retryAfter = min($retryAfter, time() + max(3600, $limits['retry_after_max'] ?? 0));
|
||||
|
||||
@mkdir(self::RETRY_AFTER_PATH);
|
||||
if (!touch(self::RETRY_AFTER_PATH . $domain . '.txt', $retryAfter)) {
|
||||
Minz_Log::error('Failed to set Retry-After for ' . $domain);
|
||||
if (!touch($txt, $retryAfter)) {
|
||||
Minz_Log::error('Failed to set Retry-After for ' . $url);
|
||||
return 0;
|
||||
}
|
||||
return $retryAfter;
|
||||
|
||||
Reference in New Issue
Block a user