Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for UTF-8 in hostname #131

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ public function __construct(string $uri = '')
if (false === $parts = \parse_url($uri)) {
throw new \InvalidArgumentException("Unable to parse URI: $uri");
}

// Apply parse_url parts to a URI.
$this->scheme = isset($parts['scheme']) ? \strtolower($parts['scheme']) : '';
$this->userInfo = $parts['user'] ?? '';
$this->host = isset($parts['host']) ? \strtolower($parts['host']) : '';
$this->host = isset($parts['host']) ? $this->filterHost($parts['host']) : '';
$this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null;
$this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : '';
$this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : '';
Expand Down Expand Up @@ -163,7 +162,7 @@ public function withHost($host): self
throw new \InvalidArgumentException('Host must be a string');
}

if ($this->host === $host = \strtolower($host)) {
if ($this->host === $host = $this->filterHost($host)) {
return $this;
}

Expand Down Expand Up @@ -271,6 +270,17 @@ private static function isNonStandardPort(string $scheme, int $port): bool
return !isset(self::SCHEMES[$scheme]) || $port !== self::SCHEMES[$scheme];
}

private function filterHost($host): string
{
return \preg_replace_callback(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this implementation is going to be very slow.
My suggestion instead:
strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');

'/([A-Z])/',
static function ($cap) {
return \strtolower($cap[0]);
},
$host
);
}

private function filterPort($port): ?int
{
if (null === $port) {
Expand Down
9 changes: 9 additions & 0 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,13 @@ public function testImmutability()
$this->assertNotSame($uri, $uri->withQuery('q=abc'));
$this->assertNotSame($uri, $uri->withFragment('test'));
}

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

}