fix: emit full raw http responses

This commit is contained in:
Marek Miklewicz
2026-06-30 19:53:28 +02:00
parent d5894a1935
commit e2814fd81c
4 changed files with 88 additions and 23 deletions
+74 -21
View File
@@ -37,23 +37,26 @@ final class Http
public static function redirectNoStore(string $url): void public static function redirectNoStore(string $url): void
{ {
header('Cache-Control: no-store, private'); self::sendResponse(302, [
header('Pragma: no-cache'); 'Cache-Control' => 'no-store, private',
header('Referrer-Policy: no-referrer'); 'Pragma' => 'no-cache',
header('X-Content-Type-Options: nosniff'); 'Referrer-Policy' => 'no-referrer',
header('Location: ' . $url, true, 302); 'X-Content-Type-Options' => 'nosniff',
'Location' => $url,
], '');
exit; exit;
} }
public static function browserRedirectNoStore(string $url): void public static function browserRedirectNoStore(string $url): void
{ {
header('Content-Type: text/html; charset=UTF-8'); self::sendResponse(200, [
header('Cache-Control: no-store, private'); 'Content-Type' => 'text/html; charset=UTF-8',
header('Pragma: no-cache'); 'Cache-Control' => 'no-store, private',
header('Referrer-Policy: no-referrer'); 'Pragma' => 'no-cache',
header('X-Content-Type-Options: nosniff'); 'Referrer-Policy' => 'no-referrer',
header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'"); 'X-Content-Type-Options' => 'nosniff',
echo self::redirectPageHtml($url); 'Content-Security-Policy' => "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'",
], self::redirectPageHtml($url));
exit; exit;
} }
@@ -78,16 +81,66 @@ final class Http
public static function errorPage(string $message, int $status): void public static function errorPage(string $message, int $status): void
{ {
http_response_code($status);
header('Content-Type: text/html; charset=UTF-8');
header('Cache-Control: no-store, private');
header('Referrer-Policy: no-referrer');
$safe = htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); $safe = htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
echo '<!doctype html><html lang="pl"><head><meta charset="utf-8">'; $body = '<!doctype html><html lang="pl"><head><meta charset="utf-8">'
echo '<meta name="viewport" content="width=device-width, initial-scale=1">'; . '<meta name="viewport" content="width=device-width, initial-scale=1">'
echo '<title>Serwer poczty</title></head><body>'; . '<title>Serwer poczty</title></head><body>'
echo '<h1>Serwer poczty</h1><p>' . $safe . '</p>'; . '<h1>Serwer poczty</h1><p>' . $safe . '</p>'
echo '</body></html>'; . '</body></html>';
self::sendResponse($status, [
'Content-Type' => 'text/html; charset=UTF-8',
'Cache-Control' => 'no-store, private',
'Referrer-Policy' => 'no-referrer',
], $body);
exit; exit;
} }
/**
* @param array<string,string> $headers
*/
public static function rawHttpResponse(int $status, array $headers, string $body): string
{
$headers['Content-Length'] = (string)strlen($body);
$lines = ['HTTP/1.1 ' . $status . ' ' . self::reasonPhrase($status)];
foreach ($headers as $name => $value) {
$safeName = preg_replace('/[^A-Za-z0-9-]/', '', (string)$name) ?? '';
if ($safeName === '') {
continue;
}
$safeValue = str_replace(["\r", "\n"], '', (string)$value);
$lines[] = $safeName . ': ' . $safeValue;
}
return implode("\r\n", $lines) . "\r\n\r\n" . $body;
}
/**
* @param array<string,string> $headers
*/
private static function sendResponse(int $status, array $headers, string $body): void
{
if (PHP_SAPI === 'cli') {
echo self::rawHttpResponse($status, $headers, $body);
return;
}
http_response_code($status);
foreach ($headers as $name => $value) {
header($name . ': ' . $value);
}
header('Content-Length: ' . strlen($body));
echo $body;
}
private static function reasonPhrase(int $status): string
{
return match ($status) {
200 => 'OK',
302 => 'Found',
403 => 'Forbidden',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
502 => 'Bad Gateway',
default => 'OK',
};
}
} }
+1 -1
View File
@@ -2,7 +2,7 @@ name=mail-login
id=mail-login id=mail-login
type=user type=user
author=HITME.PL author=HITME.PL
version=1.0.10 version=1.0.11
active=no active=no
installed=no installed=no
user_run_as=root user_run_as=root
+12
View File
@@ -12,3 +12,15 @@ test('http redirect page performs top level browser navigation', function (): vo
assert_contains('https://mx1.domena.pl:2222/api/login/url?key=SECRET&amp;x=1', $html); assert_contains('https://mx1.domena.pl:2222/api/login/url?key=SECRET&amp;x=1', $html);
assert_contains('https://mx1.domena.pl:2222/api/login/url?key=SECRET\\u0026x=1', $html); assert_contains('https://mx1.domena.pl:2222/api/login/url?key=SECRET\\u0026x=1', $html);
}); });
test('http raw response is a complete http response for directadmin proxy', function (): void {
$response = Http::rawHttpResponse(200, [
'Content-Type' => 'text/html; charset=UTF-8',
'Cache-Control' => 'no-store, private',
], '<!doctype html><html></html>');
assert_contains("HTTP/1.1 200 OK\r\n", $response);
assert_contains("Content-Type: text/html; charset=UTF-8\r\n", $response);
assert_contains("Content-Length: 28\r\n", $response);
assert_contains("\r\n\r\n<!doctype html><html></html>", $response);
});
+1 -1
View File
@@ -8,7 +8,7 @@ test('plugin metadata and packaging docs match project rules', function (): void
assert_contains('name=mail-login', $conf); assert_contains('name=mail-login', $conf);
assert_contains('id=mail-login', $conf); assert_contains('id=mail-login', $conf);
assert_contains('type=user', $conf); assert_contains('type=user', $conf);
assert_contains('version=1.0.10', $conf); assert_contains('version=1.0.11', $conf);
assert_contains('user_run_as=root', $conf); assert_contains('user_run_as=root', $conf);
assert_contains('/Users/marek/GPT/ChatGPT/da_plugins/mail-login/src', $packing); assert_contains('/Users/marek/GPT/ChatGPT/da_plugins/mail-login/src', $packing);
assert_contains('/Users/marek/GPT/ChatGPT/da_plugins/mail-login/archives/X.Y.Z/mail-login.tar.gz', $packing); assert_contains('/Users/marek/GPT/ChatGPT/da_plugins/mail-login/archives/X.Y.Z/mail-login.tar.gz', $packing);