Skip to content

Commit

Permalink
Improve Uri::fromHeaderLinkValue
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 24, 2025
1 parent c84430b commit 33879de
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ public static function provideInvalidHeaderLinkValue(): iterable
yield 'header value with missing semicolon' => ['html' => '</style.css> title="stylesheet"'];
yield 'header value with missing parameters' => ['html' => '</style.css>'];
yield 'header value with missing rel parameter' => ['html' => '</style.css> title="stylesheet"'];
yield 'header value with invalid parameters' => ['html' => '<https://example.com/page1> title="prev"; rel="Previous Page"'];
}

#[Test]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ IPv4 conversion requires at least one of the following:
otherwise an exception will be thrown when attempting to convert a host
as an IPv4 address.

Parsing or generating HTML related content requires the `dom` extension.

Dependencies
-------

Expand Down
7 changes: 6 additions & 1 deletion Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,13 @@ public static function fromHeaderLinkValue(Stringable|string $headerValue, Strin
throw new InvalidArgumentException('As per RFC8288, the URI must be defined inside two `<>` characters.');
}

$parameters = ltrim($matches['parameters']);
if (!str_starts_with($parameters, ';')) {
throw new InvalidArgumentException('The value `'.$headerValue.'` contains invalid characters.');
}

$attributes = [];
if (false !== preg_match_all('/;\s*(?<name>\w*)\*?="(?<value>[^"]*)"/', $matches['parameters'], $attrMatches, PREG_SET_ORDER)) {
if (false !== preg_match_all('/;\s*(?<name>\w*)\*?="(?<value>[^"]*)"/', $parameters, $attrMatches, PREG_SET_ORDER)) {
foreach ($attrMatches as $attrMatch) {
$attributes[$attrMatch['name']] = $attrMatch['value'];
}
Expand Down

0 comments on commit 33879de

Please sign in to comment.