f0629d8deeac4b7e10ac2fa123a1c2fbb713e3a7
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 /**
4 * @see https://github.com/laminas/laminas-diactoros for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md
6 * @license https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License
7 */
8
9 declare(strict_types=1);
10
11 namespace Laminas\Diactoros;
12
13 use function array_key_exists;
14 use function is_string;
15 use function strpos;
16 use function strtolower;
17 use function strtr;
18 use function substr;
19
20 /**
21 * @param array $server Values obtained from the SAPI (generally `$_SERVER`).
22 * @return array Header/value pairs
23 */
24 function marshalHeadersFromSapi(array $server) : array
25 {
26 $headers = [];
27 foreach ($server as $key => $value) {
28 if (! is_string($key)) {
29 continue;
30 }
31
32 if ($value === '') {
33 continue;
34 }
35
36 // Apache prefixes environment variables with REDIRECT_
37 // if they are added by rewrite rules
38 if (strpos($key, 'REDIRECT_') === 0) {
39 $key = substr($key, 9);
40
41 // We will not overwrite existing variables with the
42 // prefixed versions, though
43 if (array_key_exists($key, $server)) {
44 continue;
45 }
46 }
47
48 if (strpos($key, 'HTTP_') === 0) {
49 $name = strtr(strtolower(substr($key, 5)), '_', '-');
50 $headers[$name] = $value;
51 continue;
52 }
53
54 if (strpos($key, 'CONTENT_') === 0) {
55 $name = strtr(strtolower($key), '_', '-');
56 $headers[$name] = $value;
57 continue;
58 }
59 }
60
61 return $headers;
62 }