Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qnetworkcookiejar.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qnetworkcookiejar.h"
6
7#include "QtNetwork/qnetworkcookie.h"
8#include "QtCore/qurl.h"
9#include "QtCore/qdatetime.h"
10#if QT_CONFIG(topleveldomain)
11#include "private/qtldurl_p.h"
12#else
14static bool qIsEffectiveTLD(QStringView domain)
15{
16 // provide minimal checking by not accepting cookies on real TLDs
17 return !domain.contains(u'.');
18}
20#endif
21
23
24using namespace Qt::StringLiterals;
25
73{
74}
75
85{
86}
87
96{
97 return d_func()->allCookies;
98}
99
110{
112 d->allCookies = cookieList;
113}
114
115static inline bool isParentPath(const QString &path, const QString &reference)
116{
117 if ((path.isEmpty() && reference == "/"_L1) || path.startsWith(reference)) {
118 //The cookie-path and the request-path are identical.
119 if (path.size() == reference.size())
120 return true;
121 //The cookie-path is a prefix of the request-path, and the last
122 //character of the cookie-path is %x2F ("/").
123 if (reference.endsWith(u'/'))
124 return true;
125 //The cookie-path is a prefix of the request-path, and the first
126 //character of the request-path that is not included in the cookie-
127 //path is a %x2F ("/") character.
128 if (path.at(reference.size()) == u'/')
129 return true;
130 }
131 return false;
132}
133
134static inline bool isParentDomain(const QString &domain, const QString &reference)
135{
136 if (!reference.startsWith(u'.'))
137 return domain == reference;
138
139 return domain.endsWith(reference) || domain == QStringView{reference}.mid(1);
140}
141
164 const QUrl &url)
165{
166 bool added = false;
167 for (QNetworkCookie cookie : cookieList) {
168 cookie.normalize(url);
169 if (validateCookie(cookie, url) && insertCookie(cookie))
170 added = true;
171 }
172 return added;
173}
174
194{
195// \b Warning! This is only a dumb implementation!
196// It does NOT follow all of the recommendations from
197// http://wp.netscape.com/newsref/std/cookie_spec.html
198// It does not implement a very good cross-domain verification yet.
199
200 Q_D(const QNetworkCookieJar);
203 bool isEncrypted = url.scheme() == "https"_L1;
204
205 // scan our cookies for something that matches
206 QList<QNetworkCookie>::ConstIterator it = d->allCookies.constBegin(),
207 end = d->allCookies.constEnd();
208 for ( ; it != end; ++it) {
209 if (!isParentDomain(url.host(), it->domain()))
210 continue;
211 if (!isParentPath(url.path(), it->path()))
212 continue;
213 if (!(*it).isSessionCookie() && (*it).expirationDate() < now)
214 continue;
215 if ((*it).isSecure() && !isEncrypted)
216 continue;
217
218 QString domain = it->domain();
219 if (domain.startsWith(u'.'))
220 domain = domain.mid(1);
221#if QT_CONFIG(topleveldomain)
222 if (qIsEffectiveTLD(domain) && url.host() != domain)
223 continue;
224#else
225 if (!domain.contains(u'.') && url.host() != domain)
226 continue;
227#endif // topleveldomain
228
229 // insert this cookie into result, sorted by path
230 QList<QNetworkCookie>::Iterator insertIt = result.begin();
231 while (insertIt != result.end()) {
232 if (insertIt->path().size() < it->path().size()) {
233 // insert here
234 insertIt = result.insert(insertIt, *it);
235 break;
236 } else {
237 ++insertIt;
238 }
239 }
240
241 // this is the shortest path yet, just append
242 if (insertIt == result.end())
243 result += *it;
244 }
245
246 return result;
247}
248
259{
262 bool isDeletion = !cookie.isSessionCookie() &&
263 cookie.expirationDate() < now;
264
265 deleteCookie(cookie);
266
267 if (!isDeletion) {
268 d->allCookies += cookie;
269 return true;
270 }
271 return false;
272}
273
285{
286 if (deleteCookie(cookie))
287 return insertCookie(cookie);
288 return false;
289}
290
300{
303 for (it = d->allCookies.begin(); it != d->allCookies.end(); ++it) {
304 if (it->hasSameIdentifier(cookie)) {
305 d->allCookies.erase(it);
306 return true;
307 }
308 }
309 return false;
310}
311
319{
320 QString domain = cookie.domain();
321 const QString host = url.host();
322 if (!isParentDomain(domain, host) && !isParentDomain(host, domain))
323 return false; // not accepted
324
325 if (domain.startsWith(u'.'))
326 domain = domain.mid(1);
327
328 // We shouldn't reject if:
329 // "[...] the domain-attribute is identical to the canonicalized request-host"
330 // https://tools.ietf.org/html/rfc6265#section-5.3 step 5
331 if (host == domain)
332 return true;
333 // the check for effective TLDs makes the "embedded dot" rule from RFC 2109 section 4.3.2
334 // redundant; the "leading dot" rule has been relaxed anyway, see QNetworkCookie::normalize()
335 // we remove the leading dot for this check if it's present
336 // Normally defined in qtldurl_p.h, but uses fall-back in this file when topleveldomain isn't
337 // configured:
338 return !qIsEffectiveTLD(domain);
339}
340
342
343#include "moc_qnetworkcookiejar.cpp"
\inmodule QtCore\reentrant
Definition qdatetime.h:257
static QDateTime currentDateTimeUtc()
Definition qlist.h:74
The QNetworkCookieJar class implements a simple jar of QNetworkCookie objects.
void setAllCookies(const QList< QNetworkCookie > &cookieList)
Sets the internal list of cookies held by this cookie jar to be cookieList.
virtual ~QNetworkCookieJar()
Destroys this cookie jar object and discards all cookies stored in it.
QList< QNetworkCookie > allCookies() const
Returns all cookies stored in this cookie jar.
virtual QList< QNetworkCookie > cookiesForUrl(const QUrl &url) const
Returns the cookies to be added to when a request is sent to url.
virtual bool insertCookie(const QNetworkCookie &cookie)
QNetworkCookieJar(QObject *parent=nullptr)
Creates a QNetworkCookieJar object and sets the parent object to be parent.
virtual bool updateCookie(const QNetworkCookie &cookie)
virtual bool validateCookie(const QNetworkCookie &cookie, const QUrl &url) const
virtual bool deleteCookie(const QNetworkCookie &cookie)
virtual bool setCookiesFromUrl(const QList< QNetworkCookie > &cookieList, const QUrl &url)
Adds the cookies in the list cookieList to this cookie jar.
The QNetworkCookie class holds one network cookie.
bool isSessionCookie() const
Returns true if this cookie is a session cookie.
QString domain() const
Returns the domain this cookie is associated with.
QDateTime expirationDate() const
Returns the expiration date for this cookie.
\inmodule QtCore
Definition qobject.h:90
qsizetype size() const
Definition qset.h:50
\inmodule QtCore
Definition qstringview.h:76
bool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QStringView mid(qsizetype pos, qsizetype n=-1) const noexcept
Returns the substring of length length starting at position start in this object.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5299
QString mid(qsizetype position, qsizetype n=-1) const
Returns a string that contains n characters of this string, starting at the specified position index.
Definition qstring.cpp:5204
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5350
bool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.h:1217
\inmodule QtCore
Definition qurl.h:94
QString host(ComponentFormattingOptions=FullyDecoded) const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition qurl.cpp:2337
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1983
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
Definition qurl.cpp:2465
QSet< QString >::iterator it
Combined button and popup list for selecting options.
static bool isEncrypted(const my_mach_header *header)
static QT_BEGIN_NAMESPACE bool qIsEffectiveTLD(QStringView domain)
static bool isParentDomain(const QString &domain, const QString &reference)
static bool isParentPath(const QString &path, const QString &reference)
GLuint GLuint end
GLint reference
GLsizei const GLchar *const * path
GLuint64EXT * result
[6]
QUrl url("example.com")
[constructor-url-reference]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent