Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkcookiejar.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
24 | ** | - | ||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
35 | ** | - | ||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||
39 | - | |||||||||||||
40 | #include "qnetworkcookiejar.h" | - | ||||||||||||
41 | #include "qnetworkcookiejar_p.h" | - | ||||||||||||
42 | - | |||||||||||||
43 | #include "QtNetwork/qnetworkcookie.h" | - | ||||||||||||
44 | #include "QtCore/qurl.h" | - | ||||||||||||
45 | #include "QtCore/qdatetime.h" | - | ||||||||||||
46 | #include "private/qtldurl_p.h" | - | ||||||||||||
47 | - | |||||||||||||
48 | QT_BEGIN_NAMESPACE | - | ||||||||||||
49 | - | |||||||||||||
50 | /*! | - | ||||||||||||
51 | \class QNetworkCookieJar | - | ||||||||||||
52 | \since 4.4 | - | ||||||||||||
53 | \inmodule QtNetwork | - | ||||||||||||
54 | - | |||||||||||||
55 | \brief The QNetworkCookieJar class implements a simple jar of QNetworkCookie objects | - | ||||||||||||
56 | - | |||||||||||||
57 | Cookies are small bits of information that stateless protocols | - | ||||||||||||
58 | like HTTP use to maintain some persistent information across | - | ||||||||||||
59 | requests. | - | ||||||||||||
60 | - | |||||||||||||
61 | A cookie is set by a remote server when it replies to a request | - | ||||||||||||
62 | and it expects the same cookie to be sent back when further | - | ||||||||||||
63 | requests are sent. | - | ||||||||||||
64 | - | |||||||||||||
65 | The cookie jar is the object that holds all cookies set in | - | ||||||||||||
66 | previous requests. Web browsers save their cookie jars to disk in | - | ||||||||||||
67 | order to conserve permanent cookies across invocations of the | - | ||||||||||||
68 | application. | - | ||||||||||||
69 | - | |||||||||||||
70 | QNetworkCookieJar does not implement permanent storage: it only | - | ||||||||||||
71 | keeps the cookies in memory. Once the QNetworkCookieJar object is | - | ||||||||||||
72 | deleted, all cookies it held will be discarded as well. If you | - | ||||||||||||
73 | want to save the cookies, you should derive from this class and | - | ||||||||||||
74 | implement the saving to disk to your own storage format. | - | ||||||||||||
75 | - | |||||||||||||
76 | This class implements only the basic security recommended by the | - | ||||||||||||
77 | cookie specifications and does not implement any cookie acceptance | - | ||||||||||||
78 | policy (it accepts all cookies set by any requests). In order to | - | ||||||||||||
79 | override those rules, you should reimplement the | - | ||||||||||||
80 | cookiesForUrl() and setCookiesFromUrl() virtual | - | ||||||||||||
81 | functions. They are called by QNetworkReply and | - | ||||||||||||
82 | QNetworkAccessManager when they detect new cookies and when they | - | ||||||||||||
83 | require cookies. | - | ||||||||||||
84 | - | |||||||||||||
85 | \sa QNetworkCookie, QNetworkAccessManager, QNetworkReply, | - | ||||||||||||
86 | QNetworkRequest, QNetworkAccessManager::setCookieJar() | - | ||||||||||||
87 | */ | - | ||||||||||||
88 | - | |||||||||||||
89 | /*! | - | ||||||||||||
90 | Creates a QNetworkCookieJar object and sets the parent object to | - | ||||||||||||
91 | be \a parent. | - | ||||||||||||
92 | - | |||||||||||||
93 | The cookie jar is initialized to empty. | - | ||||||||||||
94 | */ | - | ||||||||||||
95 | QNetworkCookieJar::QNetworkCookieJar(QObject *parent) | - | ||||||||||||
96 | : QObject(*new QNetworkCookieJarPrivate, parent) | - | ||||||||||||
97 | { | - | ||||||||||||
98 | } executed 285 times by 1 test: end of block Executed by:
| 285 | ||||||||||||
99 | - | |||||||||||||
100 | /*! | - | ||||||||||||
101 | Destroys this cookie jar object and discards all cookies stored in | - | ||||||||||||
102 | it. Cookies are not saved to disk in the QNetworkCookieJar default | - | ||||||||||||
103 | implementation. | - | ||||||||||||
104 | - | |||||||||||||
105 | If you need to save the cookies to disk, you have to derive from | - | ||||||||||||
106 | QNetworkCookieJar and save the cookies to disk yourself. | - | ||||||||||||
107 | */ | - | ||||||||||||
108 | QNetworkCookieJar::~QNetworkCookieJar() | - | ||||||||||||
109 | { | - | ||||||||||||
110 | } | - | ||||||||||||
111 | - | |||||||||||||
112 | /*! | - | ||||||||||||
113 | Returns all cookies stored in this cookie jar. This function is | - | ||||||||||||
114 | suitable for derived classes to save cookies to disk, as well as | - | ||||||||||||
115 | to implement cookie expiration and other policies. | - | ||||||||||||
116 | - | |||||||||||||
117 | \sa setAllCookies(), cookiesForUrl() | - | ||||||||||||
118 | */ | - | ||||||||||||
119 | QList<QNetworkCookie> QNetworkCookieJar::allCookies() const | - | ||||||||||||
120 | { | - | ||||||||||||
121 | return d_func()->allCookies; executed 37 times by 2 tests: return d_func()->allCookies; Executed by:
| 37 | ||||||||||||
122 | } | - | ||||||||||||
123 | - | |||||||||||||
124 | /*! | - | ||||||||||||
125 | Sets the internal list of cookies held by this cookie jar to be \a | - | ||||||||||||
126 | cookieList. This function is suitable for derived classes to | - | ||||||||||||
127 | implement loading cookies from permanent storage, or their own | - | ||||||||||||
128 | cookie acceptance policies by reimplementing | - | ||||||||||||
129 | setCookiesFromUrl(). | - | ||||||||||||
130 | - | |||||||||||||
131 | \sa allCookies(), setCookiesFromUrl() | - | ||||||||||||
132 | */ | - | ||||||||||||
133 | void QNetworkCookieJar::setAllCookies(const QList<QNetworkCookie> &cookieList) | - | ||||||||||||
134 | { | - | ||||||||||||
135 | Q_D(QNetworkCookieJar); | - | ||||||||||||
136 | d->allCookies = cookieList; | - | ||||||||||||
137 | } executed 719 times by 2 tests: end of block Executed by:
| 719 | ||||||||||||
138 | - | |||||||||||||
139 | static inline bool isParentPath(const QString &path, const QString &reference) | - | ||||||||||||
140 | { | - | ||||||||||||
141 | if (path.startsWith(reference)) {
| 44-205 | ||||||||||||
142 | //The cookie-path and the request-path are identical. | - | ||||||||||||
143 | if (path.length() == reference.length())
| 24-181 | ||||||||||||
144 | return true; executed 24 times by 1 test: return true; Executed by:
| 24 | ||||||||||||
145 | //The cookie-path is a prefix of the request-path, and the last | - | ||||||||||||
146 | //character of the cookie-path is %x2F ("/"). | - | ||||||||||||
147 | if (reference.endsWith('/'))
| 21-160 | ||||||||||||
148 | return true; executed 160 times by 2 tests: return true; Executed by:
| 160 | ||||||||||||
149 | //The cookie-path is a prefix of the request-path, and the first | - | ||||||||||||
150 | //character of the request-path that is not included in the cookie- | - | ||||||||||||
151 | //path is a %x2F ("/") character. | - | ||||||||||||
152 | if (path.at(reference.length()) == '/')
| 2-19 | ||||||||||||
153 | return true; executed 19 times by 2 tests: return true; Executed by:
| 19 | ||||||||||||
154 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
155 | return false; executed 46 times by 2 tests: return false; Executed by:
| 46 | ||||||||||||
156 | } | - | ||||||||||||
157 | - | |||||||||||||
158 | static inline bool isParentDomain(const QString &domain, const QString &reference) | - | ||||||||||||
159 | { | - | ||||||||||||
160 | if (!reference.startsWith(QLatin1Char('.')))
| 149-439 | ||||||||||||
161 | return domain == reference; executed 439 times by 2 tests: return domain == reference; Executed by:
| 439 | ||||||||||||
162 | - | |||||||||||||
163 | return domain.endsWith(reference) || domain == reference.midRef(1); executed 149 times by 2 tests: return domain.endsWith(reference) || domain == reference.midRef(1); Executed by:
| 149 | ||||||||||||
164 | } | - | ||||||||||||
165 | - | |||||||||||||
166 | /*! | - | ||||||||||||
167 | Adds the cookies in the list \a cookieList to this cookie | - | ||||||||||||
168 | jar. Before being inserted cookies are normalized. | - | ||||||||||||
169 | - | |||||||||||||
170 | Returns \c true if one or more cookies are set for \a url, | - | ||||||||||||
171 | otherwise false. | - | ||||||||||||
172 | - | |||||||||||||
173 | If a cookie already exists in the cookie jar, it will be | - | ||||||||||||
174 | overridden by those in \a cookieList. | - | ||||||||||||
175 | - | |||||||||||||
176 | The default QNetworkCookieJar class implements only a very basic | - | ||||||||||||
177 | security policy (it makes sure that the cookies' domain and path | - | ||||||||||||
178 | match the reply's). To enhance the security policy with your own | - | ||||||||||||
179 | algorithms, override setCookiesFromUrl(). | - | ||||||||||||
180 | - | |||||||||||||
181 | Also, QNetworkCookieJar does not have a maximum cookie jar | - | ||||||||||||
182 | size. Reimplement this function to discard older cookies to create | - | ||||||||||||
183 | room for new ones. | - | ||||||||||||
184 | - | |||||||||||||
185 | \sa cookiesForUrl(), QNetworkAccessManager::setCookieJar(), QNetworkCookie::normalize() | - | ||||||||||||
186 | */ | - | ||||||||||||
187 | bool QNetworkCookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, | - | ||||||||||||
188 | const QUrl &url) | - | ||||||||||||
189 | { | - | ||||||||||||
190 | bool added = false; | - | ||||||||||||
191 | for (QNetworkCookie cookie : cookieList) { | - | ||||||||||||
192 | cookie.normalize(url); | - | ||||||||||||
193 | if (validateCookie(cookie, url) && insertCookie(cookie))
| 12-234 | ||||||||||||
194 | added = true; executed 222 times by 2 tests: added = true; Executed by:
| 222 | ||||||||||||
195 | } executed 256 times by 2 tests: end of block Executed by:
| 256 | ||||||||||||
196 | return added; executed 254 times by 2 tests: return added; Executed by:
| 254 | ||||||||||||
197 | } | - | ||||||||||||
198 | - | |||||||||||||
199 | /*! | - | ||||||||||||
200 | Returns the cookies to be added to when a request is sent to | - | ||||||||||||
201 | \a url. This function is called by the default | - | ||||||||||||
202 | QNetworkAccessManager::createRequest(), which adds the | - | ||||||||||||
203 | cookies returned by this function to the request being sent. | - | ||||||||||||
204 | - | |||||||||||||
205 | If more than one cookie with the same name is found, but with | - | ||||||||||||
206 | differing paths, the one with longer path is returned before the | - | ||||||||||||
207 | one with shorter path. In other words, this function returns | - | ||||||||||||
208 | cookies sorted decreasingly by path length. | - | ||||||||||||
209 | - | |||||||||||||
210 | The default QNetworkCookieJar class implements only a very basic | - | ||||||||||||
211 | security policy (it makes sure that the cookies' domain and path | - | ||||||||||||
212 | match the reply's). To enhance the security policy with your own | - | ||||||||||||
213 | algorithms, override cookiesForUrl(). | - | ||||||||||||
214 | - | |||||||||||||
215 | \sa setCookiesFromUrl(), QNetworkAccessManager::setCookieJar() | - | ||||||||||||
216 | */ | - | ||||||||||||
217 | QList<QNetworkCookie> QNetworkCookieJar::cookiesForUrl(const QUrl &url) const | - | ||||||||||||
218 | { | - | ||||||||||||
219 | // \b Warning! This is only a dumb implementation! | - | ||||||||||||
220 | // It does NOT follow all of the recommendations from | - | ||||||||||||
221 | // http://wp.netscape.com/newsref/std/cookie_spec.html | - | ||||||||||||
222 | // It does not implement a very good cross-domain verification yet. | - | ||||||||||||
223 | - | |||||||||||||
224 | Q_D(const QNetworkCookieJar); | - | ||||||||||||
225 | const QDateTime now = QDateTime::currentDateTimeUtc(); | - | ||||||||||||
226 | QList<QNetworkCookie> result; | - | ||||||||||||
227 | bool isEncrypted = url.scheme() == QLatin1String("https"); | - | ||||||||||||
228 | - | |||||||||||||
229 | // scan our cookies for something that matches | - | ||||||||||||
230 | QList<QNetworkCookie>::ConstIterator it = d->allCookies.constBegin(), | - | ||||||||||||
231 | end = d->allCookies.constEnd(); | - | ||||||||||||
232 | for ( ; it != end; ++it) {
| 269-911 | ||||||||||||
233 | if (!isParentDomain(url.host(), it->domain()))
| 20-249 | ||||||||||||
234 | continue; executed 20 times by 2 tests: continue; Executed by:
| 20 | ||||||||||||
235 | if (!isParentPath(url.path(), it->path()))
| 46-203 | ||||||||||||
236 | continue; executed 46 times by 2 tests: continue; Executed by:
| 46 | ||||||||||||
237 | if (!(*it).isSessionCookie() && (*it).expirationDate() < now)
| 0-190 | ||||||||||||
238 | continue; never executed: continue; | 0 | ||||||||||||
239 | if ((*it).isSecure() && !isEncrypted)
| 2-185 | ||||||||||||
240 | continue; executed 16 times by 1 test: continue; Executed by:
| 16 | ||||||||||||
241 | - | |||||||||||||
242 | // insert this cookie into result, sorted by path | - | ||||||||||||
243 | QList<QNetworkCookie>::Iterator insertIt = result.begin(); | - | ||||||||||||
244 | while (insertIt != result.end()) {
| 28-177 | ||||||||||||
245 | if (insertIt->path().length() < it->path().length()) {
| 10-18 | ||||||||||||
246 | // insert here | - | ||||||||||||
247 | insertIt = result.insert(insertIt, *it); | - | ||||||||||||
248 | break; executed 10 times by 2 tests: break; Executed by:
| 10 | ||||||||||||
249 | } else { | - | ||||||||||||
250 | ++insertIt; | - | ||||||||||||
251 | } executed 18 times by 1 test: end of block Executed by:
| 18 | ||||||||||||
252 | } | - | ||||||||||||
253 | - | |||||||||||||
254 | // this is the shortest path yet, just append | - | ||||||||||||
255 | if (insertIt == result.end())
| 10-177 | ||||||||||||
256 | result += *it; executed 177 times by 2 tests: result += *it; Executed by:
| 177 | ||||||||||||
257 | } executed 187 times by 2 tests: end of block Executed by:
| 187 | ||||||||||||
258 | - | |||||||||||||
259 | return result; executed 911 times by 2 tests: return result; Executed by:
| 911 | ||||||||||||
260 | } | - | ||||||||||||
261 | - | |||||||||||||
262 | /*! | - | ||||||||||||
263 | \since 5.0 | - | ||||||||||||
264 | Adds \a cookie to this cookie jar. | - | ||||||||||||
265 | - | |||||||||||||
266 | Returns \c true if \a cookie was added, false otherwise. | - | ||||||||||||
267 | - | |||||||||||||
268 | If a cookie with the same identifier already exists in the | - | ||||||||||||
269 | cookie jar, it will be overridden. | - | ||||||||||||
270 | */ | - | ||||||||||||
271 | bool QNetworkCookieJar::insertCookie(const QNetworkCookie &cookie) | - | ||||||||||||
272 | { | - | ||||||||||||
273 | Q_D(QNetworkCookieJar); | - | ||||||||||||
274 | const QDateTime now = QDateTime::currentDateTimeUtc(); | - | ||||||||||||
275 | bool isDeletion = !cookie.isSessionCookie() &&
| 28-206 | ||||||||||||
276 | cookie.expirationDate() < now;
| 12-16 | ||||||||||||
277 | - | |||||||||||||
278 | deleteCookie(cookie); | - | ||||||||||||
279 | - | |||||||||||||
280 | if (!isDeletion) {
| 12-222 | ||||||||||||
281 | d->allCookies += cookie; | - | ||||||||||||
282 | return true; executed 222 times by 2 tests: return true; Executed by:
| 222 | ||||||||||||
283 | } | - | ||||||||||||
284 | return false; executed 12 times by 1 test: return false; Executed by:
| 12 | ||||||||||||
285 | } | - | ||||||||||||
286 | - | |||||||||||||
287 | /*! | - | ||||||||||||
288 | \since 5.0 | - | ||||||||||||
289 | If a cookie with the same identifier as \a cookie exists in this cookie jar | - | ||||||||||||
290 | it will be updated. This function uses insertCookie(). | - | ||||||||||||
291 | - | |||||||||||||
292 | Returns \c true if \a cookie was updated, false if no cookie in the jar matches | - | ||||||||||||
293 | the identifier of \a cookie. | - | ||||||||||||
294 | - | |||||||||||||
295 | \sa QNetworkCookie::hasSameIdentifier() | - | ||||||||||||
296 | */ | - | ||||||||||||
297 | bool QNetworkCookieJar::updateCookie(const QNetworkCookie &cookie) | - | ||||||||||||
298 | { | - | ||||||||||||
299 | if (deleteCookie(cookie))
| 0 | ||||||||||||
300 | return insertCookie(cookie); never executed: return insertCookie(cookie); | 0 | ||||||||||||
301 | return false; never executed: return false; | 0 | ||||||||||||
302 | } | - | ||||||||||||
303 | - | |||||||||||||
304 | /*! | - | ||||||||||||
305 | \since 5.0 | - | ||||||||||||
306 | Deletes from cookie jar the cookie found to have the same identifier as \a cookie. | - | ||||||||||||
307 | - | |||||||||||||
308 | Returns \c true if a cookie was deleted, false otherwise. | - | ||||||||||||
309 | - | |||||||||||||
310 | \sa QNetworkCookie::hasSameIdentifier() | - | ||||||||||||
311 | */ | - | ||||||||||||
312 | bool QNetworkCookieJar::deleteCookie(const QNetworkCookie &cookie) | - | ||||||||||||
313 | { | - | ||||||||||||
314 | Q_D(QNetworkCookieJar); | - | ||||||||||||
315 | QList<QNetworkCookie>::Iterator it; | - | ||||||||||||
316 | for (it = d->allCookies.begin(); it != d->allCookies.end(); ++it) {
| 45-227 | ||||||||||||
317 | if (it->hasSameIdentifier(cookie)) {
| 7-38 | ||||||||||||
318 | d->allCookies.erase(it); | - | ||||||||||||
319 | return true; executed 7 times by 1 test: return true; Executed by:
| 7 | ||||||||||||
320 | } | - | ||||||||||||
321 | } executed 38 times by 2 tests: end of block Executed by:
| 38 | ||||||||||||
322 | return false; executed 227 times by 2 tests: return false; Executed by:
| 227 | ||||||||||||
323 | } | - | ||||||||||||
324 | - | |||||||||||||
325 | /*! | - | ||||||||||||
326 | \since 5.0 | - | ||||||||||||
327 | Returns \c true if the domain and path of \a cookie are valid, false otherwise. | - | ||||||||||||
328 | The \a url parameter is used to determine if the domain specified in the cookie | - | ||||||||||||
329 | is allowed. | - | ||||||||||||
330 | */ | - | ||||||||||||
331 | bool QNetworkCookieJar::validateCookie(const QNetworkCookie &cookie, const QUrl &url) const | - | ||||||||||||
332 | { | - | ||||||||||||
333 | QString domain = cookie.domain(); | - | ||||||||||||
334 | const QString host = url.host(); | - | ||||||||||||
335 | if (!isParentDomain(domain, host) && !isParentDomain(host, domain))
| 15-193 | ||||||||||||
336 | return false; // not accepted executed 15 times by 2 tests: return false; Executed by:
| 15 | ||||||||||||
337 | - | |||||||||||||
338 | // the check for effective TLDs makes the "embedded dot" rule from RFC 2109 section 4.3.2 | - | ||||||||||||
339 | // redundant; the "leading dot" rule has been relaxed anyway, see QNetworkCookie::normalize() | - | ||||||||||||
340 | // we remove the leading dot for this check if it's present | - | ||||||||||||
341 | if (qIsEffectiveTLD(domain.startsWith('.') ? domain.remove(0, 1) : domain))
| 7-234 | ||||||||||||
342 | return false; // not accepted executed 7 times by 1 test: return false; Executed by:
| 7 | ||||||||||||
343 | - | |||||||||||||
344 | return true; executed 234 times by 2 tests: return true; Executed by:
| 234 | ||||||||||||
345 | } | - | ||||||||||||
346 | - | |||||||||||||
347 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |