1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter\Normalizer; |
13
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Asset\AbstractUninstantiableAsset; |
15
|
|
|
use Ivory\HttpAdapter\Parser\HeadersParser; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author GeLo <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class HeadersNormalizer extends AbstractUninstantiableAsset |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param string|array $headers |
24
|
|
|
* @param bool $associative |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
15498 |
|
public static function normalize($headers, $associative = true) |
29
|
|
|
{ |
30
|
15498 |
|
$normalizedHeaders = []; |
31
|
|
|
|
32
|
15498 |
|
if (!$associative) { |
33
|
4377 |
|
$headers = self::normalize($headers); |
34
|
3367 |
|
} |
35
|
|
|
|
36
|
15498 |
|
foreach (HeadersParser::parse($headers) as $name => $value) { |
37
|
15435 |
|
if (strpos($value, 'HTTP/') === 0) { |
38
|
4257 |
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
15435 |
|
list($name, $value) = explode(':', $value, 2); |
42
|
|
|
|
43
|
15435 |
|
$name = self::normalizeHeaderName($name); |
44
|
15435 |
|
$value = self::normalizeHeaderValue($value); |
45
|
|
|
|
46
|
15435 |
|
if (!$associative) { |
47
|
4377 |
|
$normalizedHeaders[] = $name.': '.$value; |
48
|
3367 |
|
} else { |
49
|
15435 |
|
$normalizedHeaders[$name] = isset($normalizedHeaders[$name]) |
50
|
11903 |
|
? $normalizedHeaders[$name].', '.$value |
51
|
3577 |
|
: $value; |
52
|
|
|
} |
53
|
11942 |
|
} |
54
|
|
|
|
55
|
15498 |
|
return $normalizedHeaders; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
15444 |
|
public static function normalizeHeaderName($name) |
64
|
|
|
{ |
65
|
15444 |
|
return trim($name); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array|string $value |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
15669 |
|
public static function normalizeHeaderValue($value) |
74
|
|
|
{ |
75
|
15669 |
|
return implode(', ', array_map('trim', is_array($value) ? $value : explode(',', $value))); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|