|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Import functions |
|
16
|
|
|
*/ |
|
17
|
|
|
use function strncmp; |
|
18
|
|
|
use function strtolower; |
|
19
|
|
|
use function strtr; |
|
20
|
|
|
use function substr; |
|
21
|
|
|
use function ucwords; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Gets the request headers |
|
25
|
|
|
* |
|
26
|
|
|
* @param array|null $serverParams |
|
27
|
|
|
* |
|
28
|
|
|
* @return array<string, string> |
|
29
|
|
|
* |
|
30
|
|
|
* @link http://php.net/manual/en/reserved.variables.server.php |
|
31
|
|
|
* @link https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.18 |
|
32
|
|
|
*/ |
|
33
|
|
|
function server_request_headers(?array $serverParams = null): array |
|
34
|
|
|
{ |
|
35
|
58 |
|
$serverParams ??= $_SERVER; |
|
36
|
|
|
|
|
37
|
|
|
// https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.2 |
|
38
|
58 |
|
if (!isset($serverParams['HTTP_CONTENT_LENGTH']) && isset($serverParams['CONTENT_LENGTH'])) { |
|
39
|
2 |
|
$serverParams['HTTP_CONTENT_LENGTH'] = $serverParams['CONTENT_LENGTH']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.3 |
|
43
|
58 |
|
if (!isset($serverParams['HTTP_CONTENT_TYPE']) && isset($serverParams['CONTENT_TYPE'])) { |
|
44
|
2 |
|
$serverParams['HTTP_CONTENT_TYPE'] = $serverParams['CONTENT_TYPE']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
58 |
|
$result = []; |
|
48
|
58 |
|
foreach ($serverParams as $key => $value) { |
|
49
|
50 |
|
if (0 <> strncmp('HTTP_', $key, 5)) { |
|
50
|
44 |
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
10 |
|
$name = strtr(substr($key, 5), '_', '-'); |
|
54
|
10 |
|
$name = ucwords(strtolower($name), '-'); |
|
55
|
|
|
|
|
56
|
10 |
|
$result[$name] = $value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
58 |
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
|