1
0
mirror of https://github.com/danog/dns.git synced 2025-01-22 13:31:12 +01:00

Remove normalizeDnsName and isValidDnsName, as they're in amphp/uri now

This commit is contained in:
Niklas Keller 2017-06-24 00:49:25 +02:00
parent 6a2e666a80
commit 46cc8e47c7
4 changed files with 5 additions and 51 deletions

View File

@ -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"
},

View File

@ -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.";

View File

@ -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
}
}

View File

@ -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 = '/^(?<name>[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;
}