Skip to content

Commit

Permalink
Improve URI normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 23, 2024
1 parent 0a5fc0e commit 1fcd6ef
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ final class Uri implements UriInterface
*/
private const REGEXP_WINDOW_PATH = ',^(?<root>[a-zA-Z][:|\|]),';

/**
* Unreserved characters.
*
* @see https://www.rfc-editor.org/rfc/rfc3986.html#section-2.3
*/
private const REGEXP_UNRESERVED_CHARACTERS = ',%(2[1-9A-Fa-f]|[3-7][0-9A-Fa-f]|61|62|64|65|66|7[AB]|5F),';

/**
* Supported schemes and corresponding default port.
*
Expand Down Expand Up @@ -1419,13 +1426,14 @@ private function normalizePath(string $path, ?string $authority): string

private function normalizeQuery(?string $query): ?string
{
if (null === $query) {
return null;
}

static $regexpEncodedChars = '/%(2[1-9A-Fa-f]|[3-7][0-9A-Fa-f]|61|62|64|65|66|7[AB]|5F)/';

return preg_replace_callback($regexpEncodedChars, static fn (array $matches): string => rawurldecode($matches[0]), $query) ?? '';
return match (null) {
$query => null,
default => preg_replace_callback(
self::REGEXP_UNRESERVED_CHARACTERS,
static fn (array $matches): string => rawurldecode($matches[0]),
$query
) ?? '',
};
}

public function toNormalizedString(): string
Expand Down

0 comments on commit 1fcd6ef

Please sign in to comment.