19 #include "validatordomain_p.h" 21 #include <QStringList> 29 ValidatorRule(* new ValidatorDomainPrivate(field, checkDNS, messages, defValKey))
44 bool hasRootDot =
false;
45 if (_v.endsWith(QLatin1Char(
'.'))) {
51 const QString v = QString::fromLatin1(QUrl::toAce(_v)).toLower();
54 const QStringList nonAceParts = _v.split(QLatin1Char(
'.'));
55 if (!nonAceParts.empty()) {
56 const QString tld = nonAceParts.last();
62 for (
const QChar &ch : tld) {
63 const ushort &uc = ch.unicode();
64 if (((uc > 47) && (uc < 58)) || (uc == 45)) {
74 if (v.length() < 254) {
75 const QStringList parts = v.split(QLatin1Char(
'.'), QString::KeepEmptyParts);
77 if (parts.size() > 1) {
79 if (parts.last().length() > 1) {
80 for (
int i = 0; i < parts.size(); ++i) {
82 const QString part = parts.at(i);
83 if (!part.isEmpty()) {
85 if (part.length() < 64) {
86 bool isTld = (i == (parts.size() -1));
87 bool isPunyCode = part.startsWith(QLatin1String(
"xn--"));
88 for (
int j = 0; j < part.size(); ++j) {
89 const ushort &uc = part.at(j).unicode();
90 const bool isDigit = ((uc > 47) && (uc < 58));
91 const bool isDash = (uc == 45);
93 if ((j == 0) && (isDash || isDigit)) {
99 if ((j == (part.size() - 1)) && isDash) {
104 const bool isChar = ((uc > 96) && (uc < 123));
107 if (!(isDigit || isDash || isChar)) {
114 if (!(isDigit || isDash || isChar)) {
169 if (valid && checkDNS) {
170 QDnsLookup alookup(QDnsLookup::A, v);
172 QObject::connect(&alookup, &QDnsLookup::finished, &aloop, &QEventLoop::quit);
173 QTimer::singleShot(3100, &alookup, &QDnsLookup::abort);
177 if (((alookup.error() != QDnsLookup::NoError) && (alookup.error() != QDnsLookup::OperationCancelledError)) || alookup.hostAddressRecords().empty()) {
178 QDnsLookup aaaaLookup(QDnsLookup::AAAA, v);
180 QObject::connect(&aaaaLookup, &QDnsLookup::finished, &aaaaLoop, &QEventLoop::quit);
181 QTimer::singleShot(3100, &aaaaLookup, &QDnsLookup::abort);
185 if (((aaaaLookup.error() != QDnsLookup::NoError) && (aaaaLookup.error() != QDnsLookup::OperationCancelledError)) || aaaaLookup.hostAddressRecords().empty()) {
188 }
else if (aaaaLookup.error() == QDnsLookup::OperationCancelledError) {
192 }
else if (alookup.error() == QDnsLookup::OperationCancelledError) {
202 if (valid && extractedValue) {
204 *extractedValue = v + QLatin1Char(
'.');
217 if (label.isEmpty()) {
220 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name seems to be valid but could not be found in the domain name system.");
223 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name contains characters that are not allowed.");
226 error = c->
translate(
"Cutelyst::ValidatorDomain",
"At least one of the sections separated by dots exceeds the maximum allowed length of 63 characters. Note that internationalized domain names may be internally longer than they are displayed.");
229 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The full name of the domain must not be longer than 253 characters. Note that internationalized domain names may be internally longer than they are displayed.");
232 error = c->
translate(
"Cutelyst::ValidatorDomain",
"This is not a valid domain name because it has either no parts (is empty) or only has a top level domain.");
235 error = c->
translate(
"Cutelyst::ValidatorDomain",
"At least one of the sections separated by dots is empty. Check whether you have entered two dots consecutively.");
238 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The top level domain (last part) contains characters that are not allowed, like digits and or dashes.");
241 error = c->
translate(
"Cutelyst::ValidatorDomain",
"Domain name sections are not allowed to start with a dash.");
244 error = c->
translate(
"Cutelyst::ValidatorDomain",
"Domain name sections are not allowed to end with a dash.");
247 error = c->
translate(
"Cutelyst::ValidatorDomain",
"Domain name sections are not allowed to start with a digit.");
250 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name is valid.");
253 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The DNS lookup was aborted because it took too long.");
256 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
262 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field seems to be valid but could not be found in the domain name system.").arg(label);
265 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field contains characters that are not allowed.").arg(label);
268 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because at least one of the sections separated by dots exceeds the maximum allowed length of 63 characters. Note that internationalized domain names may be internally longer than they are displayed.").arg(label);
271 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The full name of the domain in the “%1” field must not be longer than 253 characters. Note that internationalized domain names may be internally longer than they are displayed.").arg(label);
274 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The “%1” field does not contain a valid domain name because it has either no parts (is empty) or only has a top level domain.").arg(label);
277 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because at least on of the sections separated by dots is empty. Check whether you have entered two dots consecutively.").arg(label);
280 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The top level domain (last part) of the domain name in the “%1” field contains characters that are not allowed, like digits and or dashes.").arg(label);
283 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because domain name sections are not allowed to start with a dash.").arg(label);
286 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because domain name sections are not allowed to end with a dash.").arg(label);
289 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because domain name sections are not allowed to start with a digit.").arg(label);
292 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1” field is valid.").arg(label);
295 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The DNS lookup for the name in the “%1” field was aborted because it took too long.").arg(label);
298 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
310 const QString &v =
value(params);
317 result.
value.setValue<QString>(exVal);
331 const QString _label =
label(c);
337 #include "moc_validatordomain.cpp" QMap< QString, QString > ParamsMultiMap
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Checks if the value of the input field contains FQDN according to RFC 1035.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
The Cutelyst namespace holds all public Cutelyst API.
static QString diagnoseString(Context *c, Diagnose diagnose, const QString &label=QString())
Returns a human readable description of a Diagnose.
Base class for all validator rules.
Diagnose
Possible diagnose information for the checked domain.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
ValidatorDomain(const QString &field, bool checkDNS=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorDomain with the given parameters.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
static bool validate(const QString &value, bool checkDNS, Diagnose *diagnose=nullptr, QString *extractedValue=nullptr)
Returns true if value is a valid domain name.
~ValidatorDomain()
Deconstructs ValidatorDomain.
Contains the result of a single input parameter validation.
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...