6#include "validatordomain_p.h"
10#include <QHostAddress>
21 :
ValidatorRule(*new ValidatorDomainPrivate(field, options, messages, defValKey))
41 if (!nonAceParts.
empty()) {
48 for (
const QChar &ch : tld) {
49 const char16_t uc = ch.unicode();
50 if (((uc >= ValidatorRulePrivate::ascii_0) &&
51 (uc <= ValidatorRulePrivate::ascii_9)) ||
52 (uc == ValidatorRulePrivate::ascii_dash)) {
61 if (ace.
length() <= ValidatorDomainPrivate::maxDnsNameWithLastDot) {
64 if (parts.
size() > 1) {
66 if (parts.
last().length() > 1) {
67 for (
int i = 0; i < parts.
size(); ++i) {
81 if (part.
length() > ValidatorDomainPrivate::maxDnsLabelLength) {
86 const bool isTld = (i == (parts.
size() - 1));
87 const bool isPunyCode = part.
startsWith(u
"xn--");
88 const qsizetype partEnd = part.
size() - 1;
90 for (
int j = 0; j < part.
size(); ++j) {
93 ((uc >= ValidatorRulePrivate::ascii_0) &&
94 (uc <= ValidatorRulePrivate::ascii_9));
96 (uc == ValidatorRulePrivate::ascii_dash);
99 if (j == 0 && (isDash || isDigit)) {
104 if (j == partEnd && isDash) {
109 ((uc >= ValidatorRulePrivate::ascii_a) &&
110 (uc <= ValidatorRulePrivate::ascii_z));
114 if (!(isDigit || isDash || isChar)) {
120 if (!(isDigit || isDash || isChar)) {
157 if (diag ==
Valid && extractedValue) {
161 *extractedValue = ace;
165 return diag ==
Valid;
171 std::function<
void(
Diagnose diagnose,
const QString &extractedValue)> cb)
182 if (!options.testAnyFlag(
CheckDNS)) {
192 if (dns->hostAddressRecords().empty()) {
193 cb(MissingDNS, extracted);
195 if (!options.testFlag(CheckAAAARecord)) {
196 cb(Valid, extracted);
199 auto dns2 = new QDnsLookup{QDnsLookup::AAAA, extracted};
203 if (dns2->hostAddressRecords().empty()) {
204 cb(MissingDNS, extracted);
206 cb(Valid, extracted);
209 cb(DNSTimeout, extracted);
211 cb(DNSError, extracted);
221 cb(DNSTimeout, extracted);
223 cb(DNSError, extracted);
230 }
else if (options.testFlag(CheckAAAARecord)) {
235 if (dns2->hostAddressRecords().empty()) {
236 cb(MissingDNS, extracted);
238 cb(Valid, extracted);
241 cb(DNSTimeout, extracted);
243 cb(DNSError, extracted);
259 return c->
qtTrId(
"cutelyst-valdomain-diag-missingdns");
262 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidchars");
268 return c->
qtTrId(
"cutelyst-valdomain-diag-labeltoolong");
273 return c->
qtTrId(
"cutelyst-valdomain-diag-toolong");
277 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidlabelcount");
281 return c->
qtTrId(
"cutelyst-valdomain-diag-emptylabel");
285 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidtld");
288 return c->
qtTrId(
"cutelyst-valdomain-diag-dashstart");
291 return c->
qtTrId(
"cutelyst-valdomain-diag-dashend");
294 return c->
qtTrId(
"cutelyst-valdomain-diag-digitstart");
297 return c->
qtTrId(
"cutelyst-valdomain-diag-valid");
300 return c->
qtTrId(
"cutelyst-valdomain-diag-dnstimeout");
303 return c->
qtTrId(
"cutelyst-valdomain-diag-dnserror");
305 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
313 return c->
qtTrId(
"cutelyst-valdomain-diag-missingdns-label").
arg(
label);
316 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidchars-label").
arg(
label);
323 return c->
qtTrId(
"cutelyst-valdomain-diag-labeltoolong-label").
arg(
label);
328 return c->
qtTrId(
"cutelyst-valdomain-diag-toolong-label").
arg(
label);
332 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidlabelcount-label").
arg(
label);
337 return c->
qtTrId(
"cutelyst-valdomain-diag-emptylabel-label").
arg(
label);
341 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidtld-label").
arg(
label);
345 return c->
qtTrId(
"cutelyst-valdomain-diag-dashstart-label").
arg(
label);
349 return c->
qtTrId(
"cutelyst-valdomain-diag-dashend-label").
arg(
label);
353 return c->
qtTrId(
"cutelyst-valdomain-diag-digitstart-label").
arg(
label);
356 return c->
qtTrId(
"cutelyst-valdomain-diag-valid-label").
arg(
label);
360 return c->
qtTrId(
"cutelyst-valdomain-diag-dnstimeout-label").
arg(
label);
364 return c->
qtTrId(
"cutelyst-valdomain-diag-dnserror-label");
366 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
378 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"Can not find valid DNS entry for" << v;
381 qCDebug(C_VALIDATOR).noquote()
382 << valInfo <<
"The domain name contains characters that are not allowed";
385 qCDebug(C_VALIDATOR).noquote()
386 << valInfo <<
"At least on of the domain name labels exceeds the maximum" <<
"size of"
387 << ValidatorDomainPrivate::maxDnsLabelLength <<
"characters";
390 qCDebug(C_VALIDATOR).noquote()
391 << valInfo <<
"The domain name exceeds the maximum size of"
392 << ValidatorDomainPrivate::maxDnsNameWithLastDot <<
"characters";
395 qCDebug(C_VALIDATOR).noquote()
396 << valInfo <<
"Invalid label count. Either no labels or only TLD";
399 qCDebug(C_VALIDATOR).noquote()
400 << valInfo <<
"At least one of the domain name labels is empty";
403 qCDebug(C_VALIDATOR).noquote()
404 << valInfo <<
"The TLD label contains characters that are not allowed";
407 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"At least one label starts with a dash";
410 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"At least one label ends with a dash";
413 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"At least one label starts with a digit";
416 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"The DNS lookup exceeds the timeout of"
417#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
418 << ValidatorDomainPrivate::dnsLookupTimeout;
420 << ValidatorDomainPrivate::dnsLookupTimeout.
count()
424 qCDebug(C_VALIDATOR).noquote()
425 << valInfo <<
"The DNS lookup failed because of errors in the"
444 if (C_VALIDATOR().isDebugEnabled()) {
462 v, d->options, [cb,
this, c, v](
Diagnose diagnose,
const QString &extractedValue) {
463 if (diagnose == Valid) {
464 cb({.errorMessage = {}, .value = extractedValue});
466 if (C_VALIDATOR().isDebugEnabled()) {
467 writeDebugString(debugString(c), diagnose, v);
482#include "moc_validatordomain.cpp"
QString qtTrId(const char *id, int n=-1) const
Checks if the value of the input field contains a 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={})
ValidatorDomain(const QString &field, Options options=NoOption, const ValidatorMessages &messages={}, const QString &defValKey={})
Constructs a new ValidatorDomain object with the given parameters.
~ValidatorDomain() override
Diagnose
Possible diagnose information for the checked domain.
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap ¶ms) const
static void validateCb(const QString &value, Options options, std::function< void(Diagnose diagnose, const QString &extractedValue)> cb)
Checks if value is a vaid fully qualified domain name and writes the result to the callback cb.
static bool validate(const QString &value, Diagnose *diagnose=nullptr, QString *extractedValue=nullptr)
Returns true if value is a valid fully qualified domain name.
The Cutelyst namespace holds all public Cutelyst API.
QList::const_reference at(qsizetype i) const const
qsizetype size() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
qsizetype count() const const
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
QString chopped(qsizetype len) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
qsizetype length() const const
qsizetype size() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toLower() const const
QByteArray toAce(const QString &domain, QUrl::AceProcessingOptions options)
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.