-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLinkTemplateHeaderParser.php
More file actions
266 lines (207 loc) · 8.47 KB
/
Copy pathLinkTemplateHeaderParser.php
File metadata and controls
266 lines (207 loc) · 8.47 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\WebLink;
use Psr\Link\EvolvableLinkProviderInterface;
use Symfony\Component\WebLink\Exception\InvalidArgumentException;
/**
* Parses a list of HTTP Link-Template headers into a list of Link instances.
*
* As mandated by RFC 9651 for structured fields, a header value that cannot be
* parsed is ignored as a whole and yields an empty list of links.
*
* @see https://www.rfc-editor.org/rfc/rfc9652.html
*
* @author Florent Morselli <florent.morselli@spomky-labs.com>
*/
class LinkTemplateHeaderParser
{
/**
* @param string|string[] $headers Value of the "Link-Template" HTTP header
*/
public function parse(string|array $headers): EvolvableLinkProviderInterface
{
if (\is_array($headers)) {
$headers = implode(', ', $headers);
}
try {
return self::parseList($headers);
} catch (InvalidArgumentException) {
return new GenericLinkProvider();
}
}
private static function parseList(string $input): GenericLinkProvider
{
$links = new GenericLinkProvider();
$offset = 0;
self::skipSp($input, $offset);
if ($offset >= \strlen($input)) {
return $links;
}
while (true) {
$href = match ($input[$offset] ?? null) {
'"' => self::parseString($input, $offset),
'%' => self::parseDisplayString($input, $offset),
default => throw new InvalidArgumentException('A Link-Template member must be a structured field string.'),
};
$link = new Link(null, $href);
foreach (self::parseParameters($input, $offset) as $key => $value) {
if ('rel' === $key) {
if (\is_string($value)) {
foreach (preg_split('/\s+/', $value, 0, \PREG_SPLIT_NO_EMPTY) as $rel) {
$link = $link->withRel($rel);
}
}
continue;
}
$link = $link->withAttribute($key, $value);
}
$links = $links->withLink($link);
self::skipOws($input, $offset);
if ($offset >= \strlen($input)) {
return $links;
}
if (',' !== $input[$offset]) {
throw new InvalidArgumentException('Expected a comma between Link-Template members.');
}
++$offset;
self::skipOws($input, $offset);
if ($offset >= \strlen($input)) {
throw new InvalidArgumentException('The Link-Template header must not end with a trailing comma.');
}
}
}
/**
* @return array<string, string|int|float|bool>
*/
private static function parseParameters(string $input, int &$offset): array
{
$parameters = [];
while (';' === ($input[$offset] ?? null)) {
++$offset;
self::skipSp($input, $offset);
if (!preg_match('/[a-z*][a-z0-9_.*-]*/A', $input, $matches, 0, $offset)) {
throw new InvalidArgumentException('Expected a structured field parameter key.');
}
$offset += \strlen($matches[0]);
$value = true;
if ('=' === ($input[$offset] ?? null)) {
++$offset;
$value = self::parseBareItem($input, $offset);
}
$parameters[$matches[0]] = $value;
}
return $parameters;
}
private static function parseBareItem(string $input, int &$offset): string|int|float|bool
{
return match (true) {
!isset($input[$offset]) => throw new InvalidArgumentException('Unexpected end of the Link-Template header.'),
'"' === $input[$offset] => self::parseString($input, $offset),
'%' === $input[$offset] => self::parseDisplayString($input, $offset),
'?' === $input[$offset] => self::parseBoolean($input, $offset),
'-' === $input[$offset] || ctype_digit($input[$offset]) => self::parseNumber($input, $offset),
'*' === $input[$offset] || ctype_alpha($input[$offset]) => self::parseToken($input, $offset),
default => throw new InvalidArgumentException(\sprintf('Unexpected "%s" character in the Link-Template header.', $input[$offset])),
};
}
private static function parseString(string $input, int &$offset): string
{
++$offset;
$output = '';
while (isset($input[$offset])) {
$char = $input[$offset++];
if ('\\' === $char) {
if (!isset($input[$offset])) {
throw new InvalidArgumentException('Unterminated escape sequence in the Link-Template header.');
}
$next = $input[$offset++];
if ('"' !== $next && '\\' !== $next) {
throw new InvalidArgumentException('Invalid escape sequence in the Link-Template header.');
}
$output .= $next;
continue;
}
if ('"' === $char) {
return $output;
}
if (0x20 > \ord($char) || 0x7E < \ord($char)) {
throw new InvalidArgumentException('A structured field string must hold printable ASCII characters only.');
}
$output .= $char;
}
throw new InvalidArgumentException('Unterminated string in the Link-Template header.');
}
private static function parseDisplayString(string $input, int &$offset): string
{
if ('%"' !== substr($input, $offset, 2)) {
throw new InvalidArgumentException('Expected a structured field display string.');
}
$offset += 2;
$output = '';
while (isset($input[$offset])) {
$char = $input[$offset++];
if (0x20 > \ord($char) || 0x7E < \ord($char)) {
throw new InvalidArgumentException('A structured field display string must hold printable ASCII characters only.');
}
if ('%' === $char) {
if (!preg_match('/[0-9a-f]{2}/A', $input, $matches, 0, $offset)) {
throw new InvalidArgumentException('Invalid percent-encoded byte in the Link-Template header.');
}
$offset += 2;
$output .= \chr(hexdec($matches[0]));
continue;
}
if ('"' === $char) {
if (!preg_match('//u', $output)) {
throw new InvalidArgumentException('A structured field display string must be encoded in UTF-8.');
}
return $output;
}
$output .= $char;
}
throw new InvalidArgumentException('Unterminated display string in the Link-Template header.');
}
private static function parseBoolean(string $input, int &$offset): bool
{
if (!preg_match('/\?[01]/A', $input, $matches, 0, $offset)) {
throw new InvalidArgumentException('Expected a structured field boolean.');
}
$offset += 2;
return '?1' === $matches[0];
}
private static function parseNumber(string $input, int &$offset): int|float
{
if (!preg_match('/-?(?:\d{1,12}\.\d{1,3}|\d{1,15})(?![\d.])/A', $input, $matches, 0, $offset)) {
throw new InvalidArgumentException('Expected a structured field number.');
}
$offset += \strlen($matches[0]);
return str_contains($matches[0], '.') ? (float) $matches[0] : (int) $matches[0];
}
private static function parseToken(string $input, int &$offset): string
{
if (!preg_match('/[a-zA-Z*][a-zA-Z0-9:\/!#$%&\'*+.^_`|~-]*/A', $input, $matches, 0, $offset)) {
throw new InvalidArgumentException('Expected a structured field token.');
}
$offset += \strlen($matches[0]);
return $matches[0];
}
private static function skipSp(string $input, int &$offset): void
{
while (' ' === ($input[$offset] ?? null)) {
++$offset;
}
}
private static function skipOws(string $input, int &$offset): void
{
while (' ' === ($input[$offset] ?? null) || "\t" === ($input[$offset] ?? null)) {
++$offset;
}
}
}