Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Adding isValidHost method
Browse files Browse the repository at this point in the history
Improve Host Validation according to RFC3986

- https://tools.ietf.org/html/rfc3986#appendix-A
- https://tools.ietf.org/html/rfc3986#section-3.2.2

A host can be undefined it is up to the URI scheme
to validate if the host presence is required or not
  • Loading branch information
nyamsprod committed Dec 7, 2016
1 parent ae865a2 commit a7fcd7d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All Notable changes to `league-uri-parser` will be documented in this file

### Added

- `Parser::isValidHost` method
- `ParserException::createFromInvalidScheme` replaces `ParserException::createFromInvalidState` usage
- `ParserException::createFromInvalidPath` replaces `ParserException::createFromInvalidState` usage

Expand Down
18 changes: 15 additions & 3 deletions src/HostValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,27 @@ trait HostValidation
*/
protected function filterHost($host)
{
if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
|| $this->isValidHostnameIpv6($host)
|| $this->isValidHostname($host)) {
if ($this->isValidHost($host)) {
return $host;
}

throw ParserException::createFromInvalidHost($host);
}

/**
* Tell whether a Host is valid
*
* @param string $host
* @return bool
*/
public function isValidHost($host)
{
return '' == $host
|| filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
|| $this->isValidHostnameIpv6($host)
|| $this->isValidHostname($host);
}

/**
* validate an Ipv6 Hostname
*
Expand Down
1 change: 0 additions & 1 deletion test/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ public function testInvalidURI()
'invalid host and URI' => ['2620:0:1cfe:face:b00c::3'],
'invalid scheme and path' => ['0scheme://host/path?query#fragment'],
'invalid path PHP bug #72811' => ['[::1]:80'],
'invalid uri with authority without host' => ['//user@:80'],
];
}
}

0 comments on commit a7fcd7d

Please sign in to comment.