Centralize HTTP limits

This commit is contained in:
Troy Grunt
2026-02-15 14:57:05 +01:00
parent 923dafecc9
commit cb115bac40
5 changed files with 75 additions and 18 deletions

View File

@@ -1,13 +1,16 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/http-limits.php';
function httpContext(int $timeout = 8) {
function httpContext(?int $timeout = null) {
$limits = httpLimits();
$resolvedTimeout = $timeout === null ? $limits['timeout'] : max(1, $timeout);
return stream_context_create([
'http' => [
'timeout' => $timeout,
'timeout' => $resolvedTimeout,
'follow_location' => 1,
'max_redirects' => 4,
'user_agent' => 'star-citizen.de-linkbot/1.0',
'max_redirects' => $limits['max_redirects'],
'user_agent' => $limits['user_agent'],
'ignore_errors' => true
],
'ssl' => [
@@ -66,10 +69,14 @@ function resolveUrl(string $url, string $baseUrl): ?string {
return $baseParts['scheme'] . '://' . $baseParts['host'] . $path . $url;
}
function safeFetch(string $url, int $timeout = 8): ?string {
function safeFetch(string $url, ?int $timeout = null): ?string {
$limits = httpLimits();
$ctx = httpContext($timeout);
$content = @file_get_contents($url, false, $ctx);
return $content === false ? null : $content;
if ($content === false || strlen($content) > $limits['max_bytes']) {
return null;
}
return $content;
}
function downloadImageFromUrl(string $url, string $baseUrl, string $destinationFolder = 'upl/'): ?string {
@@ -82,8 +89,8 @@ function downloadImageFromUrl(string $url, string $baseUrl, string $destinationF
return null;
}
$imageContent = safeFetch($resolved, 10);
if ($imageContent === null || strlen($imageContent) === 0 || strlen($imageContent) > (5 * 1024 * 1024)) {
$imageContent = safeFetch($resolved);
if ($imageContent === null || strlen($imageContent) === 0) {
return null;
}
@@ -130,7 +137,7 @@ function getPageInfo(string $url): array {
return $ret;
}
$html = safeFetch($normalized, 10);
$html = safeFetch($normalized);
if ($html === null) {
$ret['error'] = 'seite_nicht_erreichbar';
return $ret;