Exception
1 year ago
AppendStream.php
2 months ago
BufferStream.php
2 months ago
CachingStream.php
2 months ago
DroppingStream.php
2 months ago
FnStream.php
2 months ago
Header.php
2 months ago
HttpFactory.php
1 year ago
InflateStream.php
1 year ago
LazyOpenStream.php
1 year ago
LimitStream.php
2 months ago
Message.php
2 months ago
MessageTrait.php
2 months ago
MimeType.php
2 months ago
MultipartStream.php
2 months ago
NoSeekStream.php
2 months ago
PumpStream.php
2 months ago
Query.php
1 year ago
Request.php
2 months ago
Response.php
2 months ago
Rfc3986.php
2 months ago
Rfc7230.php
2 months ago
ServerRequest.php
2 months ago
Stream.php
2 months ago
StreamDecoratorTrait.php
2 months ago
StreamWrapper.php
2 months ago
UploadedFile.php
2 months ago
Uri.php
2 months ago
UriComparator.php
2 months ago
UriNormalizer.php
2 months ago
UriResolver.php
2 months ago
Utils.php
2 months ago
Header.php
179 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace GuzzleHttp\Psr7; |
| 6 | |
| 7 | final 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 | $result = []; |
| 99 | foreach ((array) $header as $value) { |
| 100 | foreach (self::splitList($value) as $parsed) { |
| 101 | $result[] = $parsed; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return $result; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Splits a HTTP header defined to contain a comma-separated list into |
| 110 | * each individual value. Empty values will be removed. |
| 111 | * |
| 112 | * Example headers include 'accept', 'cache-control' and 'if-none-match'. |
| 113 | * |
| 114 | * This method must not be used to parse headers that are not defined as |
| 115 | * a list, such as 'user-agent' or 'set-cookie'. |
| 116 | * |
| 117 | * @param string|string[] $values Header value as returned by MessageInterface::getHeader() |
| 118 | * |
| 119 | * @return string[] |
| 120 | */ |
| 121 | public static function splitList($values): array |
| 122 | { |
| 123 | if (!\is_array($values)) { |
| 124 | $values = [$values]; |
| 125 | } |
| 126 | |
| 127 | $result = []; |
| 128 | foreach ($values as $value) { |
| 129 | if (!\is_string($value)) { |
| 130 | throw new \TypeError('$header must either be a string or an array containing strings.'); |
| 131 | } |
| 132 | |
| 133 | $v = ''; |
| 134 | $isQuoted = false; |
| 135 | $isEscaped = false; |
| 136 | for ($i = 0, $max = \strlen($value); $i < $max; ++$i) { |
| 137 | if ($isEscaped) { |
| 138 | $v .= $value[$i]; |
| 139 | $isEscaped = false; |
| 140 | |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | if (!$isQuoted && $value[$i] === ',') { |
| 145 | $v = \trim($v); |
| 146 | if ($v !== '') { |
| 147 | $result[] = $v; |
| 148 | } |
| 149 | |
| 150 | $v = ''; |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | if ($isQuoted && $value[$i] === '\\') { |
| 155 | $isEscaped = true; |
| 156 | $v .= $value[$i]; |
| 157 | |
| 158 | continue; |
| 159 | } |
| 160 | if ($value[$i] === '"') { |
| 161 | $isQuoted = !$isQuoted; |
| 162 | $v .= $value[$i]; |
| 163 | |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | $v .= $value[$i]; |
| 168 | } |
| 169 | |
| 170 | $v = \trim($v); |
| 171 | if ($v !== '') { |
| 172 | $result[] = $v; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return $result; |
| 177 | } |
| 178 | } |
| 179 |