cutelyst 4.8.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorurl.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorurl_p.h"
7
8#include <QUrl>
9
10using namespace Cutelyst;
11
12ValidatorUrl::ValidatorUrl(const QString &field,
13 Constraints constraints,
14 const QStringList &schemes,
15 const Cutelyst::ValidatorMessages &messages,
16 const QString &defValKey)
17 : ValidatorRule(*new ValidatorUrlPrivate(field, constraints, schemes, messages, defValKey))
18{
19}
20
22
24{
26
27 Q_D(const ValidatorUrl);
28
29 const QString v = value(params);
30
31 if (!v.isEmpty()) {
32
33 bool valid = true;
34
35 QUrl::ParsingMode parsingMode = QUrl::TolerantMode;
36 if (d->constraints.testFlag(StrictParsing)) {
37 parsingMode = QUrl::StrictMode;
38 }
39
40 QUrl url(v, parsingMode);
41 if (!url.isValid() || url.isEmpty()) {
42 valid = false;
43 }
44
45 if (valid &&
46 (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) &&
47 url.isRelative()) {
48 valid = false;
49 }
50
51 if (valid &&
52 (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) &&
53 url.isLocalFile()) {
54 valid = false;
55 }
56
57 if (valid) {
58 const QStringList schemeList =
59 d->constraints.testFlag(WebsiteOnly)
60 ? QStringList({QStringLiteral("http"), QStringLiteral("https")})
61 : d->schemes;
62
63 // if (d->constraints.testFlag(WebsiteOnly)) {
64 // if (!schemeList.contains(QStringLiteral("http"), Qt::CaseInsensitive))
65 // {
66 // schemeList.append(QStringLiteral("http"));
67 // }
68 // if (!schemeList.contains(QStringLiteral("https"),
69 // Qt::CaseInsensitive)) {
70 // schemeList.append(QStringLiteral("https"));
71 // }
72 // }
73
74 if (!schemeList.empty()) {
75
76 // const QStringList sc = schemeList;
77 bool foundScheme = false;
78 for (const QString &s : schemeList) {
79 const QString sl = s.toLower();
80 if (url.scheme() == sl) {
81 foundScheme = true;
82 break;
83 }
84 }
85
86 if (!foundScheme) {
87 valid = false;
88 }
89 }
90 }
91
92 if (!valid) {
93 result.errorMessage = validationError(c);
94 qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Not a valid URL";
95 } else {
96 result.value.setValue(url);
97 }
98 } else {
99 defaultValue(c, &result);
100 }
101
102 return result;
103}
104
105QString ValidatorUrl::genericValidationError(Context *c, const QVariant &errorData) const
106{
107 Q_UNUSED(errorData)
108 const QString _label = label(c);
109 if (_label.isEmpty()) {
110 //% "Not a valid URL."
111 return c->qtTrId("cutelyst-valurl-genvalerr");
112 } else {
113 //: %1 will be replaced by the field label
114 //% "The text in the “%1” field is not a valid URL."
115 return c->qtTrId("cutelyst-valurl-genvalerr-label").arg(_label);
116 }
117}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
The field under validation must be a valid URL.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorUrl(const QString &field, Constraints constraints=NoConstraint, const QStringList &schemes=QStringList(), const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QMultiMap< QString, QString > ParamsMultiMap
The Cutelyst namespace holds all public Cutelyst API.
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.