From 46cc8e47c7432a29a43667510b6171d0aec4ab2f Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sat, 24 Jun 2017 00:49:25 +0200 Subject: [PATCH] Remove normalizeDnsName and isValidDnsName, as they're in amphp/uri now --- composer.json | 1 + lib/BasicResolver.php | 1 + lib/HostLoader.php | 4 +++- lib/functions.php | 50 ------------------------------------------- 4 files changed, 5 insertions(+), 51 deletions(-) diff --git a/composer.json b/composer.json index 975a2d4..dc99a06 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "amphp/cache": "^1", "amphp/file": "^0.2", "amphp/parser": "^1", + "amphp/uri": "^0.1", "amphp/windows-registry": "^0.3", "daverandom/libdns": "^1" }, diff --git a/lib/BasicResolver.php b/lib/BasicResolver.php index 7b5030a..16bf7dd 100644 --- a/lib/BasicResolver.php +++ b/lib/BasicResolver.php @@ -13,6 +13,7 @@ use LibDNS\Messages\MessageTypes; use LibDNS\Records\Question; use LibDNS\Records\QuestionFactory; use function Amp\call; +use function Amp\Uri\normalizeDnsName; final class BasicResolver implements Resolver { const CACHE_PREFIX = "amphp.dns."; diff --git a/lib/HostLoader.php b/lib/HostLoader.php index 483b0f2..aba83d9 100644 --- a/lib/HostLoader.php +++ b/lib/HostLoader.php @@ -4,7 +4,9 @@ namespace Amp\Dns; use Amp\File; use Amp\Promise; +use Amp\Uri\InvalidDnsNameException; use function Amp\call; +use function Amp\Uri\normalizeDnsName; class HostLoader { private $path; @@ -50,7 +52,7 @@ class HostLoader { try { $normalizedName = normalizeDnsName($parts[$i]); $data[$key][$normalizedName] = $parts[0]; - } catch (InvalidNameError $e) { + } catch (InvalidDnsNameException $e) { // ignore invalid entries } } diff --git a/lib/functions.php b/lib/functions.php index 9cc97f3..44bc314 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -52,53 +52,3 @@ function resolve(string $name, int $typeRestriction = null): Promise { function query(string $name, int $type): Promise { return resolver()->query($name, $type); } - -/** - * Checks whether a string is a valid DNS name. - * - * @param string $name String to check. - * - * @return bool - */ -function isValidDnsName(string $name) { - try { - normalizeDnsName($name); - return true; - } catch (InvalidNameError $e) { - return false; - } -} - -/** - * Normalizes a DNS name and automatically checks it for validity. - * - * @param string $name DNS name. - * - * @return string Normalized DNS name. - * - * @throws InvalidNameError If an invalid name or an IDN name without ext/intl being installed has been passed. - */ -function normalizeDnsName(string $name): string { - static $pattern = '/^(?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)(\.(?&name))*$/i'; - - if (\function_exists('idn_to_ascii')) { - if (false === $result = \idn_to_ascii($name, 0, INTL_IDNA_VARIANT_UTS46)) { - throw new InvalidNameError("Name '{$name}' could not be processed for IDN."); - } - - $name = $result; - } else { - if (\preg_match('/[\x80-\xff]/', $name)) { - throw new InvalidNameError( - "Name '{$name}' contains non-ASCII characters and IDN support is not available. " . - "Verify that ext/intl is installed for IDN support." - ); - } - } - - if (isset($name[253]) || !\preg_match($pattern, $name)) { - throw new InvalidNameError("Name '{$name}' is not a valid hostname."); - } - - return $name; -}