Skip to content

Commit 43fe2b8

Browse files
committed
Imrpove Host normalization
1 parent d4f348e commit 43fe2b8

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

interfaces/UriString.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,6 @@ public static function isHost(Stringable|string|null $host): bool
782782
*/
783783
private static function filterRegisteredName(string $host): void
784784
{
785-
if ($host !== Encoder::decodeUnreservedCharacters($host)) {
786-
throw new SyntaxError('Host `'.$host.'` is invalid: only UTF-8 characters sequence can be percent encoded in host.');
787-
}
788-
789785
$formattedHost = rawurldecode($host);
790786
if ($formattedHost !== $host) {
791787
if (IdnaConverter::toAscii($formattedHost)->hasErrors()) {

interfaces/UriStringTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,6 @@ public static function invalidUriProvider(): array
745745
'invalid port (1)' => ['//host:port/path?query#fragment'],
746746
'invalid port (2)' => ['//host:-892358/path?query#fragment'],
747747
'invalid host' => ['http://exam ple.com'],
748-
'invalid host with invalid encoded characters' => ['http://ex%61mple.com'],
749748
'invalid ipv6 host (1)' => ['scheme://[127.0.0.1]/path?query#fragment'],
750749
'invalid ipv6 host (2)' => ['scheme://]::1[/path?query#fragment'],
751750
'invalid ipv6 host (3)' => ['scheme://[::1|/path?query#fragment'],

uri/Uri.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -382,24 +382,23 @@ private function formatHost(?string $host): ?string
382382
private function formatRegisteredName(string $host): string
383383
{
384384
$formattedHost = rawurldecode($host);
385-
if ($formattedHost !== $host) {
386-
387-
if (IdnaConverter::toAscii($formattedHost)->hasErrors()) {
388-
throw new SyntaxError('The host `'.$host.'` is invalid : the registered name contains invalid characters.');
389-
}
385+
if ($formattedHost === $host) {
386+
return match (1) {
387+
preg_match(self::REGEXP_HOST_REGNAME, $formattedHost) => $formattedHost,
388+
preg_match(self::REGEXP_HOST_GEN_DELIMS, $formattedHost) => throw new SyntaxError('The host `'.$host.'` is invalid : a registered name cannot contain URI delimiters or spaces.'),
389+
default => IdnaConverter::toAsciiOrFail($host),
390+
};
391+
}
390392

391-
return (string) preg_replace_callback(
392-
'/%[0-9A-F]{2}/i',
393-
fn (array $matches) => strtoupper($matches[0]),
394-
strtolower($host)
395-
);
393+
if (IdnaConverter::toAscii($formattedHost)->hasErrors()) {
394+
throw new SyntaxError('The host `'.$host.'` is invalid : the registered name contains invalid characters.');
396395
}
397396

398-
return match (1) {
399-
preg_match(self::REGEXP_HOST_REGNAME, $formattedHost) => $formattedHost,
400-
preg_match(self::REGEXP_HOST_GEN_DELIMS, $formattedHost) => throw new SyntaxError('The host `'.$host.'` is invalid : a registered name cannot contain URI delimiters or spaces.'),
401-
default => IdnaConverter::toAsciiOrFail($host),
402-
};
397+
return (string) preg_replace_callback(
398+
'/%[0-9A-F]{2}/i',
399+
fn (array $matches) => 1 === preg_match('/%([0-7][0-9a-f])/', $matches[0]) ? rawurldecode($matches[0]) : strtoupper($matches[0]),
400+
strtolower($host)
401+
);
403402
}
404403

405404
/**
@@ -1185,7 +1184,6 @@ public function toDisplayString(): string
11851184
{
11861185
$components = $this->toComponents();
11871186

1188-
11891187
unset($components['port']);
11901188
if (null !== $components['host']) {
11911189
$components['host'] = IdnaConverter::toUnicode($components['host'])->domain();

0 commit comments

Comments
 (0)