| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkcookie.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 "qnetworkcookie.h" | - | ||||||||||||||||||
| 41 | #include "qnetworkcookie_p.h" | - | ||||||||||||||||||
| 42 | - | |||||||||||||||||||
| 43 | #include "qnetworkrequest.h" | - | ||||||||||||||||||
| 44 | #include "qnetworkreply.h" | - | ||||||||||||||||||
| 45 | #include "QtCore/qbytearray.h" | - | ||||||||||||||||||
| 46 | #include "QtCore/qdebug.h" | - | ||||||||||||||||||
| 47 | #include "QtCore/qlist.h" | - | ||||||||||||||||||
| 48 | #include "QtCore/qlocale.h" | - | ||||||||||||||||||
| 49 | #include "QtCore/qstring.h" | - | ||||||||||||||||||
| 50 | #include "QtCore/qstringlist.h" | - | ||||||||||||||||||
| 51 | #include "QtCore/qurl.h" | - | ||||||||||||||||||
| 52 | #include "QtNetwork/qhostaddress.h" | - | ||||||||||||||||||
| 53 | #include "private/qobject_p.h" | - | ||||||||||||||||||
| 54 | - | |||||||||||||||||||
| 55 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 56 | - | |||||||||||||||||||
| 57 | /*! | - | ||||||||||||||||||
| 58 | \class QNetworkCookie | - | ||||||||||||||||||
| 59 | \since 4.4 | - | ||||||||||||||||||
| 60 | \ingroup shared | - | ||||||||||||||||||
| 61 | \inmodule QtNetwork | - | ||||||||||||||||||
| 62 | - | |||||||||||||||||||
| 63 | \brief The QNetworkCookie class holds one network cookie. | - | ||||||||||||||||||
| 64 | - | |||||||||||||||||||
| 65 | Cookies are small bits of information that stateless protocols | - | ||||||||||||||||||
| 66 | like HTTP use to maintain some persistent information across | - | ||||||||||||||||||
| 67 | requests. | - | ||||||||||||||||||
| 68 | - | |||||||||||||||||||
| 69 | A cookie is set by a remote server when it replies to a request | - | ||||||||||||||||||
| 70 | and it expects the same cookie to be sent back when further | - | ||||||||||||||||||
| 71 | requests are sent. | - | ||||||||||||||||||
| 72 | - | |||||||||||||||||||
| 73 | QNetworkCookie holds one such cookie as received from the | - | ||||||||||||||||||
| 74 | network. A cookie has a name and a value, but those are opaque to | - | ||||||||||||||||||
| 75 | the application (that is, the information stored in them has no | - | ||||||||||||||||||
| 76 | meaning to the application). A cookie has an associated path name | - | ||||||||||||||||||
| 77 | and domain, which indicate when the cookie should be sent again to | - | ||||||||||||||||||
| 78 | the server. | - | ||||||||||||||||||
| 79 | - | |||||||||||||||||||
| 80 | A cookie can also have an expiration date, indicating its | - | ||||||||||||||||||
| 81 | validity. If the expiration date is not present, the cookie is | - | ||||||||||||||||||
| 82 | considered a "session cookie" and should be discarded when the | - | ||||||||||||||||||
| 83 | application exits (or when its concept of session is over). | - | ||||||||||||||||||
| 84 | - | |||||||||||||||||||
| 85 | QNetworkCookie provides a way of parsing a cookie from the HTTP | - | ||||||||||||||||||
| 86 | header format using the QNetworkCookie::parseCookies() | - | ||||||||||||||||||
| 87 | function. However, when received in a QNetworkReply, the cookie is | - | ||||||||||||||||||
| 88 | already parsed. | - | ||||||||||||||||||
| 89 | - | |||||||||||||||||||
| 90 | This class implements cookies as described by the | - | ||||||||||||||||||
| 91 | \l{Netscape Cookie Specification}{initial cookie specification by | - | ||||||||||||||||||
| 92 | Netscape}, which is somewhat similar to the \l{http://www.rfc-editor.org/rfc/rfc2109.txt}{RFC 2109} specification, | - | ||||||||||||||||||
| 93 | plus the \l{Mitigating Cross-site Scripting With HTTP-only Cookies} | - | ||||||||||||||||||
| 94 | {"HttpOnly" extension}. The more recent \l{http://www.rfc-editor.org/rfc/rfc2965.txt}{RFC 2965} specification | - | ||||||||||||||||||
| 95 | (which uses the Set-Cookie2 header) is not supported. | - | ||||||||||||||||||
| 96 | - | |||||||||||||||||||
| 97 | \sa QNetworkCookieJar, QNetworkRequest, QNetworkReply | - | ||||||||||||||||||
| 98 | */ | - | ||||||||||||||||||
| 99 | - | |||||||||||||||||||
| 100 | /*! | - | ||||||||||||||||||
| 101 | Create a new QNetworkCookie object, initializing the cookie name | - | ||||||||||||||||||
| 102 | to \a name and its value to \a value. | - | ||||||||||||||||||
| 103 | - | |||||||||||||||||||
| 104 | A cookie is only valid if it has a name. However, the value is | - | ||||||||||||||||||
| 105 | opaque to the application and being empty may have significance to | - | ||||||||||||||||||
| 106 | the remote server. | - | ||||||||||||||||||
| 107 | */ | - | ||||||||||||||||||
| 108 | QNetworkCookie::QNetworkCookie(const QByteArray &name, const QByteArray &value) | - | ||||||||||||||||||
| 109 | : d(new QNetworkCookiePrivate) | - | ||||||||||||||||||
| 110 | { | - | ||||||||||||||||||
| 111 | qRegisterMetaType<QNetworkCookie>(); | - | ||||||||||||||||||
| 112 | qRegisterMetaType<QList<QNetworkCookie> >(); | - | ||||||||||||||||||
| 113 | - | |||||||||||||||||||
| 114 | d->name = name; | - | ||||||||||||||||||
| 115 | d->value = value; | - | ||||||||||||||||||
| 116 | } executed 919 times by 5 tests: end of blockExecuted by:
| 919 | ||||||||||||||||||
| 117 | - | |||||||||||||||||||
| 118 | /*! | - | ||||||||||||||||||
| 119 | Creates a new QNetworkCookie object by copying the contents of \a | - | ||||||||||||||||||
| 120 | other. | - | ||||||||||||||||||
| 121 | */ | - | ||||||||||||||||||
| 122 | QNetworkCookie::QNetworkCookie(const QNetworkCookie &other) | - | ||||||||||||||||||
| 123 | : d(other.d) | - | ||||||||||||||||||
| 124 | { | - | ||||||||||||||||||
| 125 | } executed 2224 times by 5 tests: end of blockExecuted by:
| 2224 | ||||||||||||||||||
| 126 | - | |||||||||||||||||||
| 127 | /*! | - | ||||||||||||||||||
| 128 | Destroys this QNetworkCookie object. | - | ||||||||||||||||||
| 129 | */ | - | ||||||||||||||||||
| 130 | QNetworkCookie::~QNetworkCookie() | - | ||||||||||||||||||
| 131 | { | - | ||||||||||||||||||
| 132 | // QSharedDataPointer auto deletes | - | ||||||||||||||||||
| 133 | d = 0; | - | ||||||||||||||||||
| 134 | } executed 3143 times by 5 tests: end of blockExecuted by:
| 3143 | ||||||||||||||||||
| 135 | - | |||||||||||||||||||
| 136 | /*! | - | ||||||||||||||||||
| 137 | Copies the contents of the QNetworkCookie object \a other to this | - | ||||||||||||||||||
| 138 | object. | - | ||||||||||||||||||
| 139 | */ | - | ||||||||||||||||||
| 140 | QNetworkCookie &QNetworkCookie::operator=(const QNetworkCookie &other) | - | ||||||||||||||||||
| 141 | { | - | ||||||||||||||||||
| 142 | d = other.d; | - | ||||||||||||||||||
| 143 | return *this; executed 2 times by 1 test: return *this;Executed by:
| 2 | ||||||||||||||||||
| 144 | } | - | ||||||||||||||||||
| 145 | - | |||||||||||||||||||
| 146 | /*! | - | ||||||||||||||||||
| 147 | \fn void QNetworkCookie::swap(QNetworkCookie &other) | - | ||||||||||||||||||
| 148 | \since 5.0 | - | ||||||||||||||||||
| 149 | - | |||||||||||||||||||
| 150 | Swaps this cookie with \a other. This function is very fast and | - | ||||||||||||||||||
| 151 | never fails. | - | ||||||||||||||||||
| 152 | */ | - | ||||||||||||||||||
| 153 | - | |||||||||||||||||||
| 154 | /*! | - | ||||||||||||||||||
| 155 | \fn bool QNetworkCookie::operator!=(const QNetworkCookie &other) const | - | ||||||||||||||||||
| 156 | - | |||||||||||||||||||
| 157 | Returns \c true if this cookie is not equal to \a other. | - | ||||||||||||||||||
| 158 | - | |||||||||||||||||||
| 159 | \sa operator==() | - | ||||||||||||||||||
| 160 | */ | - | ||||||||||||||||||
| 161 | - | |||||||||||||||||||
| 162 | /*! | - | ||||||||||||||||||
| 163 | \since 5.0 | - | ||||||||||||||||||
| 164 | Returns \c true if this cookie is equal to \a other. This function | - | ||||||||||||||||||
| 165 | only returns \c true if all fields of the cookie are the same. | - | ||||||||||||||||||
| 166 | - | |||||||||||||||||||
| 167 | However, in some contexts, two cookies of the same name could be | - | ||||||||||||||||||
| 168 | considered equal. | - | ||||||||||||||||||
| 169 | - | |||||||||||||||||||
| 170 | \sa operator!=(), hasSameIdentifier() | - | ||||||||||||||||||
| 171 | */ | - | ||||||||||||||||||
| 172 | bool QNetworkCookie::operator==(const QNetworkCookie &other) const | - | ||||||||||||||||||
| 173 | { | - | ||||||||||||||||||
| 174 | if (d == other.d)
| 33-499 | ||||||||||||||||||
| 175 | return true; executed 33 times by 1 test: return true;Executed by:
| 33 | ||||||||||||||||||
| 176 | return d->name == other.d->name && executed 499 times by 4 tests: return d->name == other.d->name && d->value == other.d->value && d->expirationDate.toUTC() == other.d->expirationDate.toUTC() && d->domain == other.d->domain && d->path == other.d->path && d->secure == other.d->secure && d->comment == other.d->comment;Executed by:
| 499 | ||||||||||||||||||
| 177 | d->value == other.d->value && executed 499 times by 4 tests: return d->name == other.d->name && d->value == other.d->value && d->expirationDate.toUTC() == other.d->expirationDate.toUTC() && d->domain == other.d->domain && d->path == other.d->path && d->secure == other.d->secure && d->comment == other.d->comment;Executed by:
| 499 | ||||||||||||||||||
| 178 | d->expirationDate.toUTC() == other.d->expirationDate.toUTC() && executed 499 times by 4 tests: return d->name == other.d->name && d->value == other.d->value && d->expirationDate.toUTC() == other.d->expirationDate.toUTC() && d->domain == other.d->domain && d->path == other.d->path && d->secure == other.d->secure && d->comment == other.d->comment;Executed by:
| 499 | ||||||||||||||||||
| 179 | d->domain == other.d->domain && executed 499 times by 4 tests: return d->name == other.d->name && d->value == other.d->value && d->expirationDate.toUTC() == other.d->expirationDate.toUTC() && d->domain == other.d->domain && d->path == other.d->path && d->secure == other.d->secure && d->comment == other.d->comment;Executed by:
| 499 | ||||||||||||||||||
| 180 | d->path == other.d->path && executed 499 times by 4 tests: return d->name == other.d->name && d->value == other.d->value && d->expirationDate.toUTC() == other.d->expirationDate.toUTC() && d->domain == other.d->domain && d->path == other.d->path && d->secure == other.d->secure && d->comment == other.d->comment;Executed by:
| 499 | ||||||||||||||||||
| 181 | d->secure == other.d->secure && executed 499 times by 4 tests: return d->name == other.d->name && d->value == other.d->value && d->expirationDate.toUTC() == other.d->expirationDate.toUTC() && d->domain == other.d->domain && d->path == other.d->path && d->secure == other.d->secure && d->comment == other.d->comment;Executed by:
| 499 | ||||||||||||||||||
| 182 | d->comment == other.d->comment; executed 499 times by 4 tests: return d->name == other.d->name && d->value == other.d->value && d->expirationDate.toUTC() == other.d->expirationDate.toUTC() && d->domain == other.d->domain && d->path == other.d->path && d->secure == other.d->secure && d->comment == other.d->comment;Executed by:
| 499 | ||||||||||||||||||
| 183 | } | - | ||||||||||||||||||
| 184 | - | |||||||||||||||||||
| 185 | /*! | - | ||||||||||||||||||
| 186 | Returns \c true if this cookie has the same identifier tuple as \a other. | - | ||||||||||||||||||
| 187 | The identifier tuple is composed of the name, domain and path. | - | ||||||||||||||||||
| 188 | - | |||||||||||||||||||
| 189 | \sa operator==() | - | ||||||||||||||||||
| 190 | */ | - | ||||||||||||||||||
| 191 | bool QNetworkCookie::hasSameIdentifier(const QNetworkCookie &other) const | - | ||||||||||||||||||
| 192 | { | - | ||||||||||||||||||
| 193 | return d->name == other.d->name && d->domain == other.d->domain && d->path == other.d->path; executed 45 times by 2 tests: return d->name == other.d->name && d->domain == other.d->domain && d->path == other.d->path;Executed by:
| 45 | ||||||||||||||||||
| 194 | } | - | ||||||||||||||||||
| 195 | - | |||||||||||||||||||
| 196 | /*! | - | ||||||||||||||||||
| 197 | Returns \c true if the "secure" option was specified in the cookie | - | ||||||||||||||||||
| 198 | string, false otherwise. | - | ||||||||||||||||||
| 199 | - | |||||||||||||||||||
| 200 | Secure cookies may contain private information and should not be | - | ||||||||||||||||||
| 201 | resent over unencrypted connections. | - | ||||||||||||||||||
| 202 | - | |||||||||||||||||||
| 203 | \sa setSecure() | - | ||||||||||||||||||
| 204 | */ | - | ||||||||||||||||||
| 205 | bool QNetworkCookie::isSecure() const | - | ||||||||||||||||||
| 206 | { | - | ||||||||||||||||||
| 207 | return d->secure; executed 1312 times by 4 tests: return d->secure;Executed by:
| 1312 | ||||||||||||||||||
| 208 | } | - | ||||||||||||||||||
| 209 | - | |||||||||||||||||||
| 210 | /*! | - | ||||||||||||||||||
| 211 | Sets the secure flag of this cookie to \a enable. | - | ||||||||||||||||||
| 212 | - | |||||||||||||||||||
| 213 | Secure cookies may contain private information and should not be | - | ||||||||||||||||||
| 214 | resent over unencrypted connections. | - | ||||||||||||||||||
| 215 | - | |||||||||||||||||||
| 216 | \sa isSecure() | - | ||||||||||||||||||
| 217 | */ | - | ||||||||||||||||||
| 218 | void QNetworkCookie::setSecure(bool enable) | - | ||||||||||||||||||
| 219 | { | - | ||||||||||||||||||
| 220 | d->secure = enable; | - | ||||||||||||||||||
| 221 | } executed 44 times by 2 tests: end of blockExecuted by:
| 44 | ||||||||||||||||||
| 222 | - | |||||||||||||||||||
| 223 | /*! | - | ||||||||||||||||||
| 224 | \since 4.5 | - | ||||||||||||||||||
| 225 | - | |||||||||||||||||||
| 226 | Returns \c true if the "HttpOnly" flag is enabled for this cookie. | - | ||||||||||||||||||
| 227 | - | |||||||||||||||||||
| 228 | A cookie that is "HttpOnly" is only set and retrieved by the | - | ||||||||||||||||||
| 229 | network requests and replies; i.e., the HTTP protocol. It is not | - | ||||||||||||||||||
| 230 | accessible from scripts running on browsers. | - | ||||||||||||||||||
| 231 | - | |||||||||||||||||||
| 232 | \sa isSecure() | - | ||||||||||||||||||
| 233 | */ | - | ||||||||||||||||||
| 234 | bool QNetworkCookie::isHttpOnly() const | - | ||||||||||||||||||
| 235 | { | - | ||||||||||||||||||
| 236 | return d->httpOnly; executed 1106 times by 4 tests: return d->httpOnly;Executed by:
| 1106 | ||||||||||||||||||
| 237 | } | - | ||||||||||||||||||
| 238 | - | |||||||||||||||||||
| 239 | /*! | - | ||||||||||||||||||
| 240 | \since 4.5 | - | ||||||||||||||||||
| 241 | - | |||||||||||||||||||
| 242 | Sets this cookie's "HttpOnly" flag to \a enable. | - | ||||||||||||||||||
| 243 | */ | - | ||||||||||||||||||
| 244 | void QNetworkCookie::setHttpOnly(bool enable) | - | ||||||||||||||||||
| 245 | { | - | ||||||||||||||||||
| 246 | d->httpOnly = enable; | - | ||||||||||||||||||
| 247 | } executed 19 times by 2 tests: end of blockExecuted by:
| 19 | ||||||||||||||||||
| 248 | - | |||||||||||||||||||
| 249 | /*! | - | ||||||||||||||||||
| 250 | Returns \c true if this cookie is a session cookie. A session cookie | - | ||||||||||||||||||
| 251 | is a cookie which has no expiration date, which means it should be | - | ||||||||||||||||||
| 252 | discarded when the application's concept of session is over | - | ||||||||||||||||||
| 253 | (usually, when the application exits). | - | ||||||||||||||||||
| 254 | - | |||||||||||||||||||
| 255 | \sa expirationDate(), setExpirationDate() | - | ||||||||||||||||||
| 256 | */ | - | ||||||||||||||||||
| 257 | bool QNetworkCookie::isSessionCookie() const | - | ||||||||||||||||||
| 258 | { | - | ||||||||||||||||||
| 259 | return !d->expirationDate.isValid(); executed 1546 times by 4 tests: return !d->expirationDate.isValid();Executed by:
| 1546 | ||||||||||||||||||
| 260 | } | - | ||||||||||||||||||
| 261 | - | |||||||||||||||||||
| 262 | /*! | - | ||||||||||||||||||
| 263 | Returns the expiration date for this cookie. If this cookie is a | - | ||||||||||||||||||
| 264 | session cookie, the QDateTime returned will not be valid. If the | - | ||||||||||||||||||
| 265 | date is in the past, this cookie has already expired and should | - | ||||||||||||||||||
| 266 | not be sent again back to a remote server. | - | ||||||||||||||||||
| 267 | - | |||||||||||||||||||
| 268 | The expiration date corresponds to the parameters of the "expires" | - | ||||||||||||||||||
| 269 | entry in the cookie string. | - | ||||||||||||||||||
| 270 | - | |||||||||||||||||||
| 271 | \sa isSessionCookie(), setExpirationDate() | - | ||||||||||||||||||
| 272 | */ | - | ||||||||||||||||||
| 273 | QDateTime QNetworkCookie::expirationDate() const | - | ||||||||||||||||||
| 274 | { | - | ||||||||||||||||||
| 275 | return d->expirationDate; executed 252 times by 3 tests: return d->expirationDate;Executed by:
| 252 | ||||||||||||||||||
| 276 | } | - | ||||||||||||||||||
| 277 | - | |||||||||||||||||||
| 278 | /*! | - | ||||||||||||||||||
| 279 | Sets the expiration date of this cookie to \a date. Setting an | - | ||||||||||||||||||
| 280 | invalid expiration date to this cookie will mean it's a session | - | ||||||||||||||||||
| 281 | cookie. | - | ||||||||||||||||||
| 282 | - | |||||||||||||||||||
| 283 | \sa isSessionCookie(), expirationDate() | - | ||||||||||||||||||
| 284 | */ | - | ||||||||||||||||||
| 285 | void QNetworkCookie::setExpirationDate(const QDateTime &date) | - | ||||||||||||||||||
| 286 | { | - | ||||||||||||||||||
| 287 | d->expirationDate = date; | - | ||||||||||||||||||
| 288 | } executed 507 times by 3 tests: end of blockExecuted by:
| 507 | ||||||||||||||||||
| 289 | - | |||||||||||||||||||
| 290 | /*! | - | ||||||||||||||||||
| 291 | Returns the domain this cookie is associated with. This | - | ||||||||||||||||||
| 292 | corresponds to the "domain" field of the cookie string. | - | ||||||||||||||||||
| 293 | - | |||||||||||||||||||
| 294 | Note that the domain here may start with a dot, which is not a | - | ||||||||||||||||||
| 295 | valid hostname. However, it means this cookie matches all | - | ||||||||||||||||||
| 296 | hostnames ending with that domain name. | - | ||||||||||||||||||
| 297 | - | |||||||||||||||||||
| 298 | \sa setDomain() | - | ||||||||||||||||||
| 299 | */ | - | ||||||||||||||||||
| 300 | QString QNetworkCookie::domain() const | - | ||||||||||||||||||
| 301 | { | - | ||||||||||||||||||
| 302 | return d->domain; executed 528 times by 3 tests: return d->domain;Executed by:
| 528 | ||||||||||||||||||
| 303 | } | - | ||||||||||||||||||
| 304 | - | |||||||||||||||||||
| 305 | /*! | - | ||||||||||||||||||
| 306 | Sets the domain associated with this cookie to be \a domain. | - | ||||||||||||||||||
| 307 | - | |||||||||||||||||||
| 308 | \sa domain() | - | ||||||||||||||||||
| 309 | */ | - | ||||||||||||||||||
| 310 | void QNetworkCookie::setDomain(const QString &domain) | - | ||||||||||||||||||
| 311 | { | - | ||||||||||||||||||
| 312 | d->domain = domain; | - | ||||||||||||||||||
| 313 | } executed 157 times by 3 tests: end of blockExecuted by:
| 157 | ||||||||||||||||||
| 314 | - | |||||||||||||||||||
| 315 | /*! | - | ||||||||||||||||||
| 316 | Returns the path associated with this cookie. This corresponds to | - | ||||||||||||||||||
| 317 | the "path" field of the cookie string. | - | ||||||||||||||||||
| 318 | - | |||||||||||||||||||
| 319 | \sa setPath() | - | ||||||||||||||||||
| 320 | */ | - | ||||||||||||||||||
| 321 | QString QNetworkCookie::path() const | - | ||||||||||||||||||
| 322 | { | - | ||||||||||||||||||
| 323 | return d->path; executed 308 times by 3 tests: return d->path;Executed by:
| 308 | ||||||||||||||||||
| 324 | } | - | ||||||||||||||||||
| 325 | - | |||||||||||||||||||
| 326 | /*! | - | ||||||||||||||||||
| 327 | Sets the path associated with this cookie to be \a path. | - | ||||||||||||||||||
| 328 | - | |||||||||||||||||||
| 329 | \sa path() | - | ||||||||||||||||||
| 330 | */ | - | ||||||||||||||||||
| 331 | void QNetworkCookie::setPath(const QString &path) | - | ||||||||||||||||||
| 332 | { | - | ||||||||||||||||||
| 333 | d->path = path; | - | ||||||||||||||||||
| 334 | } executed 197 times by 4 tests: end of blockExecuted by:
| 197 | ||||||||||||||||||
| 335 | - | |||||||||||||||||||
| 336 | /*! | - | ||||||||||||||||||
| 337 | Returns the name of this cookie. The only mandatory field of a | - | ||||||||||||||||||
| 338 | cookie is its name, without which it is not considered valid. | - | ||||||||||||||||||
| 339 | - | |||||||||||||||||||
| 340 | \sa setName(), value() | - | ||||||||||||||||||
| 341 | */ | - | ||||||||||||||||||
| 342 | QByteArray QNetworkCookie::name() const | - | ||||||||||||||||||
| 343 | { | - | ||||||||||||||||||
| 344 | return d->name; executed 979 times by 5 tests: return d->name;Executed by:
| 979 | ||||||||||||||||||
| 345 | } | - | ||||||||||||||||||
| 346 | - | |||||||||||||||||||
| 347 | /*! | - | ||||||||||||||||||
| 348 | Sets the name of this cookie to be \a cookieName. Note that | - | ||||||||||||||||||
| 349 | setting a cookie name to an empty QByteArray will make this cookie | - | ||||||||||||||||||
| 350 | invalid. | - | ||||||||||||||||||
| 351 | - | |||||||||||||||||||
| 352 | \sa name(), value() | - | ||||||||||||||||||
| 353 | */ | - | ||||||||||||||||||
| 354 | void QNetworkCookie::setName(const QByteArray &cookieName) | - | ||||||||||||||||||
| 355 | { | - | ||||||||||||||||||
| 356 | d->name = cookieName; | - | ||||||||||||||||||
| 357 | } executed 858 times by 5 tests: end of blockExecuted by:
| 858 | ||||||||||||||||||
| 358 | - | |||||||||||||||||||
| 359 | /*! | - | ||||||||||||||||||
| 360 | Returns this cookies value, as specified in the cookie | - | ||||||||||||||||||
| 361 | string. Note that a cookie is still valid if its value is empty. | - | ||||||||||||||||||
| 362 | - | |||||||||||||||||||
| 363 | Cookie name-value pairs are considered opaque to the application: | - | ||||||||||||||||||
| 364 | that is, their values don't mean anything. | - | ||||||||||||||||||
| 365 | - | |||||||||||||||||||
| 366 | \sa setValue(), name() | - | ||||||||||||||||||
| 367 | */ | - | ||||||||||||||||||
| 368 | QByteArray QNetworkCookie::value() const | - | ||||||||||||||||||
| 369 | { | - | ||||||||||||||||||
| 370 | return d->value; executed 305 times by 2 tests: return d->value;Executed by:
| 305 | ||||||||||||||||||
| 371 | } | - | ||||||||||||||||||
| 372 | - | |||||||||||||||||||
| 373 | /*! | - | ||||||||||||||||||
| 374 | Sets the value of this cookie to be \a value. | - | ||||||||||||||||||
| 375 | - | |||||||||||||||||||
| 376 | \sa value(), name() | - | ||||||||||||||||||
| 377 | */ | - | ||||||||||||||||||
| 378 | void QNetworkCookie::setValue(const QByteArray &value) | - | ||||||||||||||||||
| 379 | { | - | ||||||||||||||||||
| 380 | d->value = value; | - | ||||||||||||||||||
| 381 | } executed 866 times by 5 tests: end of blockExecuted by:
| 866 | ||||||||||||||||||
| 382 | - | |||||||||||||||||||
| 383 | // ### move this to qnetworkcookie_p.h and share with qnetworkaccesshttpbackend | - | ||||||||||||||||||
| 384 | static QPair<QByteArray, QByteArray> nextField(const QByteArray &text, int &position, bool isNameValue) | - | ||||||||||||||||||
| 385 | { | - | ||||||||||||||||||
| 386 | // format is one of: | - | ||||||||||||||||||
| 387 | // (1) token | - | ||||||||||||||||||
| 388 | // (2) token = token | - | ||||||||||||||||||
| 389 | // (3) token = quoted-string | - | ||||||||||||||||||
| 390 | const int length = text.length(); | - | ||||||||||||||||||
| 391 | position = nextNonWhitespace(text, position); | - | ||||||||||||||||||
| 392 | - | |||||||||||||||||||
| 393 | int semiColonPosition = text.indexOf(';', position); | - | ||||||||||||||||||
| 394 | if (semiColonPosition < 0)
| 694-717 | ||||||||||||||||||
| 395 | semiColonPosition = length; //no ';' means take everything to end of string executed 717 times by 5 tests: semiColonPosition = length;Executed by:
| 717 | ||||||||||||||||||
| 396 | - | |||||||||||||||||||
| 397 | int equalsPosition = text.indexOf('=', position); | - | ||||||||||||||||||
| 398 | if (equalsPosition < 0 || equalsPosition > semiColonPosition) {
| 20-1286 | ||||||||||||||||||
| 399 | if (isNameValue)
| 34-111 | ||||||||||||||||||
| 400 | return qMakePair(QByteArray(), QByteArray()); //'=' is required for name-value-pair (RFC6265 section 5.2, rule 2) executed 34 times by 2 tests: return qMakePair(QByteArray(), QByteArray());Executed by:
| 34 | ||||||||||||||||||
| 401 | equalsPosition = semiColonPosition; //no '=' means there is an attribute-name but no attribute-value | - | ||||||||||||||||||
| 402 | } executed 111 times by 3 tests: end of blockExecuted by:
| 111 | ||||||||||||||||||
| 403 | - | |||||||||||||||||||
| 404 | QByteArray first = text.mid(position, equalsPosition - position).trimmed(); | - | ||||||||||||||||||
| 405 | QByteArray second; | - | ||||||||||||||||||
| 406 | int secondLength = semiColonPosition - equalsPosition - 1; | - | ||||||||||||||||||
| 407 | if (secondLength > 0)
| 140-1237 | ||||||||||||||||||
| 408 | second = text.mid(equalsPosition + 1, secondLength).trimmed(); executed 1237 times by 5 tests: second = text.mid(equalsPosition + 1, secondLength).trimmed();Executed by:
| 1237 | ||||||||||||||||||
| 409 | - | |||||||||||||||||||
| 410 | position = semiColonPosition; | - | ||||||||||||||||||
| 411 | return qMakePair(first, second); executed 1377 times by 5 tests: return qMakePair(first, second);Executed by:
| 1377 | ||||||||||||||||||
| 412 | } | - | ||||||||||||||||||
| 413 | - | |||||||||||||||||||
| 414 | /*! | - | ||||||||||||||||||
| 415 | \enum QNetworkCookie::RawForm | - | ||||||||||||||||||
| 416 | - | |||||||||||||||||||
| 417 | This enum is used with the toRawForm() function to declare which | - | ||||||||||||||||||
| 418 | form of a cookie shall be returned. | - | ||||||||||||||||||
| 419 | - | |||||||||||||||||||
| 420 | \value NameAndValueOnly makes toRawForm() return only the | - | ||||||||||||||||||
| 421 | "NAME=VALUE" part of the cookie, as suitable for sending back | - | ||||||||||||||||||
| 422 | to a server in a client request's "Cookie:" header. Multiple | - | ||||||||||||||||||
| 423 | cookies are separated by a semi-colon in the "Cookie:" header | - | ||||||||||||||||||
| 424 | field. | - | ||||||||||||||||||
| 425 | - | |||||||||||||||||||
| 426 | \value Full makes toRawForm() return the full | - | ||||||||||||||||||
| 427 | cookie contents, as suitable for sending to a client in a | - | ||||||||||||||||||
| 428 | server's "Set-Cookie:" header. | - | ||||||||||||||||||
| 429 | - | |||||||||||||||||||
| 430 | Note that only the Full form of the cookie can be parsed back into | - | ||||||||||||||||||
| 431 | its original contents. | - | ||||||||||||||||||
| 432 | - | |||||||||||||||||||
| 433 | \sa toRawForm(), parseCookies() | - | ||||||||||||||||||
| 434 | */ | - | ||||||||||||||||||
| 435 | - | |||||||||||||||||||
| 436 | /*! | - | ||||||||||||||||||
| 437 | Returns the raw form of this QNetworkCookie. The QByteArray | - | ||||||||||||||||||
| 438 | returned by this function is suitable for an HTTP header, either | - | ||||||||||||||||||
| 439 | in a server response (the Set-Cookie header) or the client request | - | ||||||||||||||||||
| 440 | (the Cookie header). You can choose from one of two formats, using | - | ||||||||||||||||||
| 441 | \a form. | - | ||||||||||||||||||
| 442 | - | |||||||||||||||||||
| 443 | \sa parseCookies() | - | ||||||||||||||||||
| 444 | */ | - | ||||||||||||||||||
| 445 | QByteArray QNetworkCookie::toRawForm(RawForm form) const | - | ||||||||||||||||||
| 446 | { | - | ||||||||||||||||||
| 447 | QByteArray result; | - | ||||||||||||||||||
| 448 | if (d->name.isEmpty())
| 6-1120 | ||||||||||||||||||
| 449 | return result; // not a valid cookie executed 6 times by 1 test: return result;Executed by:
| 6 | ||||||||||||||||||
| 450 | - | |||||||||||||||||||
| 451 | result = d->name; | - | ||||||||||||||||||
| 452 | result += '='; | - | ||||||||||||||||||
| 453 | result += d->value; | - | ||||||||||||||||||
| 454 | - | |||||||||||||||||||
| 455 | if (form == Full) {
| 14-1106 | ||||||||||||||||||
| 456 | // same as above, but encoding everything back | - | ||||||||||||||||||
| 457 | if (isSecure())
| 60-1046 | ||||||||||||||||||
| 458 | result += "; secure"; executed 60 times by 1 test: result += "; secure";Executed by:
| 60 | ||||||||||||||||||
| 459 | if (isHttpOnly())
| 40-1066 | ||||||||||||||||||
| 460 | result += "; HttpOnly"; executed 40 times by 1 test: result += "; HttpOnly";Executed by:
| 40 | ||||||||||||||||||
| 461 | if (!isSessionCookie()) {
| 501-605 | ||||||||||||||||||
| 462 | result += "; expires="; | - | ||||||||||||||||||
| 463 | result += QLocale::c().toString(d->expirationDate.toUTC(), | - | ||||||||||||||||||
| 464 | QLatin1String("ddd, dd-MMM-yyyy hh:mm:ss 'GMT")).toLatin1(); | - | ||||||||||||||||||
| 465 | } executed 605 times by 1 test: end of blockExecuted by:
| 605 | ||||||||||||||||||
| 466 | if (!d->domain.isEmpty()) {
| 172-934 | ||||||||||||||||||
| 467 | result += "; domain="; | - | ||||||||||||||||||
| 468 | if (d->domain.startsWith(QLatin1Char('.'))) {
| 50-122 | ||||||||||||||||||
| 469 | result += '.'; | - | ||||||||||||||||||
| 470 | result += QUrl::toAce(d->domain.mid(1)); | - | ||||||||||||||||||
| 471 | } else { executed 122 times by 3 tests: end of blockExecuted by:
| 122 | ||||||||||||||||||
| 472 | QHostAddress hostAddr(d->domain); | - | ||||||||||||||||||
| 473 | if (hostAddr.protocol() == QAbstractSocket::IPv6Protocol) {
| 1-49 | ||||||||||||||||||
| 474 | result += '['; | - | ||||||||||||||||||
| 475 | result += d->domain.toUtf8(); | - | ||||||||||||||||||
| 476 | result += ']'; | - | ||||||||||||||||||
| 477 | } else { executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 478 | result += QUrl::toAce(d->domain); | - | ||||||||||||||||||
| 479 | } executed 49 times by 3 tests: end of blockExecuted by:
| 49 | ||||||||||||||||||
| 480 | } | - | ||||||||||||||||||
| 481 | } | - | ||||||||||||||||||
| 482 | if (!d->path.isEmpty()) {
| 219-887 | ||||||||||||||||||
| 483 | result += "; path="; | - | ||||||||||||||||||
| 484 | result += d->path.toUtf8(); | - | ||||||||||||||||||
| 485 | } executed 219 times by 4 tests: end of blockExecuted by:
| 219 | ||||||||||||||||||
| 486 | } executed 1106 times by 4 tests: end of blockExecuted by:
| 1106 | ||||||||||||||||||
| 487 | return result; executed 1120 times by 4 tests: return result;Executed by:
| 1120 | ||||||||||||||||||
| 488 | } | - | ||||||||||||||||||
| 489 | - | |||||||||||||||||||
| 490 | static const char zones[] = | - | ||||||||||||||||||
| 491 | "pst\0" // -8 | - | ||||||||||||||||||
| 492 | "pdt\0" | - | ||||||||||||||||||
| 493 | "mst\0" // -7 | - | ||||||||||||||||||
| 494 | "mdt\0" | - | ||||||||||||||||||
| 495 | "cst\0" // -6 | - | ||||||||||||||||||
| 496 | "cdt\0" | - | ||||||||||||||||||
| 497 | "est\0" // -5 | - | ||||||||||||||||||
| 498 | "edt\0" | - | ||||||||||||||||||
| 499 | "ast\0" // -4 | - | ||||||||||||||||||
| 500 | "nst\0" // -3 | - | ||||||||||||||||||
| 501 | "gmt\0" // 0 | - | ||||||||||||||||||
| 502 | "utc\0" | - | ||||||||||||||||||
| 503 | "bst\0" | - | ||||||||||||||||||
| 504 | "met\0" // 1 | - | ||||||||||||||||||
| 505 | "eet\0" // 2 | - | ||||||||||||||||||
| 506 | "jst\0" // 9 | - | ||||||||||||||||||
| 507 | "\0"; | - | ||||||||||||||||||
| 508 | static const int zoneOffsets[] = {-8, -8, -7, -7, -6, -6, -5, -5, -4, -3, 0, 0, 0, 1, 2, 9 }; | - | ||||||||||||||||||
| 509 | - | |||||||||||||||||||
| 510 | static const char months[] = | - | ||||||||||||||||||
| 511 | "jan\0" | - | ||||||||||||||||||
| 512 | "feb\0" | - | ||||||||||||||||||
| 513 | "mar\0" | - | ||||||||||||||||||
| 514 | "apr\0" | - | ||||||||||||||||||
| 515 | "may\0" | - | ||||||||||||||||||
| 516 | "jun\0" | - | ||||||||||||||||||
| 517 | "jul\0" | - | ||||||||||||||||||
| 518 | "aug\0" | - | ||||||||||||||||||
| 519 | "sep\0" | - | ||||||||||||||||||
| 520 | "oct\0" | - | ||||||||||||||||||
| 521 | "nov\0" | - | ||||||||||||||||||
| 522 | "dec\0" | - | ||||||||||||||||||
| 523 | "\0"; | - | ||||||||||||||||||
| 524 | - | |||||||||||||||||||
| 525 | static inline bool isNumber(char s) | - | ||||||||||||||||||
| 526 | { executed 4731 times by 3 tests: return s >= '0' && s <= '9'; }return s >= '0' && s <= '9';Executed by:
executed 4731 times by 3 tests: return s >= '0' && s <= '9';Executed by:
| 4731 | ||||||||||||||||||
| 527 | - | |||||||||||||||||||
| 528 | static inline bool isTerminator(char c) | - | ||||||||||||||||||
| 529 | { executed 6332 times by 3 tests: return c == '\n' || c == '\r'; }return c == '\n' || c == '\r';Executed by:
executed 6332 times by 3 tests: return c == '\n' || c == '\r';Executed by:
| 6332 | ||||||||||||||||||
| 530 | - | |||||||||||||||||||
| 531 | static inline bool isValueSeparator(char c) | - | ||||||||||||||||||
| 532 | { executed 6332 times by 3 tests: return isTerminator(c) || c == ';'; }return isTerminator(c) || c == ';';Executed by:
executed 6332 times by 3 tests: return isTerminator(c) || c == ';';Executed by:
| 6332 | ||||||||||||||||||
| 533 | - | |||||||||||||||||||
| 534 | static inline bool isWhitespace(char c) | - | ||||||||||||||||||
| 535 | { executed 315 times by 3 tests: return c == ' ' || c == '\t'; }return c == ' ' || c == '\t';Executed by:
executed 315 times by 3 tests: return c == ' ' || c == '\t';Executed by:
| 315 | ||||||||||||||||||
| 536 | - | |||||||||||||||||||
| 537 | static bool checkStaticArray(int &val, const QByteArray &dateString, int at, const char *array, int size) | - | ||||||||||||||||||
| 538 | { | - | ||||||||||||||||||
| 539 | if (dateString[at] < 'a' || dateString[at] > 'z')
| 0-2637 | ||||||||||||||||||
| 540 | return false; executed 2637 times by 3 tests: return false;Executed by:
| 2637 | ||||||||||||||||||
| 541 | if (val == -1 && dateString.length() >= at + 3) {
| 10-1581 | ||||||||||||||||||
| 542 | int j = 0; | - | ||||||||||||||||||
| 543 | int i = 0; | - | ||||||||||||||||||
| 544 | while (i <= size) {
| 1156-21501 | ||||||||||||||||||
| 545 | const char *str = array + i; | - | ||||||||||||||||||
| 546 | if (str[0] == dateString[at]
| 1149-20352 | ||||||||||||||||||
| 547 | && str[1] == dateString[at + 1]
| 460-689 | ||||||||||||||||||
| 548 | && str[2] == dateString[at + 2]) {
| 45-415 | ||||||||||||||||||
| 549 | val = j; | - | ||||||||||||||||||
| 550 | return true; executed 415 times by 3 tests: return true;Executed by:
| 415 | ||||||||||||||||||
| 551 | } | - | ||||||||||||||||||
| 552 | i += int(strlen(str)) + 1; | - | ||||||||||||||||||
| 553 | ++j; | - | ||||||||||||||||||
| 554 | } executed 21086 times by 3 tests: end of blockExecuted by:
| 21086 | ||||||||||||||||||
| 555 | } executed 1156 times by 3 tests: end of blockExecuted by:
| 1156 | ||||||||||||||||||
| 556 | return false; executed 1385 times by 3 tests: return false;Executed by:
| 1385 | ||||||||||||||||||
| 557 | } | - | ||||||||||||||||||
| 558 | - | |||||||||||||||||||
| 559 | //#define PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 560 | - | |||||||||||||||||||
| 561 | #define ADAY 1 | - | ||||||||||||||||||
| 562 | #define AMONTH 2 | - | ||||||||||||||||||
| 563 | #define AYEAR 4 | - | ||||||||||||||||||
| 564 | - | |||||||||||||||||||
| 565 | /* | - | ||||||||||||||||||
| 566 | Parse all the date formats that Firefox can. | - | ||||||||||||||||||
| 567 | - | |||||||||||||||||||
| 568 | The official format is: | - | ||||||||||||||||||
| 569 | expires=ddd(d)?, dd-MMM-yyyy hh:mm:ss GMT | - | ||||||||||||||||||
| 570 | - | |||||||||||||||||||
| 571 | But browsers have been supporting a very wide range of date | - | ||||||||||||||||||
| 572 | strings. To work on many sites we need to support more then | - | ||||||||||||||||||
| 573 | just the official date format. | - | ||||||||||||||||||
| 574 | - | |||||||||||||||||||
| 575 | For reference see Firefox's PR_ParseTimeStringToExplodedTime in | - | ||||||||||||||||||
| 576 | prtime.c. The Firefox date parser is coded in a very complex way | - | ||||||||||||||||||
| 577 | and is slightly over ~700 lines long. While this implementation | - | ||||||||||||||||||
| 578 | will be slightly slower for the non standard dates it is smaller, | - | ||||||||||||||||||
| 579 | more readable, and maintainable. | - | ||||||||||||||||||
| 580 | - | |||||||||||||||||||
| 581 | Or in their own words: | - | ||||||||||||||||||
| 582 | "} // else what the hell is this." | - | ||||||||||||||||||
| 583 | */ | - | ||||||||||||||||||
| 584 | static QDateTime parseDateString(const QByteArray &dateString) | - | ||||||||||||||||||
| 585 | { | - | ||||||||||||||||||
| 586 | QTime time; | - | ||||||||||||||||||
| 587 | // placeholders for values when we are not sure it is a year, month or day | - | ||||||||||||||||||
| 588 | int unknown[3] = {-1, -1, -1}; | - | ||||||||||||||||||
| 589 | int month = -1; | - | ||||||||||||||||||
| 590 | int day = -1; | - | ||||||||||||||||||
| 591 | int year = -1; | - | ||||||||||||||||||
| 592 | int zoneOffset = -1; | - | ||||||||||||||||||
| 593 | - | |||||||||||||||||||
| 594 | // hour:minute:second.ms pm | - | ||||||||||||||||||
| 595 | QRegExp timeRx(QLatin1String("(\\d{1,2}):(\\d{1,2})(:(\\d{1,2})|)(\\.(\\d{1,3})|)((\\s{0,}(am|pm))|)")); | - | ||||||||||||||||||
| 596 | - | |||||||||||||||||||
| 597 | int at = 0; | - | ||||||||||||||||||
| 598 | while (at < dateString.length()) {
| 267-3176 | ||||||||||||||||||
| 599 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 600 | qDebug() << dateString.mid(at); | - | ||||||||||||||||||
| 601 | #endif | - | ||||||||||||||||||
| 602 | bool isNum = isNumber(dateString[at]); | - | ||||||||||||||||||
| 603 | - | |||||||||||||||||||
| 604 | // Month | - | ||||||||||||||||||
| 605 | if (!isNum
| 822-2354 | ||||||||||||||||||
| 606 | && checkStaticArray(month, dateString, at, months, sizeof(months)- 1)) {
| 229-2125 | ||||||||||||||||||
| 607 | ++month; | - | ||||||||||||||||||
| 608 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 609 | qDebug() << "Month:" << month; | - | ||||||||||||||||||
| 610 | #endif | - | ||||||||||||||||||
| 611 | at += 3; | - | ||||||||||||||||||
| 612 | continue; executed 229 times by 3 tests: continue;Executed by:
| 229 | ||||||||||||||||||
| 613 | } | - | ||||||||||||||||||
| 614 | // Zone | - | ||||||||||||||||||
| 615 | if (!isNum
| 822-2125 | ||||||||||||||||||
| 616 | && zoneOffset == -1
| 42-2083 | ||||||||||||||||||
| 617 | && checkStaticArray(zoneOffset, dateString, at, zones, sizeof(zones)- 1)) {
| 186-1897 | ||||||||||||||||||
| 618 | int sign = (at >= 0 && dateString[at - 1] == '-') ? -1 : 1;
| 0-186 | ||||||||||||||||||
| 619 | zoneOffset = sign * zoneOffsets[zoneOffset] * 60 * 60; | - | ||||||||||||||||||
| 620 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 621 | qDebug() << "Zone:" << month; | - | ||||||||||||||||||
| 622 | #endif | - | ||||||||||||||||||
| 623 | at += 3; | - | ||||||||||||||||||
| 624 | continue; executed 186 times by 3 tests: continue;Executed by:
| 186 | ||||||||||||||||||
| 625 | } | - | ||||||||||||||||||
| 626 | // Zone offset | - | ||||||||||||||||||
| 627 | if (!isNum
| 822-1939 | ||||||||||||||||||
| 628 | && (zoneOffset == -1 || zoneOffset == 0) // Can only go after gmt
| 15-1897 | ||||||||||||||||||
| 629 | && (dateString[at] == '+' || dateString[at] == '-')
| 9-1915 | ||||||||||||||||||
| 630 | && (at == 0
| 0-315 | ||||||||||||||||||
| 631 | || isWhitespace(dateString[at - 1])
| 7-308 | ||||||||||||||||||
| 632 | || dateString[at - 1] == ','
| 0-308 | ||||||||||||||||||
| 633 | || (at >= 3
| 7-301 | ||||||||||||||||||
| 634 | && (dateString[at - 3] == 'g')
| 5-296 | ||||||||||||||||||
| 635 | && (dateString[at - 2] == 'm')
| 0-5 | ||||||||||||||||||
| 636 | && (dateString[at - 1] == 't')))) {
| 0-5 | ||||||||||||||||||
| 637 | - | |||||||||||||||||||
| 638 | int end = 1; | - | ||||||||||||||||||
| 639 | while (end < 5 && dateString.length() > at+end
| 5-38 | ||||||||||||||||||
| 640 | && dateString[at + end] >= '0' && dateString[at + end] <= '9')
| 0-31 | ||||||||||||||||||
| 641 | ++end; executed 31 times by 1 test: ++end;Executed by:
| 31 | ||||||||||||||||||
| 642 | int minutes = 0; | - | ||||||||||||||||||
| 643 | int hours = 0; | - | ||||||||||||||||||
| 644 | switch (end - 1) { | - | ||||||||||||||||||
| 645 | case 4: executed 5 times by 1 test: case 4:Executed by:
| 5 | ||||||||||||||||||
| 646 | minutes = atoi(dateString.mid(at + 3, 2).constData()); | - | ||||||||||||||||||
| 647 | // fall through | - | ||||||||||||||||||
| 648 | case 2: code before this statement executed 5 times by 1 test: case 2:Executed by:
executed 2 times by 1 test: case 2:Executed by:
| 2-5 | ||||||||||||||||||
| 649 | hours = atoi(dateString.mid(at + 1, 2).constData()); | - | ||||||||||||||||||
| 650 | break; executed 7 times by 1 test: break;Executed by:
| 7 | ||||||||||||||||||
| 651 | case 1: executed 4 times by 1 test: case 1:Executed by:
| 4 | ||||||||||||||||||
| 652 | hours = atoi(dateString.mid(at + 1, 1).constData()); | - | ||||||||||||||||||
| 653 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||||||||
| 654 | default: executed 1 time by 1 test: default:Executed by:
| 1 | ||||||||||||||||||
| 655 | at += end; | - | ||||||||||||||||||
| 656 | continue; executed 1 time by 1 test: continue;Executed by:
| 1 | ||||||||||||||||||
| 657 | } | - | ||||||||||||||||||
| 658 | if (end != 1) {
| 0-11 | ||||||||||||||||||
| 659 | int sign = dateString[at] == '-' ? -1 : 1;
| 3-8 | ||||||||||||||||||
| 660 | zoneOffset = sign * ((minutes * 60) + (hours * 60 * 60)); | - | ||||||||||||||||||
| 661 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 662 | qDebug() << "Zone offset:" << zoneOffset << hours << minutes; | - | ||||||||||||||||||
| 663 | #endif | - | ||||||||||||||||||
| 664 | at += end; | - | ||||||||||||||||||
| 665 | continue; executed 11 times by 1 test: continue;Executed by:
| 11 | ||||||||||||||||||
| 666 | } | - | ||||||||||||||||||
| 667 | } never executed: end of block | 0 | ||||||||||||||||||
| 668 | - | |||||||||||||||||||
| 669 | // Time | - | ||||||||||||||||||
| 670 | if (isNum && time.isNull()
| 17-1927 | ||||||||||||||||||
| 671 | && dateString.length() >= at + 3
| 1-804 | ||||||||||||||||||
| 672 | && (dateString[at + 2] == ':' || dateString[at + 1] == ':')) {
| 78-620 | ||||||||||||||||||
| 673 | // While the date can be found all over the string the format | - | ||||||||||||||||||
| 674 | // for the time is set and a nice regexp can be used. | - | ||||||||||||||||||
| 675 | int pos = timeRx.indexIn(QLatin1String(dateString), at); | - | ||||||||||||||||||
| 676 | if (pos != -1) {
| 0-262 | ||||||||||||||||||
| 677 | QStringList list = timeRx.capturedTexts(); | - | ||||||||||||||||||
| 678 | int h = atoi(list.at(1).toLatin1().constData()); | - | ||||||||||||||||||
| 679 | int m = atoi(list.at(2).toLatin1().constData()); | - | ||||||||||||||||||
| 680 | int s = atoi(list.at(4).toLatin1().constData()); | - | ||||||||||||||||||
| 681 | int ms = atoi(list.at(6).toLatin1().constData()); | - | ||||||||||||||||||
| 682 | if (h < 12 && !list.at(9).isEmpty())
| 8-199 | ||||||||||||||||||
| 683 | if (list.at(9) == QLatin1String("pm"))
| 2-6 | ||||||||||||||||||
| 684 | h += 12; executed 6 times by 1 test: h += 12;Executed by:
| 6 | ||||||||||||||||||
| 685 | time = QTime(h, m, s, ms); | - | ||||||||||||||||||
| 686 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 687 | qDebug() << "Time:" << list << timeRx.matchedLength(); | - | ||||||||||||||||||
| 688 | #endif | - | ||||||||||||||||||
| 689 | at += timeRx.matchedLength(); | - | ||||||||||||||||||
| 690 | continue; executed 262 times by 3 tests: continue;Executed by:
| 262 | ||||||||||||||||||
| 691 | } | - | ||||||||||||||||||
| 692 | } never executed: end of block | 0 | ||||||||||||||||||
| 693 | - | |||||||||||||||||||
| 694 | // 4 digit Year | - | ||||||||||||||||||
| 695 | if (isNum
| 560-1927 | ||||||||||||||||||
| 696 | && year == -1
| 22-538 | ||||||||||||||||||
| 697 | && dateString.length() > at + 3) {
| 4-534 | ||||||||||||||||||
| 698 | if (isNumber(dateString[at + 1])
| 71-463 | ||||||||||||||||||
| 699 | && isNumber(dateString[at + 2])
| 166-297 | ||||||||||||||||||
| 700 | && isNumber(dateString[at + 3])) {
| 0-166 | ||||||||||||||||||
| 701 | year = atoi(dateString.mid(at, 4).constData()); | - | ||||||||||||||||||
| 702 | at += 4; | - | ||||||||||||||||||
| 703 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 704 | qDebug() << "Year:" << year; | - | ||||||||||||||||||
| 705 | #endif | - | ||||||||||||||||||
| 706 | continue; executed 166 times by 2 tests: continue;Executed by:
| 166 | ||||||||||||||||||
| 707 | } | - | ||||||||||||||||||
| 708 | } executed 368 times by 3 tests: end of blockExecuted by:
| 368 | ||||||||||||||||||
| 709 | - | |||||||||||||||||||
| 710 | // a one or two digit number | - | ||||||||||||||||||
| 711 | // Could be month, day or year | - | ||||||||||||||||||
| 712 | if (isNum) {
| 394-1927 | ||||||||||||||||||
| 713 | int length = 1; | - | ||||||||||||||||||
| 714 | if (dateString.length() > at + 1
| 2-392 | ||||||||||||||||||
| 715 | && isNumber(dateString[at + 1]))
| 78-314 | ||||||||||||||||||
| 716 | ++length; executed 314 times by 3 tests: ++length;Executed by:
| 314 | ||||||||||||||||||
| 717 | int x = atoi(dateString.mid(at, length).constData()); | - | ||||||||||||||||||
| 718 | if (year == -1 && (x > 31 || x == 0)) {
| 2-372 | ||||||||||||||||||
| 719 | year = x; | - | ||||||||||||||||||
| 720 | } else { executed 72 times by 1 test: end of blockExecuted by:
| 72 | ||||||||||||||||||
| 721 | if (unknown[0] == -1) unknown[0] = x; executed 263 times by 3 tests: unknown[0] = x;Executed by:
| 59-263 | ||||||||||||||||||
| 722 | else if (unknown[1] == -1) unknown[1] = x; executed 41 times by 2 tests: unknown[1] = x;Executed by:
| 18-41 | ||||||||||||||||||
| 723 | else if (unknown[2] == -1) unknown[2] = x; executed 18 times by 1 test: unknown[2] = x;Executed by:
| 0-18 | ||||||||||||||||||
| 724 | } executed 322 times by 3 tests: end of blockExecuted by:
| 322 | ||||||||||||||||||
| 725 | at += length; | - | ||||||||||||||||||
| 726 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 727 | qDebug() << "Saving" << x; | - | ||||||||||||||||||
| 728 | #endif | - | ||||||||||||||||||
| 729 | continue; executed 394 times by 3 tests: continue;Executed by:
| 394 | ||||||||||||||||||
| 730 | } | - | ||||||||||||||||||
| 731 | - | |||||||||||||||||||
| 732 | // Unknown character, typically a weekday such as 'Mon' | - | ||||||||||||||||||
| 733 | ++at; | - | ||||||||||||||||||
| 734 | } executed 1927 times by 3 tests: end of blockExecuted by:
| 1927 | ||||||||||||||||||
| 735 | - | |||||||||||||||||||
| 736 | // Once we are done parsing the string take the digits in unknown | - | ||||||||||||||||||
| 737 | // and determine which is the unknown year/month/day | - | ||||||||||||||||||
| 738 | - | |||||||||||||||||||
| 739 | int couldBe[3] = { 0, 0, 0 }; | - | ||||||||||||||||||
| 740 | int unknownCount = 3; | - | ||||||||||||||||||
| 741 | for (int i = 0; i < unknownCount; ++i) {
| 267-571 | ||||||||||||||||||
| 742 | if (unknown[i] == -1) {
| 249-322 | ||||||||||||||||||
| 743 | couldBe[i] = ADAY | AYEAR | AMONTH; | - | ||||||||||||||||||
| 744 | unknownCount = i; | - | ||||||||||||||||||
| 745 | continue; executed 249 times by 3 tests: continue;Executed by:
| 249 | ||||||||||||||||||
| 746 | } | - | ||||||||||||||||||
| 747 | - | |||||||||||||||||||
| 748 | if (unknown[i] >= 1)
| 0-322 | ||||||||||||||||||
| 749 | couldBe[i] = ADAY; executed 322 times by 3 tests: couldBe[i] = 1;Executed by:
| 322 | ||||||||||||||||||
| 750 | - | |||||||||||||||||||
| 751 | if (month == -1 && unknown[i] >= 1 && unknown[i] <= 12)
| 0-236 | ||||||||||||||||||
| 752 | couldBe[i] |= AMONTH; executed 52 times by 1 test: couldBe[i] |= 2;Executed by:
| 52 | ||||||||||||||||||
| 753 | - | |||||||||||||||||||
| 754 | if (year == -1)
| 68-254 | ||||||||||||||||||
| 755 | couldBe[i] |= AYEAR; executed 68 times by 2 tests: couldBe[i] |= 4;Executed by:
| 68 | ||||||||||||||||||
| 756 | } executed 322 times by 3 tests: end of blockExecuted by:
| 322 | ||||||||||||||||||
| 757 | - | |||||||||||||||||||
| 758 | // For any possible day make sure one of the values that could be a month | - | ||||||||||||||||||
| 759 | // can contain that day. | - | ||||||||||||||||||
| 760 | // For any possible month make sure one of the values that can be a | - | ||||||||||||||||||
| 761 | // day that month can have. | - | ||||||||||||||||||
| 762 | // Example: 31 11 06 | - | ||||||||||||||||||
| 763 | // 31 can't be a day because 11 and 6 don't have 31 days | - | ||||||||||||||||||
| 764 | for (int i = 0; i < unknownCount; ++i) {
| 267-322 | ||||||||||||||||||
| 765 | int currentValue = unknown[i]; | - | ||||||||||||||||||
| 766 | bool findMatchingMonth = couldBe[i] & ADAY && currentValue >= 29;
| 0-322 | ||||||||||||||||||
| 767 | bool findMatchingDay = couldBe[i] & AMONTH; | - | ||||||||||||||||||
| 768 | if (!findMatchingMonth || !findMatchingDay)
| 0-296 | ||||||||||||||||||
| 769 | continue; executed 322 times by 3 tests: continue;Executed by:
| 322 | ||||||||||||||||||
| 770 | for (int j = 0; j < 3; ++j) {
| 0 | ||||||||||||||||||
| 771 | if (j == i)
| 0 | ||||||||||||||||||
| 772 | continue; never executed: continue; | 0 | ||||||||||||||||||
| 773 | for (int k = 0; k < 2; ++k) {
| 0 | ||||||||||||||||||
| 774 | if (k == 0 && !(findMatchingMonth && (couldBe[j] & AMONTH)))
| 0 | ||||||||||||||||||
| 775 | continue; never executed: continue; | 0 | ||||||||||||||||||
| 776 | else if (k == 1 && !(findMatchingDay && (couldBe[j] & ADAY)))
| 0 | ||||||||||||||||||
| 777 | continue; never executed: continue; | 0 | ||||||||||||||||||
| 778 | int m = currentValue; | - | ||||||||||||||||||
| 779 | int d = unknown[j]; | - | ||||||||||||||||||
| 780 | if (k == 0)
| 0 | ||||||||||||||||||
| 781 | qSwap(m, d); never executed: qSwap(m, d); | 0 | ||||||||||||||||||
| 782 | if (m == -1) m = month; never executed: m = month;
| 0 | ||||||||||||||||||
| 783 | bool found = true; | - | ||||||||||||||||||
| 784 | switch(m) { | - | ||||||||||||||||||
| 785 | case 2: never executed: case 2: | 0 | ||||||||||||||||||
| 786 | // When we get 29 and the year ends up having only 28 | - | ||||||||||||||||||
| 787 | // See date.isValid below | - | ||||||||||||||||||
| 788 | // Example: 29 23 Feb | - | ||||||||||||||||||
| 789 | if (d <= 29)
| 0 | ||||||||||||||||||
| 790 | found = false; never executed: found = false; | 0 | ||||||||||||||||||
| 791 | break; never executed: break; | 0 | ||||||||||||||||||
| 792 | case 4: case 6: case 9: case 11: never executed: case 4:never executed: case 6:never executed: case 9:never executed: case 11: | 0 | ||||||||||||||||||
| 793 | if (d <= 30)
| 0 | ||||||||||||||||||
| 794 | found = false; never executed: found = false; | 0 | ||||||||||||||||||
| 795 | break; never executed: break; | 0 | ||||||||||||||||||
| 796 | default: never executed: default: | 0 | ||||||||||||||||||
| 797 | if (d > 0 && d <= 31)
| 0 | ||||||||||||||||||
| 798 | found = false; never executed: found = false; | 0 | ||||||||||||||||||
| 799 | } never executed: end of block | 0 | ||||||||||||||||||
| 800 | if (k == 0) findMatchingMonth = found; never executed: findMatchingMonth = found;
| 0 | ||||||||||||||||||
| 801 | else if (k == 1) findMatchingDay = found; never executed: findMatchingDay = found;
| 0 | ||||||||||||||||||
| 802 | } never executed: end of block | 0 | ||||||||||||||||||
| 803 | } never executed: end of block | 0 | ||||||||||||||||||
| 804 | if (findMatchingMonth)
| 0 | ||||||||||||||||||
| 805 | couldBe[i] &= ~ADAY; never executed: couldBe[i] &= ~1; | 0 | ||||||||||||||||||
| 806 | if (findMatchingDay)
| 0 | ||||||||||||||||||
| 807 | couldBe[i] &= ~AMONTH; never executed: couldBe[i] &= ~2; | 0 | ||||||||||||||||||
| 808 | } never executed: end of block | 0 | ||||||||||||||||||
| 809 | - | |||||||||||||||||||
| 810 | // First set the year/month/day that have been deduced | - | ||||||||||||||||||
| 811 | // and reduce the set as we go along to deduce more | - | ||||||||||||||||||
| 812 | for (int i = 0; i < unknownCount; ++i) {
| 267-322 | ||||||||||||||||||
| 813 | int unset = 0; | - | ||||||||||||||||||
| 814 | for (int j = 0; j < 3; ++j) {
| 0-552 | ||||||||||||||||||
| 815 | if (couldBe[j] == ADAY && day == -1) {
| 2-320 | ||||||||||||||||||
| 816 | day = unknown[j]; | - | ||||||||||||||||||
| 817 | unset |= ADAY; | - | ||||||||||||||||||
| 818 | } else if (couldBe[j] == AMONTH && month == -1) { executed 230 times by 2 tests: end of blockExecuted by:
| 0-322 | ||||||||||||||||||
| 819 | month = unknown[j]; | - | ||||||||||||||||||
| 820 | unset |= AMONTH; | - | ||||||||||||||||||
| 821 | } else if (couldBe[j] == AYEAR && year == -1) { never executed: end of block
| 0-322 | ||||||||||||||||||
| 822 | year = unknown[j]; | - | ||||||||||||||||||
| 823 | unset |= AYEAR; | - | ||||||||||||||||||
| 824 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 825 | // common case | - | ||||||||||||||||||
| 826 | break; executed 322 times by 3 tests: break;Executed by:
| 322 | ||||||||||||||||||
| 827 | } | - | ||||||||||||||||||
| 828 | couldBe[j] &= ~unset; | - | ||||||||||||||||||
| 829 | } executed 230 times by 2 tests: end of blockExecuted by:
| 230 | ||||||||||||||||||
| 830 | } executed 322 times by 3 tests: end of blockExecuted by:
| 322 | ||||||||||||||||||
| 831 | - | |||||||||||||||||||
| 832 | // Now fallback to a standardized order to fill in the rest with | - | ||||||||||||||||||
| 833 | for (int i = 0; i < unknownCount; ++i) {
| 267-322 | ||||||||||||||||||
| 834 | if (couldBe[i] & AMONTH && month == -1) month = unknown[i]; executed 33 times by 1 test: month = unknown[i];Executed by:
| 19-270 | ||||||||||||||||||
| 835 | else if (couldBe[i] & ADAY && day == -1) day = unknown[i]; executed 33 times by 2 tests: day = unknown[i];Executed by:
| 26-230 | ||||||||||||||||||
| 836 | else if (couldBe[i] & AYEAR && year == -1) year = unknown[i]; executed 24 times by 2 tests: year = unknown[i];Executed by:
| 0-232 | ||||||||||||||||||
| 837 | } executed 322 times by 3 tests: end of blockExecuted by:
| 322 | ||||||||||||||||||
| 838 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 839 | qDebug() << "Final set" << year << month << day; | - | ||||||||||||||||||
| 840 | #endif | - | ||||||||||||||||||
| 841 | - | |||||||||||||||||||
| 842 | if (year == -1 || month == -1 || day == -1) {
| 0-262 | ||||||||||||||||||
| 843 | #ifdef PARSEDATESTRINGDEBUG | - | ||||||||||||||||||
| 844 | qDebug() << "Parser failure" << year << month << day; | - | ||||||||||||||||||
| 845 | #endif | - | ||||||||||||||||||
| 846 | return QDateTime(); executed 6 times by 1 test: return QDateTime();Executed by:
| 6 | ||||||||||||||||||
| 847 | } | - | ||||||||||||||||||
| 848 | - | |||||||||||||||||||
| 849 | // Y2k behavior | - | ||||||||||||||||||
| 850 | int y2k = 0; | - | ||||||||||||||||||
| 851 | if (year < 70)
| 27-234 | ||||||||||||||||||
| 852 | y2k = 2000; executed 27 times by 2 tests: y2k = 2000;Executed by:
| 27 | ||||||||||||||||||
| 853 | else if (year < 100)
| 68-166 | ||||||||||||||||||
| 854 | y2k = 1900; executed 68 times by 1 test: y2k = 1900;Executed by:
| 68 | ||||||||||||||||||
| 855 | - | |||||||||||||||||||
| 856 | QDate date(year + y2k, month, day); | - | ||||||||||||||||||
| 857 | - | |||||||||||||||||||
| 858 | // When we were given a bad cookie that when parsed | - | ||||||||||||||||||
| 859 | // set the day to 29 and the year to one that doesn't | - | ||||||||||||||||||
| 860 | // have the 29th of Feb rather then adding the extra | - | ||||||||||||||||||
| 861 | // complicated checking earlier just swap here. | - | ||||||||||||||||||
| 862 | // Example: 29 23 Feb | - | ||||||||||||||||||
| 863 | if (!date.isValid())
| 9-252 | ||||||||||||||||||
| 864 | date = QDate(day + y2k, month, year); executed 9 times by 1 test: date = QDate(day + y2k, month, year);Executed by:
| 9 | ||||||||||||||||||
| 865 | - | |||||||||||||||||||
| 866 | QDateTime dateTime(date, time, Qt::UTC); | - | ||||||||||||||||||
| 867 | - | |||||||||||||||||||
| 868 | if (zoneOffset != -1) {
| 70-191 | ||||||||||||||||||
| 869 | dateTime = dateTime.addSecs(zoneOffset); | - | ||||||||||||||||||
| 870 | } executed 191 times by 3 tests: end of blockExecuted by:
| 191 | ||||||||||||||||||
| 871 | if (!dateTime.isValid())
| 0-261 | ||||||||||||||||||
| 872 | return QDateTime(); never executed: return QDateTime(); | 0 | ||||||||||||||||||
| 873 | return dateTime; executed 261 times by 3 tests: return dateTime;Executed by:
| 261 | ||||||||||||||||||
| 874 | } | - | ||||||||||||||||||
| 875 | - | |||||||||||||||||||
| 876 | /*! | - | ||||||||||||||||||
| 877 | Parses the cookie string \a cookieString as received from a server | - | ||||||||||||||||||
| 878 | response in the "Set-Cookie:" header. If there's a parsing error, | - | ||||||||||||||||||
| 879 | this function returns an empty list. | - | ||||||||||||||||||
| 880 | - | |||||||||||||||||||
| 881 | Since the HTTP header can set more than one cookie at the same | - | ||||||||||||||||||
| 882 | time, this function returns a QList<QNetworkCookie>, one for each | - | ||||||||||||||||||
| 883 | cookie that is parsed. | - | ||||||||||||||||||
| 884 | - | |||||||||||||||||||
| 885 | \sa toRawForm() | - | ||||||||||||||||||
| 886 | */ | - | ||||||||||||||||||
| 887 | QList<QNetworkCookie> QNetworkCookie::parseCookies(const QByteArray &cookieString) | - | ||||||||||||||||||
| 888 | { | - | ||||||||||||||||||
| 889 | // cookieString can be a number of set-cookie header strings joined together | - | ||||||||||||||||||
| 890 | // by \n, parse each line separately. | - | ||||||||||||||||||
| 891 | QList<QNetworkCookie> cookies; | - | ||||||||||||||||||
| 892 | QList<QByteArray> list = cookieString.split('\n'); | - | ||||||||||||||||||
| 893 | for (int a = 0; a < list.size(); a++)
| 726-734 | ||||||||||||||||||
| 894 | cookies += QNetworkCookiePrivate::parseSetCookieHeaderLine(list.at(a)); executed 734 times by 5 tests: cookies += QNetworkCookiePrivate::parseSetCookieHeaderLine(list.at(a));Executed by:
| 734 | ||||||||||||||||||
| 895 | return cookies; executed 726 times by 5 tests: return cookies;Executed by:
| 726 | ||||||||||||||||||
| 896 | } | - | ||||||||||||||||||
| 897 | - | |||||||||||||||||||
| 898 | QList<QNetworkCookie> QNetworkCookiePrivate::parseSetCookieHeaderLine(const QByteArray &cookieString) | - | ||||||||||||||||||
| 899 | { | - | ||||||||||||||||||
| 900 | // According to http://wp.netscape.com/newsref/std/cookie_spec.html,< | - | ||||||||||||||||||
| 901 | // the Set-Cookie response header is of the format: | - | ||||||||||||||||||
| 902 | // | - | ||||||||||||||||||
| 903 | // Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure | - | ||||||||||||||||||
| 904 | // | - | ||||||||||||||||||
| 905 | // where only the NAME=VALUE part is mandatory | - | ||||||||||||||||||
| 906 | // | - | ||||||||||||||||||
| 907 | // We do not support RFC 2965 Set-Cookie2-style cookies | - | ||||||||||||||||||
| 908 | - | |||||||||||||||||||
| 909 | QList<QNetworkCookie> result; | - | ||||||||||||||||||
| 910 | const QDateTime now = QDateTime::currentDateTimeUtc(); | - | ||||||||||||||||||
| 911 | - | |||||||||||||||||||
| 912 | int position = 0; | - | ||||||||||||||||||
| 913 | const int length = cookieString.length(); | - | ||||||||||||||||||
| 914 | while (position < length) {
| 680-728 | ||||||||||||||||||
| 915 | QNetworkCookie cookie; | - | ||||||||||||||||||
| 916 | - | |||||||||||||||||||
| 917 | // The first part is always the "NAME=VALUE" part | - | ||||||||||||||||||
| 918 | QPair<QByteArray,QByteArray> field = nextField(cookieString, position, true); | - | ||||||||||||||||||
| 919 | if (field.first.isEmpty())
| 46-682 | ||||||||||||||||||
| 920 | // parsing error | - | ||||||||||||||||||
| 921 | break; executed 46 times by 2 tests: break;Executed by:
| 46 | ||||||||||||||||||
| 922 | cookie.setName(field.first); | - | ||||||||||||||||||
| 923 | cookie.setValue(field.second); | - | ||||||||||||||||||
| 924 | - | |||||||||||||||||||
| 925 | position = nextNonWhitespace(cookieString, position); | - | ||||||||||||||||||
| 926 | while (position < length) {
| 674-683 | ||||||||||||||||||
| 927 | switch (cookieString.at(position++)) { | - | ||||||||||||||||||
| 928 | case ';': executed 683 times by 4 tests: case ';':Executed by:
| 683 | ||||||||||||||||||
| 929 | // new field in the cookie | - | ||||||||||||||||||
| 930 | field = nextField(cookieString, position, false); | - | ||||||||||||||||||
| 931 | field.first = field.first.toLower(); // everything but the NAME=VALUE is case-insensitive | - | ||||||||||||||||||
| 932 | - | |||||||||||||||||||
| 933 | if (field.first == "expires") {
| 267-416 | ||||||||||||||||||
| 934 | position -= field.second.length(); | - | ||||||||||||||||||
| 935 | int end; | - | ||||||||||||||||||
| 936 | for (end = position; end < length; ++end)
| 245-6332 | ||||||||||||||||||
| 937 | if (isValueSeparator(cookieString.at(end)))
| 22-6310 | ||||||||||||||||||
| 938 | break; executed 22 times by 3 tests: break;Executed by:
| 22 | ||||||||||||||||||
| 939 | - | |||||||||||||||||||
| 940 | QByteArray dateString = cookieString.mid(position, end - position).trimmed(); | - | ||||||||||||||||||
| 941 | position = end; | - | ||||||||||||||||||
| 942 | QDateTime dt = parseDateString(dateString.toLower()); | - | ||||||||||||||||||
| 943 | if (dt.isValid())
| 6-261 | ||||||||||||||||||
| 944 | cookie.setExpirationDate(dt); executed 261 times by 3 tests: cookie.setExpirationDate(dt);Executed by:
| 261 | ||||||||||||||||||
| 945 | //if unparsed, ignore the attribute but not the whole cookie (RFC6265 section 5.2.1) | - | ||||||||||||||||||
| 946 | } else if (field.first == "domain") { executed 267 times by 3 tests: end of blockExecuted by:
| 122-294 | ||||||||||||||||||
| 947 | QByteArray rawDomain = field.second; | - | ||||||||||||||||||
| 948 | //empty domain should be ignored (RFC6265 section 5.2.3) | - | ||||||||||||||||||
| 949 | if (!rawDomain.isEmpty()) {
| 4-118 | ||||||||||||||||||
| 950 | QString maybeLeadingDot; | - | ||||||||||||||||||
| 951 | if (rawDomain.startsWith('.')) {
| 51-67 | ||||||||||||||||||
| 952 | maybeLeadingDot = QLatin1Char('.'); | - | ||||||||||||||||||
| 953 | rawDomain = rawDomain.mid(1); | - | ||||||||||||||||||
| 954 | } executed 67 times by 3 tests: end of blockExecuted by:
| 67 | ||||||||||||||||||
| 955 | - | |||||||||||||||||||
| 956 | //IDN domains are required by RFC6265, accepting utf8 as well doesn't break any test cases. | - | ||||||||||||||||||
| 957 | QString normalizedDomain = QUrl::fromAce(QUrl::toAce(QString::fromUtf8(rawDomain))); | - | ||||||||||||||||||
| 958 | if (!normalizedDomain.isEmpty()) {
| 8-110 | ||||||||||||||||||
| 959 | cookie.setDomain(maybeLeadingDot + normalizedDomain); | - | ||||||||||||||||||
| 960 | } else { executed 110 times by 3 tests: end of blockExecuted by:
| 110 | ||||||||||||||||||
| 961 | //Normalization fails for malformed domains, e.g. "..example.org", reject the cookie now | - | ||||||||||||||||||
| 962 | //rather than accepting it but never sending it due to domain match failure, as the | - | ||||||||||||||||||
| 963 | //strict reading of RFC6265 would indicate. | - | ||||||||||||||||||
| 964 | return result; executed 8 times by 2 tests: return result;Executed by:
| 8 | ||||||||||||||||||
| 965 | } | - | ||||||||||||||||||
| 966 | } | - | ||||||||||||||||||
| 967 | } else if (field.first == "max-age") { executed 114 times by 3 tests: end of blockExecuted by:
| 21-273 | ||||||||||||||||||
| 968 | bool ok = false; | - | ||||||||||||||||||
| 969 | int secs = field.second.toInt(&ok); | - | ||||||||||||||||||
| 970 | if (ok) {
| 2-19 | ||||||||||||||||||
| 971 | if (secs <= 0) {
| 8-11 | ||||||||||||||||||
| 972 | //earliest representable time (RFC6265 section 5.2.2) | - | ||||||||||||||||||
| 973 | cookie.setExpirationDate(QDateTime::fromTime_t(0)); | - | ||||||||||||||||||
| 974 | } else { executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||||||||
| 975 | cookie.setExpirationDate(now.addSecs(secs)); | - | ||||||||||||||||||
| 976 | } executed 11 times by 2 tests: end of blockExecuted by:
| 11 | ||||||||||||||||||
| 977 | } | - | ||||||||||||||||||
| 978 | //if unparsed, ignore the attribute but not the whole cookie (RFC6265 section 5.2.2) | - | ||||||||||||||||||
| 979 | } else if (field.first == "path") { executed 21 times by 2 tests: end of blockExecuted by:
| 21-145 | ||||||||||||||||||
| 980 | if (field.second.startsWith('/')) {
| 10-135 | ||||||||||||||||||
| 981 | // ### we should treat cookie paths as an octet sequence internally | - | ||||||||||||||||||
| 982 | // However RFC6265 says we should assume UTF-8 for presentation as a string | - | ||||||||||||||||||
| 983 | cookie.setPath(QString::fromUtf8(field.second)); | - | ||||||||||||||||||
| 984 | } else { executed 135 times by 4 tests: end of blockExecuted by:
| 135 | ||||||||||||||||||
| 985 | // if the path doesn't start with '/' then set the default path (RFC6265 section 5.2.4) | - | ||||||||||||||||||
| 986 | // and also IETF test case path0030 which has valid and empty path in the same cookie | - | ||||||||||||||||||
| 987 | cookie.setPath(QString()); | - | ||||||||||||||||||
| 988 | } executed 10 times by 2 tests: end of blockExecuted by:
| 10 | ||||||||||||||||||
| 989 | } else if (field.first == "secure") {
| 38-90 | ||||||||||||||||||
| 990 | cookie.setSecure(true); | - | ||||||||||||||||||
| 991 | } else if (field.first == "httponly") { executed 38 times by 2 tests: end of blockExecuted by:
| 17-73 | ||||||||||||||||||
| 992 | cookie.setHttpOnly(true); | - | ||||||||||||||||||
| 993 | } else { executed 17 times by 2 tests: end of blockExecuted by:
| 17 | ||||||||||||||||||
| 994 | // ignore unknown fields in the cookie (RFC6265 section 5.2, rule 6) | - | ||||||||||||||||||
| 995 | } executed 73 times by 2 tests: end of blockExecuted by:
| 73 | ||||||||||||||||||
| 996 | - | |||||||||||||||||||
| 997 | position = nextNonWhitespace(cookieString, position); | - | ||||||||||||||||||
| 998 | } executed 675 times by 4 tests: end of blockExecuted by:
| 675 | ||||||||||||||||||
| 999 | } executed 675 times by 4 tests: end of blockExecuted by:
| 675 | ||||||||||||||||||
| 1000 | - | |||||||||||||||||||
| 1001 | if (!cookie.name().isEmpty())
| 0-674 | ||||||||||||||||||
| 1002 | result += cookie; executed 674 times by 5 tests: result += cookie;Executed by:
| 674 | ||||||||||||||||||
| 1003 | } executed 674 times by 5 tests: end of blockExecuted by:
| 674 | ||||||||||||||||||
| 1004 | - | |||||||||||||||||||
| 1005 | return result; executed 726 times by 5 tests: return result;Executed by:
| 726 | ||||||||||||||||||
| 1006 | } | - | ||||||||||||||||||
| 1007 | - | |||||||||||||||||||
| 1008 | /*! | - | ||||||||||||||||||
| 1009 | \since 5.0 | - | ||||||||||||||||||
| 1010 | This functions normalizes the path and domain of the cookie if they were previously empty. | - | ||||||||||||||||||
| 1011 | The \a url parameter is used to determine the correct domain and path. | - | ||||||||||||||||||
| 1012 | */ | - | ||||||||||||||||||
| 1013 | void QNetworkCookie::normalize(const QUrl &url) | - | ||||||||||||||||||
| 1014 | { | - | ||||||||||||||||||
| 1015 | // don't do path checking. See QTBUG-5815 | - | ||||||||||||||||||
| 1016 | if (d->path.isEmpty()) {
| 73-183 | ||||||||||||||||||
| 1017 | QString pathAndFileName = url.path(); | - | ||||||||||||||||||
| 1018 | QString defaultPath = pathAndFileName.left(pathAndFileName.lastIndexOf(QLatin1Char('/'))+1); | - | ||||||||||||||||||
| 1019 | if (defaultPath.isEmpty())
| 3-180 | ||||||||||||||||||
| 1020 | defaultPath = QLatin1Char('/'); executed 3 times by 1 test: defaultPath = QLatin1Char('/');Executed by:
| 3 | ||||||||||||||||||
| 1021 | d->path = defaultPath; | - | ||||||||||||||||||
| 1022 | } executed 183 times by 2 tests: end of blockExecuted by:
| 183 | ||||||||||||||||||
| 1023 | - | |||||||||||||||||||
| 1024 | if (d->domain.isEmpty()) {
| 65-191 | ||||||||||||||||||
| 1025 | d->domain = url.host(); | - | ||||||||||||||||||
| 1026 | } else { executed 191 times by 2 tests: end of blockExecuted by:
| 191 | ||||||||||||||||||
| 1027 | QHostAddress hostAddress(d->domain); | - | ||||||||||||||||||
| 1028 | if (hostAddress.protocol() != QAbstractSocket::IPv4Protocol
| 1-64 | ||||||||||||||||||
| 1029 | && hostAddress.protocol() != QAbstractSocket::IPv6Protocol
| 1-63 | ||||||||||||||||||
| 1030 | && !d->domain.startsWith(QLatin1Char('.'))) {
| 27-36 | ||||||||||||||||||
| 1031 | // Ensure the domain starts with a dot if its field was not empty | - | ||||||||||||||||||
| 1032 | // in the HTTP header. There are some servers that forget the | - | ||||||||||||||||||
| 1033 | // leading dot and this is actually forbidden according to RFC 2109, | - | ||||||||||||||||||
| 1034 | // but all browsers accept it anyway so we do that as well. | - | ||||||||||||||||||
| 1035 | d->domain.prepend(QLatin1Char('.')); | - | ||||||||||||||||||
| 1036 | } executed 27 times by 1 test: end of blockExecuted by:
| 27 | ||||||||||||||||||
| 1037 | } executed 65 times by 2 tests: end of blockExecuted by:
| 65 | ||||||||||||||||||
| 1038 | } | - | ||||||||||||||||||
| 1039 | - | |||||||||||||||||||
| 1040 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||||||||
| 1041 | QDebug operator<<(QDebug s, const QNetworkCookie &cookie) | - | ||||||||||||||||||
| 1042 | { | - | ||||||||||||||||||
| 1043 | QDebugStateSaver saver(s); | - | ||||||||||||||||||
| 1044 | s.resetFormat().nospace(); | - | ||||||||||||||||||
| 1045 | s << "QNetworkCookie(" << cookie.toRawForm(QNetworkCookie::Full) << ')'; | - | ||||||||||||||||||
| 1046 | return s; never executed: return s; | 0 | ||||||||||||||||||
| 1047 | } | - | ||||||||||||||||||
| 1048 | #endif | - | ||||||||||||||||||
| 1049 | - | |||||||||||||||||||
| 1050 | QT_END_NAMESPACE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |