Change 404 error page to 503 and enhance HTTP request handling with header support

This commit is contained in:
Fabian Schieder 2026-03-01 14:18:16 +01:00
parent 33b1efb903
commit 37c2ef88e9
2 changed files with 22 additions and 16 deletions

View File

@ -1,5 +1,5 @@
<?php
http_response_code(404);
http_response_code(503);
?>
<!DOCTYPE html>
<html lang="de">
@ -138,15 +138,14 @@ http_response_code(404);
<div class="error-wrap">
<div class="error-icon">🔍</div>
<div class="error-code">404</div>
<div class="error-code">503</div>
<hr class="error-divider">
<h1 class="error-title">Seite nicht gefunden</h1>
<h1 class="error-title">Dienst nicht verfügbar</h1>
<p class="error-desc">
Die Seite, die du suchst, existiert nicht (mehr)<br>
oder wurde vielleicht verschoben.
Die Seite die du öffnen willst ist gerade nicht verfügbar<br>
</p>
<div class="error-actions">

View File

@ -124,7 +124,7 @@ function is_reachable_http_code(?int $code): bool
/**
* Perform a lightweight HTTP request (HEAD/GET) without following redirects.
* @return array{ok:bool, code:int|null, ms:int|null, error:string|null}
* @return array{ok:bool, code:int|null, ms:int|null, error:string|null, headers?:array<int,string>}
*/
function check_http_request(string $url, string $method, float $timeoutSeconds = 1.6, ?string $extraHeaders = null): array
{
@ -190,13 +190,18 @@ function check_http_request(string $url, string $method, float $timeoutSeconds =
$error = 'Unbekannter Fehler';
}
return ['ok' => $ok, 'code' => $code, 'ms' => $ms, 'error' => $error];
$res = ['ok' => $ok, 'code' => $code, 'ms' => $ms, 'error' => $error];
if (isset($http_response_header) && is_array($http_response_header)) {
$res['headers'] = $http_response_header;
}
return $res;
}
/**
* HTTP check (no redirects).
* - Tries HEAD first.
* - If result is a redirect, probes the Location once (still without following further redirects).
* - If result is a redirect, probes the Location once, because we don't follow redirects automatically.
* - If HEAD fails, tries a minimal GET with Range.
* @return array{ok:bool, code:int|null, ms:int|null, error:string|null}
*/
@ -207,8 +212,8 @@ function check_http_head(string $url, float $timeoutSeconds = 1.6): array
// If we got a redirect, probe the Location target once, because we don't follow redirects automatically.
if ($head['code'] !== null && $head['code'] >= 300 && $head['code'] < 400) {
$loc = null;
if (isset($http_response_header) && is_array($http_response_header)) {
foreach ($http_response_header as $h) {
if (!empty($head['headers']) && is_array($head['headers'])) {
foreach ($head['headers'] as $h) {
if (stripos((string)$h, 'Location:') === 0) {
$loc = trim(substr((string)$h, strlen('Location:')));
break;
@ -232,12 +237,14 @@ function check_http_head(string $url, float $timeoutSeconds = 1.6): array
$probe = check_http_request($loc, 'GET', $timeoutSeconds, "Range: bytes=0-0\r\n");
}
// If the target is clearly reachable/unreachable, use that.
if ($probe['code'] !== null && ($probe['code'] < 300 || $probe['code'] >= 400)) {
return $probe;
// return the probe result (drop headers)
if (!$probe['ok'] && empty($probe['error'])) {
$probe['error'] = 'HTTP ' . (string)$probe['code'];
}
return ['ok' => (bool)$probe['ok'], 'code' => $probe['code'], 'ms' => $probe['ms'], 'error' => $probe['error']];
}
// Otherwise keep the redirect info, but don't mark it UP.
return ['ok' => false, 'code' => $head['code'], 'ms' => $head['ms'], 'error' => 'Redirect'];
}
@ -245,7 +252,7 @@ function check_http_head(string $url, float $timeoutSeconds = 1.6): array
}
if ($head['ok']) {
return $head;
return ['ok' => true, 'code' => $head['code'], 'ms' => $head['ms'], 'error' => null];
}
// If HEAD is not supported/misconfigured, a GET with Range is a cheap reachability check.
@ -253,11 +260,11 @@ function check_http_head(string $url, float $timeoutSeconds = 1.6): array
// prefer the GET result if it produced a code (more reliable)
if ($get['code'] !== null) {
return $get;
return ['ok' => $get['ok'], 'code' => $get['code'], 'ms' => $get['ms'], 'error' => $get['error']];
}
// otherwise fall back to the HEAD info
return $head;
return ['ok' => false, 'code' => $head['code'], 'ms' => $head['ms'], 'error' => $head['error']];
}
function build_server_status(array $targets): array