6#include "validatordomain_p.h"
19 const QString &defValKey)
20 :
ValidatorRule(*new ValidatorDomainPrivate(field, checkDNS, messages, defValKey))
31 QString *extractedValue)
38 bool hasRootDot =
false;
39 if (_v.endsWith(u
'.')) {
45 const QString v = QString::fromLatin1(QUrl::toAce(_v)).toLower();
48 const QStringList nonAceParts = _v.split(QLatin1Char(
'.'));
49 if (!nonAceParts.empty()) {
50 const QString tld = nonAceParts.last();
56 for (
const QChar &ch : tld) {
57 const ushort &uc = ch.unicode();
58 if (((uc > 47) && (uc < 58)) || (uc == 45)) {
68 if (v.length() < 254) {
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()) {
79 if (part.length() < 64) {
80 bool isTld = (i == (parts.size() - 1));
81 bool isPunyCode = part.startsWith(u
"xn--");
82 for (
int j = 0; j < part.size(); ++j) {
83 const ushort &uc = part.at(j).unicode();
84 const bool isDigit = ((uc > 47) && (uc < 58));
85 const bool isDash = (uc == 45);
88 if ((j == 0) && (isDash || isDigit)) {
94 if ((j == (part.size() - 1)) && isDash) {
99 const bool isChar = ((uc > 96) && (uc < 123));
103 if (!(isDigit || isDash || isChar)) {
110 if (!(isDigit || isDash || isChar)) {
164 if (valid && checkDNS) {
165 QDnsLookup alookup(QDnsLookup::A, v);
167 QObject::connect(&alookup, &QDnsLookup::finished, &aloop, &QEventLoop::quit);
168 QTimer::singleShot(std::chrono::milliseconds{3100}, &alookup, &QDnsLookup::abort);
172 if (((alookup.error() != QDnsLookup::NoError) &&
173 (alookup.error() != QDnsLookup::OperationCancelledError)) ||
174 alookup.hostAddressRecords().empty()) {
175 QDnsLookup aaaaLookup(QDnsLookup::AAAA, v);
177 QObject::connect(&aaaaLookup, &QDnsLookup::finished, &aaaaLoop, &QEventLoop::quit);
178 QTimer::singleShot(std::chrono::milliseconds{3100}, &aaaaLookup, &QDnsLookup::abort);
182 if (((aaaaLookup.error() != QDnsLookup::NoError) &&
183 (aaaaLookup.error() != QDnsLookup::OperationCancelledError)) ||
184 aaaaLookup.hostAddressRecords().empty()) {
187 }
else if (aaaaLookup.error() == QDnsLookup::OperationCancelledError) {
191 }
else if (alookup.error() == QDnsLookup::OperationCancelledError) {
201 if (valid && extractedValue) {
203 *extractedValue = v + QLatin1Char(
'.');
216 if (
label.isEmpty()) {
219 error = c->
translate(
"Cutelyst::ValidatorDomain",
220 "The domain name seems to be valid but could not be found in the "
221 "domain name system.");
224 error = c->
translate(
"Cutelyst::ValidatorDomain",
225 "The domain name contains characters that are not allowed.");
229 c->
translate(
"Cutelyst::ValidatorDomain",
230 "At least one of the sections separated by dots exceeds the maximum "
231 "allowed length of 63 characters. Note that internationalized domain "
232 "names can be longer internally than they are displayed.");
236 "Cutelyst::ValidatorDomain",
237 "The full name of the domain must not be longer than 253 characters. Note that "
238 "internationalized domain names can be longer internally than they are displayed.");
241 error = c->
translate(
"Cutelyst::ValidatorDomain",
242 "This is not a valid domain name because it has either no parts "
243 "(is empty) or only has a top level domain.");
246 error = c->
translate(
"Cutelyst::ValidatorDomain",
247 "At least one of the sections separated by dots is empty. Check "
248 "whether you have entered two dots consecutively.");
251 error = c->
translate(
"Cutelyst::ValidatorDomain",
252 "The top level domain (last part) contains characters that are "
253 "not allowed, like digits and/or dashes.");
256 error = c->
translate(
"Cutelyst::ValidatorDomain",
257 "Domain name sections are not allowed to start with a dash.");
260 error = c->
translate(
"Cutelyst::ValidatorDomain",
261 "Domain name sections are not allowed to end with a dash.");
264 error = c->
translate(
"Cutelyst::ValidatorDomain",
265 "Domain name sections are not allowed to start with a digit.");
268 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name is valid.");
271 error = c->
translate(
"Cutelyst::ValidatorDomain",
272 "The DNS lookup was aborted because it took too long.");
275 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
281 error = c->
translate(
"Cutelyst::ValidatorDomain",
282 "The domain name in the “%1“ field seems to be valid but could "
283 "not be found in the domain name system.")
289 "Cutelyst::ValidatorDomain",
290 "The domain name in the “%1“ field contains characters that are not allowed.")
294 error = c->
translate(
"Cutelyst::ValidatorDomain",
295 "The domain name in the “%1“ field is not valid because at least "
296 "one of the sections separated by dots exceeds the maximum "
297 "allowed length of 63 characters. Note that internationalized "
298 "domain names can be longer internally than they are displayed.")
302 error = c->
translate(
"Cutelyst::ValidatorDomain",
303 "The full name of the domain in the “%1” field must not be longer "
304 "than 253 characters. Note that internationalized domain names "
305 "can be longer internally than they are displayed.")
309 error = c->
translate(
"Cutelyst::ValidatorDomain",
310 "The “%1” field does not contain a valid domain name because it "
311 "has either no parts (is empty) or only has a top level domain.")
315 error = c->
translate(
"Cutelyst::ValidatorDomain",
316 "The domain name in the “%1“ field is not valid because at least "
317 "one of the sections separated by dots is empty. Check whether "
318 "you have entered two dots consecutively.")
323 "Cutelyst::ValidatorDomain",
324 "The top level domain (last part) of the domain name in the “%1” field "
325 "contains characters that are not allowed, like digits and or dashes.")
329 error = c->
translate(
"Cutelyst::ValidatorDomain",
330 "The domain name in the “%1“ field is not valid because domain "
331 "name sections are not allowed to start with a dash.")
335 error = c->
translate(
"Cutelyst::ValidatorDomain",
336 "The domain name in the “%1“ field is not valid because domain "
337 "name sections are not allowed to end with a dash.")
341 error = c->
translate(
"Cutelyst::ValidatorDomain",
342 "The domain name in the “%1“ field is not valid because domain "
343 "name sections are not allowed to start with a digit.")
347 error = c->
translate(
"Cutelyst::ValidatorDomain",
348 "The domain name in the “%1” field is valid.")
352 error = c->
translate(
"Cutelyst::ValidatorDomain",
353 "The DNS lookup for the domain name in the “%1” field was aborted "
354 "because it took too long.")
358 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
370 const QString &v =
value(params);
377 result.
value.setValue(exVal);
391 const QString _label =
label(c);
397#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 char *validatorName) 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.
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.