* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ declare(strict_types=1); namespace League\Uri\Components; use League\Uri\Contracts\AuthorityInterface; use League\Uri\Contracts\IpHostInterface; use League\Uri\Contracts\UriInterface; use League\Uri\Exceptions\ConversionFailed; use League\Uri\Exceptions\MissingFeature; use League\Uri\Exceptions\SyntaxError; use League\Uri\Idna\Converter as IdnConverter; use League\Uri\IPv4\Converter as IPv4Converter; use League\Uri\IPv4Normalizer; use Psr\Http\Message\UriInterface as Psr7UriInterface; use Stringable; use function explode; use function filter_var; use function in_array; use function inet_pton; use function preg_match; use function rawurldecode; use function rawurlencode; use function sprintf; use function strpos; use function strtolower; use function substr; use const FILTER_FLAG_IPV4; use const FILTER_FLAG_IPV6; use const FILTER_VALIDATE_IP; final class Host extends Component implements IpHostInterface { protected const REGEXP_NON_ASCII_PATTERN = '/[^\x20-\x7f]/'; /** * @see https://tools.ietf.org/html/rfc3986#section-3.2.2 * * invalid characters in host regular expression */ private const REGEXP_INVALID_HOST_CHARS = '/ [:\/?#\[\]@ ] # gen-delims characters as well as the space character /ix'; /** * General registered name regular expression. * * @see https://tools.ietf.org/html/rfc3986#section-3.2.2 * @see https://regex101.com/r/fptU8V/1 */ private const REGEXP_REGISTERED_NAME = '/ (?(DEFINE) (?[a-z0-9_~\-]) # . is missing as it is used to separate labels (?[!$&\'()*+,;=]) (?%[A-F0-9]{2}) (?(?:(?&unreserved)|(?&sub_delims)|(?&encoded))*) ) ^(?:(?®_name)\.)*(?®_name)\.?$ /ix'; /** * Domain name regular expression. * * Everything but the domain name length is validated * * @see https://tools.ietf.org/html/rfc1034#section-3.5 * @see https://tools.ietf.org/html/rfc1123#section-2.1 * @see https://regex101.com/r/71j6rt/1 */ private const REGEXP_DOMAIN_NAME = '/ (?(DEFINE) (? [a-z0-9]) # alpha digit (? [a-z0-9-]) # alpha digit and hyphen (? (?&let_dig_hyp){0,61}(?&let_dig)) # domain label end (?