Bicycle/System/Classes/HTTP.php
Egor Isaev b65603b4d0 dev
2026-06-23 10:44:18 +03:00

63 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description HTTP.php
* @copyright (c) 04/06/2026
*/
namespace System\Classes;
use System\Classes\HTTP\Header;
/**
* HTTP-хелперы: редирект и сбор входящих заголовков.
*/
class HTTP
{
/**
* Выполняет редирект и завершает выполнение.
*
* @param string $uri Целевой URL
* @param int $code HTTP-код редиректа
* @return never
*/
public static function redirect(string $uri, int $code = 302): never
{
http_response_code($code);
header('Location: ' . $uri);
exit;
}
/**
* Возвращает заголовки входящего запроса (через apache_request_headers
* или реконструкцию из $_SERVER).
*
* @return Header
*/
public static function requestHeaders(): Header
{
if (function_exists('apache_request_headers')) {
return new Header(array_change_key_case(apache_request_headers()));
}
$headers = [];
if (!empty($_SERVER['CONTENT_TYPE'])) {
$headers['content-type'] = $_SERVER['CONTENT_TYPE'];
}
if (!empty($_SERVER['CONTENT_LENGTH'])) {
$headers['content-length'] = $_SERVER['CONTENT_LENGTH'];
}
foreach ($_SERVER as $key => $value) {
if (str_starts_with($key, 'HTTP_')) {
$headers[str_replace('_', '-', strtolower(substr($key, 5)))] = $value;
}
}
return new Header($headers);
}
}