vendor/guzzlehttp/psr7/src/Header.php
180 lines · 5093 bytes
1<?php
2
3declare(strict_types=1);
4
5namespace GuzzleHttp\Psr7;
6
7final class Header
8{
9 /**
10 * Parse an array of header values containing ";" separated data into an
11 * array of associative arrays representing the header key value pair data
12 * of the header. When a parameter does not contain a value, but just
13 * contains a key, this function will inject a key with a '' string value.
14 *
15 * @param string|array $header Header to parse into components.
16 */
17 public static function parse($header): array
18 {
19 static $trimmed = "\"' \n\t\r";
20 $params = $matches = [];
21
22 foreach ((array) $header as $value) {
23 foreach (self::splitList($value) as $val) {
24 $part = [];
25 foreach (self::splitParameters($val) as $kvp) {
26 if (preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) {
27 $m = $matches[0];
28 if (isset($m[1])) {
29 $part[trim($m[0], $trimmed)] = trim($m[1], $trimmed);
30 } else {
31 $part[] = trim($m[0], $trimmed);
32 }
33 }
34 }
35 if ($part) {
36 $params[] = $part;
37 }
38 }
39 }
40
41 return $params;
42 }
43
44 /**
45 * Split a header value into semicolon-separated parameters.
46 *
47 * @return string[]
48 */
49 private static function splitParameters(string $value): array
50 {
51 $values = [];
52 $start = 0;
53 $isQuoted = false;
54 $isEscaped = false;
55
56 for ($i = 0, $max = \strlen($value); $i < $max; ++$i) {
57 $char = $value[$i];
58
59 if ($isEscaped) {
60 $isEscaped = false;
61
62 continue;
63 }
64
65 if ($isQuoted && $char === '\\') {
66 $isEscaped = true;
67
68 continue;
69 }
70
71 if ($char === '"') {
72 $isQuoted = !$isQuoted;
73
74 continue;
75 }
76
77 if (!$isQuoted && $char === ';') {
78 $values[] = \substr($value, $start, $i - $start);
79 $start = $i + 1;
80 }
81 }
82
83 $values[] = \substr($value, $start);
84
85 return $values;
86 }
87
88 /**
89 * Converts an array of header values that may contain comma separated
90 * headers into an array of headers with no comma separated values.
91 *
92 * @param string|array $header Header to normalize.
93 *
94 * @deprecated Use self::splitList() instead.
95 */
96 public static function normalize($header): array
97 {
98 \trigger_deprecation('guzzlehttp/psr7', '2.3', 'Header::normalize() is deprecated and will be removed in guzzlehttp/psr7 3.0. Use Header::splitList() instead.');
99
100 $result = [];
101 foreach ((array) $header as $value) {
102 foreach (self::splitList($value) as $parsed) {
103 $result[] = $parsed;
104 }
105 }
106
107 return $result;
108 }
109
110 /**
111 * Splits a HTTP header defined to contain a comma-separated list into
112 * each individual value. Empty values will be removed.
113 *
114 * Example headers include 'accept', 'cache-control' and 'if-none-match'.
115 *
116 * This method must not be used to parse headers that are not defined as
117 * a list, such as 'user-agent' or 'set-cookie'.
118 *
119 * @param string|string[] $values Header value as returned by MessageInterface::getHeader()
120 *
121 * @return string[]
122 */
123 public static function splitList($values): array
124 {
125 if (!\is_array($values)) {
126 $values = [$values];
127 }
128
129 $result = [];
130 foreach ($values as $value) {
131 if (!\is_string($value)) {
132 throw new \TypeError('$header must either be a string or an array containing strings.');
133 }
134
135 $v = '';
136 $isQuoted = false;
137 $isEscaped = false;
138 for ($i = 0, $max = \strlen($value); $i < $max; ++$i) {
139 if ($isEscaped) {
140 $v .= $value[$i];
141 $isEscaped = false;
142
143 continue;
144 }
145
146 if (!$isQuoted && $value[$i] === ',') {
147 $v = \trim($v, " \n\r\t\0\x0B");
148 if ($v !== '') {
149 $result[] = $v;
150 }
151
152 $v = '';
153 continue;
154 }
155
156 if ($isQuoted && $value[$i] === '\\') {
157 $isEscaped = true;
158 $v .= $value[$i];
159
160 continue;
161 }
162 if ($value[$i] === '"') {
163 $isQuoted = !$isQuoted;
164 $v .= $value[$i];
165
166 continue;
167 }
168
169 $v .= $value[$i];
170 }
171
172 $v = \trim($v, " \n\r\t\0\x0B");
173 if ($v !== '') {
174 $result[] = $v;
175 }
176 }
177
178 return $result;
179 }
180}