Skip to content

Commit 19b062c

Browse files
committed
Update Token regex
1 parent 9978053 commit 19b062c

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/Parser.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,19 @@ private static function parseDisplayString(ParsingInput $string): DisplayString
334334
*/
335335
private static function parseToken(ParsingInput $input): Token
336336
{
337-
// Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
338-
// 3.2.6. Field Value Components
339-
// @see https://tools.ietf.org/html/rfc7230#section-3.2.6
340-
$tchar = preg_quote("!#$%&'*+-.^_`|~");
337+
// RFC 9110: HTTP Semantics (5.6.2. Tokens)
338+
// @see https://www.rfc-editor.org/rfc/rfc9110.html#name-tokens
339+
// $tchar = preg_quote("!#$%&'*+-.^_`|~");
340+
$tchar = "!#$%&'*+\-.^_`|~";
341341

342342
// parseToken is only called by parseBareItem if the initial character
343343
// is valid, so a Token object is always returned. If there is an
344344
// invalid character in the token, the public function that was called
345345
// will detect that the remainder of the input string is invalid.
346-
return new Token($input->consumeRegex('/^([a-z*][a-z0-9:\/' . $tchar . ']*)/i'));
346+
return new Token($input->consumeRegex('/^(
347+
(?:\*|[a-z]) # an alphabetic character or "*"
348+
[a-z0-9:\/' . $tchar . ']* # zero to many token characters
349+
)/ix'));
347350
}
348351

349352
/**

src/Serializer.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,17 @@ private static function serializeDisplayString(DisplayString $value): string
224224

225225
private static function serializeToken(Token $value): string
226226
{
227-
// Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
228-
// 3.2.6. Field Value Components
229-
// @see https://tools.ietf.org/html/rfc7230#section-3.2.6
230-
$tchar = preg_quote("!#$%&'*+-.^_`|~");
231-
232-
if (!preg_match('/^((?:\*|[a-z])[a-z0-9:\/' . $tchar . ']*)$/i', (string) $value)) {
227+
// RFC 9110: HTTP Semantics (5.6.2. Tokens)
228+
// @see https://www.rfc-editor.org/rfc/rfc9110.html#name-tokens
229+
// $tchar = preg_quote("!#$%&'*+-.^_`|~");
230+
$tchar = "!#$%&'*+\-.^_`|~";
231+
232+
if (
233+
!preg_match('/^(
234+
(?:\*|[a-z]) # an alphabetic character or "*"
235+
[a-z0-9:\/' . $tchar . ']* # zero to many token characters
236+
)$/ix', (string) $value)
237+
) {
233238
throw new SerializeException('Invalid characters in token');
234239
}
235240

0 commit comments

Comments
 (0)