6#include "validatordomain_p.h"
19 const QString &defValKey)
20 :
ValidatorRule(*new ValidatorDomainPrivate(field, checkDNS, messages, defValKey))
29 QString *extractedValue)
36 bool hasRootDot =
false;
37 if (_v.endsWith(u
'.')) {
43 const QString v = QString::fromLatin1(QUrl::toAce(_v)).toLower();
46 const QStringList nonAceParts = _v.split(QLatin1Char(
'.'));
47 if (!nonAceParts.empty()) {
48 const QString tld = nonAceParts.last();
54 for (
const QChar &ch : tld) {
55 const ushort &uc = ch.unicode();
56 if (((uc >= ValidatorRulePrivate::ascii_0) &&
57 (uc <= ValidatorRulePrivate::ascii_9)) ||
58 (uc == ValidatorRulePrivate::ascii_dash)) {
68 if (v.length() <= ValidatorDomainPrivate::maxDnsNameWithLastDot) {
69 const QStringList parts = v.split(QLatin1Char(
'.'), Qt::KeepEmptyParts);
71 if (parts.size() > 1) {
73 if (parts.last().length() > 1) {
74 for (
int i = 0; i < parts.size(); ++i) {
76 const QString part = parts.at(i);
77 if (!part.isEmpty()) {
80 ValidatorDomainPrivate::maxDnsLabelLength) {
81 bool isTld = (i == (parts.size() - 1));
82 bool isPunyCode = part.startsWith(u
"xn--");
83 for (
int j = 0; j < part.size(); ++j) {
84 const ushort &uc = part.at(j).unicode();
86 ((uc >= ValidatorRulePrivate::ascii_0) &&
87 (uc <= ValidatorRulePrivate::ascii_9));
89 (uc == ValidatorRulePrivate::ascii_dash);
92 if ((j == 0) && (isDash || isDigit)) {
98 if ((j == (part.size() - 1)) && isDash) {
104 ((uc >= ValidatorRulePrivate::ascii_a) &&
105 (uc <= ValidatorRulePrivate::ascii_z));
109 if (!(isDigit || isDash || isChar)) {
116 if (!(isDigit || isDash || isChar)) {
170 if (valid && checkDNS) {
171 QDnsLookup alookup(QDnsLookup::A, v);
173 QObject::connect(&alookup, &QDnsLookup::finished, &aloop, &QEventLoop::quit);
174 QTimer::singleShot(ValidatorDomainPrivate::dnsLookupTimeout, &alookup, &QDnsLookup::abort);
178 if (((alookup.error() != QDnsLookup::NoError) &&
179 (alookup.error() != QDnsLookup::OperationCancelledError)) ||
180 alookup.hostAddressRecords().empty()) {
181 QDnsLookup aaaaLookup(QDnsLookup::AAAA, v);
183 QObject::connect(&aaaaLookup, &QDnsLookup::finished, &aaaaLoop, &QEventLoop::quit);
185 ValidatorDomainPrivate::dnsLookupTimeout, &aaaaLookup, &QDnsLookup::abort);
189 if (((aaaaLookup.error() != QDnsLookup::NoError) &&
190 (aaaaLookup.error() != QDnsLookup::OperationCancelledError)) ||
191 aaaaLookup.hostAddressRecords().empty()) {
194 }
else if (aaaaLookup.error() == QDnsLookup::OperationCancelledError) {
198 }
else if (alookup.error() == QDnsLookup::OperationCancelledError) {
208 if (valid && extractedValue) {
210 *extractedValue = v + QLatin1Char(
'.');
223 if (
label.isEmpty()) {
226 error = c->
translate(
"Cutelyst::ValidatorDomain",
227 "The domain name seems to be valid but could not be found in the "
228 "domain name system.");
231 error = c->
translate(
"Cutelyst::ValidatorDomain",
232 "The domain name contains characters that are not allowed.");
236 c->
translate(
"Cutelyst::ValidatorDomain",
237 "At least one of the sections separated by dots exceeds the maximum "
238 "allowed length of 63 characters. Note that internationalized domain "
239 "names can be longer internally than they are displayed.");
243 "Cutelyst::ValidatorDomain",
244 "The full name of the domain must not be longer than 253 characters. Note that "
245 "internationalized domain names can be longer internally than they are displayed.");
248 error = c->
translate(
"Cutelyst::ValidatorDomain",
249 "This is not a valid domain name because it has either no parts "
250 "(is empty) or only has a top level domain.");
253 error = c->
translate(
"Cutelyst::ValidatorDomain",
254 "At least one of the sections separated by dots is empty. Check "
255 "whether you have entered two dots consecutively.");
258 error = c->
translate(
"Cutelyst::ValidatorDomain",
259 "The top level domain (last part) contains characters that are "
260 "not allowed, like digits and/or dashes.");
263 error = c->
translate(
"Cutelyst::ValidatorDomain",
264 "Domain name sections are not allowed to start with a dash.");
267 error = c->
translate(
"Cutelyst::ValidatorDomain",
268 "Domain name sections are not allowed to end with a dash.");
271 error = c->
translate(
"Cutelyst::ValidatorDomain",
272 "Domain name sections are not allowed to start with a digit.");
275 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name is valid.");
278 error = c->
translate(
"Cutelyst::ValidatorDomain",
279 "The DNS lookup was aborted because it took too long.");
282 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
288 error = c->
translate(
"Cutelyst::ValidatorDomain",
289 "The domain name in the “%1“ field seems to be valid but could "
290 "not be found in the domain name system.")
296 "Cutelyst::ValidatorDomain",
297 "The domain name in the “%1“ field contains characters that are not allowed.")
301 error = c->
translate(
"Cutelyst::ValidatorDomain",
302 "The domain name in the “%1“ field is not valid because at least "
303 "one of the sections separated by dots exceeds the maximum "
304 "allowed length of 63 characters. Note that internationalized "
305 "domain names can be longer internally than they are displayed.")
309 error = c->
translate(
"Cutelyst::ValidatorDomain",
310 "The full name of the domain in the “%1” field must not be longer "
311 "than 253 characters. Note that internationalized domain names "
312 "can be longer internally than they are displayed.")
316 error = c->
translate(
"Cutelyst::ValidatorDomain",
317 "The “%1” field does not contain a valid domain name because it "
318 "has either no parts (is empty) or only has a top level domain.")
322 error = c->
translate(
"Cutelyst::ValidatorDomain",
323 "The domain name in the “%1“ field is not valid because at least "
324 "one of the sections separated by dots is empty. Check whether "
325 "you have entered two dots consecutively.")
330 "Cutelyst::ValidatorDomain",
331 "The top level domain (last part) of the domain name in the “%1” field "
332 "contains characters that are not allowed, like digits and or dashes.")
336 error = c->
translate(
"Cutelyst::ValidatorDomain",
337 "The domain name in the “%1“ field is not valid because domain "
338 "name sections are not allowed to start with a dash.")
342 error = c->
translate(
"Cutelyst::ValidatorDomain",
343 "The domain name in the “%1“ field is not valid because domain "
344 "name sections are not allowed to end with a dash.")
348 error = c->
translate(
"Cutelyst::ValidatorDomain",
349 "The domain name in the “%1“ field is not valid because domain "
350 "name sections are not allowed to start with a digit.")
354 error = c->
translate(
"Cutelyst::ValidatorDomain",
355 "The domain name in the “%1” field is valid.")
359 error = c->
translate(
"Cutelyst::ValidatorDomain",
360 "The DNS lookup for the domain name in the “%1” field was aborted "
361 "because it took too long.")
365 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
377 const QString &v =
value(params);
384 result.
value.setValue(exVal);
387 if (C_VALIDATOR().isDebugEnabled()) {
392 qCDebug(C_VALIDATOR).noquote()
393 <<
debugString(c) <<
"Can not find valid DNS entry for" << v;
396 qCDebug(C_VALIDATOR).noquote()
398 <<
"The domain name contains characters that are not allowed";
401 qCDebug(C_VALIDATOR).noquote()
403 <<
"At least on of the domain name labels exceeds the maximum"
404 <<
"size of" << ValidatorDomainPrivate::maxDnsLabelLength <<
"characters";
407 qCDebug(C_VALIDATOR).noquote()
408 <<
debugString(c) <<
"The domain name exceeds the maximum size of"
409 << ValidatorDomainPrivate::maxDnsNameWithLastDot <<
"characters";
412 qCDebug(C_VALIDATOR).noquote()
413 <<
debugString(c) <<
"Invalid label count. Either no labels or only TLD";
416 qCDebug(C_VALIDATOR).noquote()
417 <<
debugString(c) <<
"At least one of the domain name labels is empty";
420 qCDebug(C_VALIDATOR).noquote()
422 <<
"The TLD label contains characters that are not allowed";
425 qCDebug(C_VALIDATOR).noquote()
426 <<
debugString(c) <<
"At least one label starts with a dash";
429 qCDebug(C_VALIDATOR).noquote()
430 <<
debugString(c) <<
"At least one label ends with a dash";
433 qCDebug(C_VALIDATOR).noquote()
434 <<
debugString(c) <<
"At least one label start with a digit";
437 qCDebug(C_VALIDATOR).noquote()
438 <<
debugString(c) <<
"The DNS lookup exceeds the timeout of"
439#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
440 << ValidatorDomainPrivate::dnsLookupTimeout;
442 << ValidatorDomainPrivate::dnsLookupTimeout.count() <<
"milliseconds";
459#include "moc_validatordomain.cpp"
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Checks if the value of the input field contains FQDN according to RFC 1035.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
static QString diagnoseString(Context *c, Diagnose diagnose, const QString &label=QString())
Returns a human readable description of a Diagnose.
ValidatorDomain(const QString &field, bool checkDNS=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorDomain with the given parameters.
~ValidatorDomain() override
Deconstructs ValidatorDomain.
Diagnose
Possible diagnose information for the checked domain.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void defaultValue(Context *c, ValidatorReturnType *result) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
static bool validate(const QString &value, bool checkDNS, Diagnose *diagnose=nullptr, QString *extractedValue=nullptr)
Returns true if value is a valid domain name.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.