cutelyst  3.7.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
cookie.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2023 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "cookie_p.h"
7 #include <QLocale>
8 #include <QUrl>
9 #include <QHostAddress>
10 #include <QDateTime>
11 
12 using namespace Cutelyst;
13 
14 Cookie::Cookie(const QByteArray &name, const QByteArray &value)
15  : QNetworkCookie(name, value), d(new CookiePrivate)
16 {
17  qRegisterMetaType<Cookie>();
18  qRegisterMetaType<QList<Cookie>>();
19 }
20 
21 Cookie::Cookie(const Cookie &other)
22  : QNetworkCookie(other), d(other.d)
23 {
24 
25 }
26 
27 Cookie::~Cookie() = default;
28 
30 {
31  QNetworkCookie::operator=(other);
32  d = other.d;
33  return *this;
34 }
35 
36 bool Cookie::operator==(const Cookie &other) const
37 {
38  if (QNetworkCookie::operator==(other) && d == other.d) {
39  return true;
40  }
41 
42  return QNetworkCookie::operator==(other) && d->sameSite == other.d->sameSite;
43 }
44 
46 {
47  return d->sameSite;
48 }
49 
51 {
52  d->sameSite = sameSite;
53 }
54 
55 namespace {
56 QByteArray sameSiteToRawString(Cookie::SameSite samesite)
57 {
58  switch (samesite) {
60  return QByteArrayLiteral("None");
62  return QByteArrayLiteral("Lax");
64  return QByteArrayLiteral("Strict");
66  break;
67  }
68  return QByteArray();
69 }
70 } // namespace
71 
72 QByteArray Cookie::toRawForm(RawForm form) const
73 {
74  QByteArray result;
75  if (name().isEmpty())
76  return result; // not a valid cookie
77 
78  result = name();
79  result += '=';
80  result += value();
81 
82  if (form == Full) {
83  // same as above, but encoding everything back
84  if (isSecure())
85  result += "; secure";
86  if (isHttpOnly())
87  result += "; HttpOnly";
88  if (d->sameSite != SameSite::Default) {
89  result += "; SameSite=";
90  result += sameSiteToRawString(d->sameSite);
91  }
92  if (!isSessionCookie()) {
93  result += "; expires=";
94  result += QLocale::c().toString(expirationDate().toUTC(),
95  QStringLiteral("ddd, dd-MMM-yyyy hh:mm:ss 'GMT")).toLatin1();
96  }
97  if (!domain().isEmpty()) {
98  result += "; domain=";
99  if (domain().startsWith(u'.')) {
100  result += '.';
101  result += QUrl::toAce(domain().mid(1));
102  } else {
103  QHostAddress hostAddr(domain());
104  if (hostAddr.protocol() == QAbstractSocket::IPv6Protocol) {
105  result += '[';
106  result += domain().toUtf8();
107  result += ']';
108  } else {
109  result += QUrl::toAce(domain());
110  }
111  }
112  }
113  if (!path().isEmpty()) {
114  result += "; path=";
115  result += path().toUtf8();
116  }
117  }
118  return result;
119 }
120 
121 #ifndef QT_NO_DEBUG_STREAM
122 QDebug operator<<(QDebug s, const Cutelyst::Cookie &cookie)
123 {
124  QDebugStateSaver saver(s);
125  Q_UNUSED(saver)
126  s.resetFormat().nospace();
127  s << "Cutelyst::Cookie(" << cookie.toRawForm(Cookie::Full) << ')';
128  return s;
129 }
130 #endif
131 
132 #include "moc_cookie.cpp"
The Cutelyst Cookie.
Definition: cookie.h:29
SameSite sameSitePolicy() const
Returns the "SameSite" option if specified in the cookie string, SameSite::Default if not present.
Definition: cookie.cpp:45
Cookie & operator=(Cookie &&other) noexcept
Move assigns the contents of the Cookie object other to this object.
Definition: cookie.h:57
Cookie(const QByteArray &name=QByteArray(), const QByteArray &value=QByteArray())
Create a new Cookie object, initializing the cookie name to name and its value to value.
Definition: cookie.cpp:14
void setSameSitePolicy(SameSite sameSite)
Sets the "SameSite" option of this cookie to sameSite.
Definition: cookie.cpp:50
bool operator==(const Cookie &other) const
Returns true if this cookie is equal to other.
Definition: cookie.cpp:36
~Cookie()
Destroys this Cookie object.
QByteArray toRawForm(RawForm form=Full) const
Returns the raw form of this Cookie.
Definition: cookie.cpp:72
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8