cutelyst  3.9.1
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-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorurl_p.h"
7 
8 #include <QUrl>
9 
10 using namespace Cutelyst;
11 
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 {
23 }
24 
26 {
27  ValidatorReturnType result;
28 
29  Q_D(const ValidatorUrl);
30 
31  const QString v = value(params);
32 
33  if (!v.isEmpty()) {
34 
35  bool valid = true;
36 
38  if (d->constraints.testFlag(StrictParsing)) {
39  parsingMode = QUrl::StrictMode;
40  }
41 
42  QUrl url(v, parsingMode);
43  if (!url.isValid() || url.isEmpty()) {
44  valid = false;
45  }
46 
47  if (valid &&
48  (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) &&
49  url.isRelative()) {
50  valid = false;
51  }
52 
53  if (valid &&
54  (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) &&
55  url.isLocalFile()) {
56  valid = false;
57  }
58 
59  if (valid) {
60  const QStringList schemeList =
61  d->constraints.testFlag(WebsiteOnly)
62  ? QStringList({QStringLiteral("http"), QStringLiteral("https")})
63  : d->schemes;
64 
65  // if (d->constraints.testFlag(WebsiteOnly)) {
66  // if (!schemeList.contains(QStringLiteral("http"), Qt::CaseInsensitive))
67  // {
68  // schemeList.append(QStringLiteral("http"));
69  // }
70  // if (!schemeList.contains(QStringLiteral("https"),
71  // Qt::CaseInsensitive)) {
72  // schemeList.append(QStringLiteral("https"));
73  // }
74  // }
75 
76  if (!schemeList.empty()) {
77 
78  // const QStringList sc = schemeList;
79  bool foundScheme = false;
80  for (const QString &s : schemeList) {
81  const QString sl = s.toLower();
82  if (url.scheme() == sl) {
83  foundScheme = true;
84  break;
85  }
86  }
87 
88  if (!foundScheme) {
89  valid = false;
90  }
91  }
92  }
93 
94  if (!valid) {
95  result.errorMessage = validationError(c);
96  qCDebug(C_VALIDATOR,
97  "ValidatorUrl: Validation failed for field %s at %s::%s: not a valid URL",
98  qPrintable(field()),
99  qPrintable(c->controllerName()),
100  qPrintable(c->actionName()));
101  } else {
102  result.value.setValue(url);
103  }
104  } else {
105  defaultValue(c, &result, "ValidatorUrl");
106  }
107 
108  return result;
109 }
110 
112 {
113  QString error;
114  Q_UNUSED(errorData)
115  const QString _label = label(c);
116  if (_label.isEmpty()) {
117  error = c->translate("Cutelyst::ValidatorUrl", "Not a valid URL.");
118  } else {
119  //: %1 will be replaced by the field label
120  error = c->translate("Cutelyst::ValidatorUrl",
121  "The value in the “%1” field is not a valid URL.")
122  .arg(_label);
123  }
124  return error;
125 }
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Stores custom error messages and the input field label.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorUrl(const QString &field, Constraints constraints=NoConstraint, const QStringList &schemes=QStringList(), const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new url validator.
~ValidatorUrl() override
Deconstructs the validator.
bool isEmpty() const const
The Cutelyst Context.
Definition: context.h:38
bool empty() const const
bool isEmpty() const const
The field under validation must be a valid URL.
Definition: validatorurl.h:32
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString scheme() const const
QString toLower() const const
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
bool isValid() const const
bool isRelative() const const
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(Args &&... args) const const
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 ...
void setValue(QVariant &&value)
bool isLocalFile() const const