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