Skip to content

Commit bb9d005

Browse files
committed
Improve Host normalization
1 parent 3b0aea2 commit bb9d005

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

Uri.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
use function strpos;
7777
use function strspn;
7878
use function strtolower;
79-
use function strtoupper;
8079
use function substr;
8180
use function trim;
8281

@@ -382,24 +381,19 @@ private function formatHost(?string $host): ?string
382381
private function formatRegisteredName(string $host): string
383382
{
384383
$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-
}
384+
if ($formattedHost === $host) {
385+
return match (1) {
386+
preg_match(self::REGEXP_HOST_REGNAME, $formattedHost) => $formattedHost,
387+
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.'),
388+
default => IdnaConverter::toAsciiOrFail($host),
389+
};
390+
}
390391

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

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-
};
396+
return (string) Encoder::normalizeHost($host);
403397
}
404398

405399
/**
@@ -1185,7 +1179,6 @@ public function toDisplayString(): string
11851179
{
11861180
$components = $this->toComponents();
11871181

1188-
11891182
unset($components['port']);
11901183
if (null !== $components['host']) {
11911184
$components['host'] = IdnaConverter::toUnicode($components['host'])->domain();

0 commit comments

Comments
 (0)