Skip to content

Commit

Permalink
Fix use of UTF-8 characters to host in Uri class (#23)
Browse files Browse the repository at this point in the history
* Additional tests for strtolower function checking to UTF-8

* Fixed use of UTF-8 characters to host in Uri class
  • Loading branch information
devanych committed May 6, 2023
1 parent 2b2b114 commit 7f0ee00
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use function rawurlencode;
use function sprintf;
use function strtolower;
use function strtr;

final class Uri implements UriInterface
{
Expand Down Expand Up @@ -406,7 +407,7 @@ private function normalizeUserInfo(string $user, ?string $pass = null): string
*/
private function normalizeHost(string $host): string
{
return strtolower($host);
return strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
}

/**
Expand Down
49 changes: 40 additions & 9 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,13 @@ public function testWithFragmentThrowExceptionForInvalidFragment($query): void
$this->uri->withQuery($query);
}

public function testUtf8Host(): void
{
$uri = (new Uri())->withHost($host = '例子.例子');
$this->assertSame($host, $uri->getHost());
$this->assertSame('//' . $host, (string) $uri);
}

public function testPercentageEncodedWillNotBeReEncoded(): void
{
$uri = new Uri('https://example.com/pa<th/to/tar>get/?qu^ery=str|ing#frag%ment');
$this->assertSame('https://example.com/pa%3Cth/to/tar%3Eget/?qu%5Eery=str%7Cing#frag%25ment', (string) $uri);
$uri = new Uri('https://us%40er:pa%23ss@example.com/pa<th/to/tar>get/?qu^ery=str|ing#frag%ment');
$this->assertSame(
'https://us%40er:pa%23ss@example.com/pa%3Cth/to/tar%3Eget/?qu%5Eery=str%7Cing#frag%25ment',
(string) $uri,
);

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

public function testUtf8Host(): void
{
$uri = new Uri('https://ουτοπία.δπθ.gr/');
$this->assertSame('ουτοπία.δπθ.gr', $uri->getHost());
$new = $uri->withHost($host = '程式设计.com');
$this->assertSame($host, $new->getHost());

$uri = (new Uri())->withHost($host = '例子.例子');
$this->assertSame($host, $uri->getHost());
$this->assertSame('//' . $host, (string) $uri);

$uri = (new Uri())->withHost($host = 'παράδειγμα.δοκιμή');
$this->assertSame($host, $uri->getHost());
$this->assertSame('//' . $host, (string) $uri);

$uri = (new Uri())->withHost($host = 'яндекс.рф');
$this->assertSame($host, $uri->getHost());

// "A" is a Latin letter
$uri = (new Uri())->withHost('яндекAс.рф');
$this->assertSame('яндекaс.рф', $uri->getHost());
}

public function testIPv6Host(): void
{
$uri = new Uri('https://[2a00:f48:1008::212:183:10]');
$this->assertSame('[2a00:f48:1008::212:183:10]', $uri->getHost());

$uri = new Uri('https://[2a00:f48:1008::212:183:10]:56/path/to/target?key=value');
$this->assertSame('[2a00:f48:1008::212:183:10]', $uri->getHost());
$this->assertSame(56, $uri->getPort());
$this->assertSame('/path/to/target', $uri->getPath());
$this->assertSame('key=value', $uri->getQuery());
}

/**
* @param array $values
* @return array
Expand Down

0 comments on commit 7f0ee00

Please sign in to comment.