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