-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver_request_headers.php
More file actions
53 lines (43 loc) · 1.53 KB
/
server_request_headers.php
File metadata and controls
53 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php declare(strict_types=1);
/**
* It's free open-source software released under the MIT License.
*
* @author Anatoly Nekhay <[email protected]>
* @copyright Copyright (c) 2018, Anatoly Nekhay
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
* @link https://github.com/sunrise-php/http-message
*/
namespace Sunrise\Http\Message;
use function strncmp;
use function strtolower;
use function strtr;
use function substr;
use function ucwords;
/**
* @return array<string, string>
*
* @link http://php.net/manual/en/reserved.variables.server.php
* @link https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.18
*/
function server_request_headers(?array $serverParams = null): array
{
$serverParams ??= $_SERVER;
// https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.2
if (!isset($serverParams['HTTP_CONTENT_LENGTH']) && isset($serverParams['CONTENT_LENGTH'])) {
$serverParams['HTTP_CONTENT_LENGTH'] = $serverParams['CONTENT_LENGTH'];
}
// https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.3
if (!isset($serverParams['HTTP_CONTENT_TYPE']) && isset($serverParams['CONTENT_TYPE'])) {
$serverParams['HTTP_CONTENT_TYPE'] = $serverParams['CONTENT_TYPE'];
}
$result = [];
foreach ($serverParams as $key => $value) {
if (strncmp('HTTP_', $key, 5) !== 0) {
continue;
}
$name = strtr(substr($key, 5), '_', '-');
$name = ucwords(strtolower($name), '-');
$result[$name] = $value;
}
return $result;
}