Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function __construct(
$password = ''
) {
$this->scheme = $this->filterScheme($scheme);
$this->host = strtolower($host);
$this->host = $this->filterHost($host);
$this->port = $this->filterPort($port);
$this->path = empty($path) ? '/' : $this->filterPath($path);
$this->query = $this->filterQuery($query);
Expand Down Expand Up @@ -390,11 +390,32 @@ public function getHost()
public function withHost($host)
{
$clone = clone $this;
$clone->host = strtolower($host);
$clone->host = $this->filterHost($host);

return $clone;
}

/**
* Filter Uri host.
*
* @param string $host The Uri port number.
*
* @return string
* @throws \InvalidArgumentException for invalid host names.
*/
protected function filterHost($host)
{
if (!is_string($host) && !method_exists($host, '__toString')) {
throw new InvalidArgumentException('Uri host must be a string');
}

if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$host = '[' . $host . ']';
}

return strtolower($host);
}

/**
* Retrieve the port component of the URI.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ public function testWithHost()
$this->assertAttributeEquals('slimframework.com', 'host', $uri);
}

public function testFilterHost()
{
$uri = new Uri('http', '2001:db8::1');

$this->assertEquals('[2001:db8::1]', $uri->getHost());
}

public function testGetPortWithSchemeAndNonDefaultPort()
{
$uri = new Uri('https', 'www.example.com', 4000);
Expand Down