cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
csrf.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "csrf.h"
6 
7 #include <Cutelyst/Context>
8 #include <Cutelyst/ParamsMultiMap>
9 #include <Cutelyst/Request>
10 #include <Cutelyst/Response>
11 #include <grantlee/exception.h>
12 #include <grantlee/parser.h>
13 
14 #ifdef PLUGIN_CSRFPROTECTION_ENABLED
15 # include <Cutelyst/Plugins/CSRFProtection/CSRFProtection>
16 #endif
17 
18 Grantlee::Node *CSRFTag::getNode(const QString &tagContent, Grantlee::Parser *p) const
19 {
20  Q_UNUSED(tagContent);
21  return new CSRF(p);
22 }
23 
24 CSRF::CSRF(Grantlee::Parser *parser)
25  : Grantlee::Node(parser)
26 {
27 }
28 
29 void CSRF::render(Grantlee::OutputStream *stream, Grantlee::Context *gc) const
30 {
31 #ifdef PLUGIN_CSRFPROTECTION_ENABLED
32  // In case cutelyst context is not set as "c"
33  auto c = gc->lookup(m_cutelystContext).value<Cutelyst::Context *>();
34  if (!c) {
35  const QVariantHash hash = gc->stackHash(0);
36  auto it = hash.constBegin();
37  while (it != hash.constEnd()) {
38  if (it.value().userType() == qMetaTypeId<Cutelyst::Context *>()) {
39  c = it.value().value<Cutelyst::Context *>();
40  if (c) {
41  m_cutelystContext = it.key();
42  break;
43  }
44  }
45  ++it;
46  }
47 
48  if (!c) {
49  return;
50  }
51  }
52 
54 #else
55  Q_UNUSED(stream)
56  Q_UNUSED(gc)
57  qWarning("%s", "The CSRF protection plugin has not been built.");
58 #endif
59 }
60 
61 #include "moc_csrf.cpp"
The Cutelyst Context.
Definition: context.h:38
static QString getTokenFormField(Context *c)