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