Skip to content

Commit 7f0ee00

Browse files
authored
Fix use of UTF-8 characters to host in Uri class (#23)
* Additional tests for strtolower function checking to UTF-8 * Fixed use of UTF-8 characters to host in Uri class
1 parent 2b2b114 commit 7f0ee00

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/Uri.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use function rawurlencode;
2323
use function sprintf;
2424
use function strtolower;
25+
use function strtr;
2526

2627
final class Uri implements UriInterface
2728
{
@@ -406,7 +407,7 @@ private function normalizeUserInfo(string $user, ?string $pass = null): string
406407
*/
407408
private function normalizeHost(string $host): string
408409
{
409-
return strtolower($host);
410+
return strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
410411
}
411412

412413
/**

tests/UriTest.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -442,17 +442,13 @@ public function testWithFragmentThrowExceptionForInvalidFragment($query): void
442442
$this->uri->withQuery($query);
443443
}
444444

445-
public function testUtf8Host(): void
446-
{
447-
$uri = (new Uri())->withHost($host = '例子.例子');
448-
$this->assertSame($host, $uri->getHost());
449-
$this->assertSame('//' . $host, (string) $uri);
450-
}
451-
452445
public function testPercentageEncodedWillNotBeReEncoded(): void
453446
{
454-
$uri = new Uri('https://example.com/pa<th/to/tar>get/?qu^ery=str|ing#frag%ment');
455-
$this->assertSame('https://example.com/pa%3Cth/to/tar%3Eget/?qu%5Eery=str%7Cing#frag%25ment', (string) $uri);
447+
$uri = new Uri('https://us%40er:pa%[email protected]/pa<th/to/tar>get/?qu^ery=str|ing#frag%ment');
448+
$this->assertSame(
449+
'https://us%40er:pa%[email protected]/pa%3Cth/to/tar%3Eget/?qu%5Eery=str%7Cing#frag%25ment',
450+
(string) $uri,
451+
);
456452

457453
$newUri = new Uri((string) $uri);
458454
$this->assertSame((string) $uri, (string) $newUri);
@@ -467,6 +463,41 @@ public function testPercentageEncodedWillNotBeReEncoded(): void
467463
$this->assertSame($fragment, $uri->getFragment());
468464
}
469465

466+
public function testUtf8Host(): void
467+
{
468+
$uri = new Uri('https://ουτοπία.δπθ.gr/');
469+
$this->assertSame('ουτοπία.δπθ.gr', $uri->getHost());
470+
$new = $uri->withHost($host = '程式设计.com');
471+
$this->assertSame($host, $new->getHost());
472+
473+
$uri = (new Uri())->withHost($host = '例子.例子');
474+
$this->assertSame($host, $uri->getHost());
475+
$this->assertSame('//' . $host, (string) $uri);
476+
477+
$uri = (new Uri())->withHost($host = 'παράδειγμα.δοκιμή');
478+
$this->assertSame($host, $uri->getHost());
479+
$this->assertSame('//' . $host, (string) $uri);
480+
481+
$uri = (new Uri())->withHost($host = 'яндекс.рф');
482+
$this->assertSame($host, $uri->getHost());
483+
484+
// "A" is a Latin letter
485+
$uri = (new Uri())->withHost('яндекAс.рф');
486+
$this->assertSame('яндекaс.рф', $uri->getHost());
487+
}
488+
489+
public function testIPv6Host(): void
490+
{
491+
$uri = new Uri('https://[2a00:f48:1008::212:183:10]');
492+
$this->assertSame('[2a00:f48:1008::212:183:10]', $uri->getHost());
493+
494+
$uri = new Uri('https://[2a00:f48:1008::212:183:10]:56/path/to/target?key=value');
495+
$this->assertSame('[2a00:f48:1008::212:183:10]', $uri->getHost());
496+
$this->assertSame(56, $uri->getPort());
497+
$this->assertSame('/path/to/target', $uri->getPath());
498+
$this->assertSame('key=value', $uri->getQuery());
499+
}
500+
470501
/**
471502
* @param array $values
472503
* @return array

0 commit comments

Comments
 (0)