Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qabstractnetworkcache.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
9
10#include <qdatastream.h>
11#include <qdatetime.h>
12#include <qurl.h>
13#include <qhash.h>
14
15#include <qdebug.h>
16
18
20{
21
22public:
27
28 bool operator==(const QNetworkCacheMetaDataPrivate &other) const
29 {
30 return
31 url == other.url
32 && lastModified == other.lastModified
33 && expirationDate == other.expirationDate
34 && saveToDisk == other.saveToDisk
35 && QHttpHeadersHelper::compareStrict(headers, other.headers);
36 }
37
38 QUrl url;
39 QDateTime lastModified;
40 QDateTime expirationDate;
44
45 static void save(QDataStream &out, const QNetworkCacheMetaData &metaData);
46 static void load(QDataStream &in, QNetworkCacheMetaData &metaData);
47};
48Q_GLOBAL_STATIC(QNetworkCacheMetaDataPrivate, metadata_shared_invalid)
49
50/*!
51 \class QNetworkCacheMetaData
52 \since 4.5
53 \ingroup shared
54 \inmodule QtNetwork
55
56 \brief The QNetworkCacheMetaData class provides cache information.
57
58 QNetworkCacheMetaData provides information about a cache file including
59 the url, when it was last modified, when the cache file was created, headers
60 for file and if the file should be saved onto a disk.
61
62 \sa QAbstractNetworkCache
63*/
64
65/*!
66 \typedef QNetworkCacheMetaData::RawHeader
67
68 Synonym for std::pair<QByteArray, QByteArray>
69*/
70
71/*!
72 \typedef QNetworkCacheMetaData::RawHeaderList
73
74 Synonym for QList<RawHeader>
75*/
76
77/*!
78 \typedef QNetworkCacheMetaData::AttributesMap
79
80 Synonym for QHash<QNetworkRequest::Attribute, QVariant>
81*/
82
83/*!
84 Constructs an invalid network cache meta data.
85
86 \sa isValid()
87 */
88QNetworkCacheMetaData::QNetworkCacheMetaData()
89 : d(new QNetworkCacheMetaDataPrivate)
90{
91}
92
93/*!
94 Destroys the network cache meta data.
95 */
96QNetworkCacheMetaData::~QNetworkCacheMetaData()
97{
98 // QSharedDataPointer takes care of freeing d
99}
100
101/*!
102 Constructs a copy of the \a other QNetworkCacheMetaData.
103 */
104QNetworkCacheMetaData::QNetworkCacheMetaData(const QNetworkCacheMetaData &other)
105 : d(other.d)
106{
107}
108
109/*!
110 Makes a copy of the \a other QNetworkCacheMetaData and returns a reference to the copy.
111 */
112QNetworkCacheMetaData &QNetworkCacheMetaData::operator=(const QNetworkCacheMetaData &other)
113{
114 d = other.d;
115 return *this;
116}
117
118/*!
119 \fn void QNetworkCacheMetaData::swap(QNetworkCacheMetaData &other)
120 \since 5.0
121 \memberswap{metadata instance}
122 */
123
124/*!
125 Returns \c true if this meta data is equal to the \a other meta data; otherwise returns \c false.
126
127 \sa operator!=()
128 */
129bool QNetworkCacheMetaData::operator==(const QNetworkCacheMetaData &other) const
130{
131 if (d == other.d)
132 return true;
133 if (d && other.d)
134 return *d == *other.d;
135 return false;
136}
137
138/*!
139 \fn bool QNetworkCacheMetaData::operator!=(const QNetworkCacheMetaData &other) const
140
141 Returns \c true if this meta data is not equal to the \a other meta data; otherwise returns \c false.
142
143 \sa operator==()
144 */
145
146/*!
147 Returns \c true if this network cache meta data has attributes that have been set otherwise false.
148 */
149bool QNetworkCacheMetaData::isValid() const
150{
151 return !(*d == *metadata_shared_invalid());
152}
153
154/*!
155 Returns is this cache should be allowed to be stored on disk.
156
157 Some cache implementations can keep these cache items in memory for performance reasons,
158 but for security reasons they should not be written to disk.
159
160 Specifically with http, documents with Cache-control set to no-store or any
161 https document that doesn't have "Cache-control: public" set will
162 set the saveToDisk to false.
163
164 \sa setSaveToDisk()
165 */
166bool QNetworkCacheMetaData::saveToDisk() const
167{
168 return d->saveToDisk;
169}
170
171/*!
172 Sets whether this network cache meta data and associated content should be
173 allowed to be stored on disk to \a allow.
174
175 \sa saveToDisk()
176 */
177void QNetworkCacheMetaData::setSaveToDisk(bool allow)
178{
179 d->saveToDisk = allow;
180}
181
182/*!
183 Returns the URL this network cache meta data is referring to.
184
185 \sa setUrl()
186 */
187QUrl QNetworkCacheMetaData::url() const
188{
189 return d->url;
190}
191
192/*!
193 Sets the URL this network cache meta data to be \a url.
194
195 The password and fragment are removed from the url.
196
197 \sa url()
198 */
199void QNetworkCacheMetaData::setUrl(const QUrl &url)
200{
201 auto *p = d.data();
202 p->url = url;
203 p->url.setPassword(QString());
204 p->url.setFragment(QString());
205}
206
207/*!
208 Returns a list of all raw headers that are set in this meta data.
209 The list is in the same order that the headers were set.
210
211 \sa setRawHeaders(), headers()
212 */
213QNetworkCacheMetaData::RawHeaderList QNetworkCacheMetaData::rawHeaders() const
214{
215 return QNetworkHeadersPrivate::fromHttpToRaw(d->headers);
216}
217
218/*!
219 Sets the raw headers to \a list.
220
221 \sa rawHeaders(), setHeaders()
222 */
223void QNetworkCacheMetaData::setRawHeaders(const RawHeaderList &list)
224{
225 d->headers = QNetworkHeadersPrivate::fromRawToHttp(list);
226}
227
228/*!
229 \since 6.8
230
231 Returns headers in form of QHttpHeaders that are set in this meta data.
232
233 \sa setHeaders()
234*/
235QHttpHeaders QNetworkCacheMetaData::headers() const
236{
237 return d->headers;
238}
239
240/*!
241 \since 6.8
242
243 Sets the headers of this network cache meta data to \a headers.
244
245 \sa headers()
246*/
247void QNetworkCacheMetaData::setHeaders(const QHttpHeaders &headers)
248{
249 d->headers = headers;
250}
251
252/*!
253 Returns the date and time when the meta data was last modified.
254 */
255QDateTime QNetworkCacheMetaData::lastModified() const
256{
257 return d->lastModified;
258}
259
260/*!
261 Sets the date and time when the meta data was last modified to \a dateTime.
262 */
263void QNetworkCacheMetaData::setLastModified(const QDateTime &dateTime)
264{
265 d->lastModified = dateTime;
266}
267
268/*!
269 Returns the date and time when the meta data expires.
270 */
271QDateTime QNetworkCacheMetaData::expirationDate() const
272{
273 return d->expirationDate;
274}
275
276/*!
277 Sets the date and time when the meta data expires to \a dateTime.
278 */
279void QNetworkCacheMetaData::setExpirationDate(const QDateTime &dateTime)
280{
281 d->expirationDate = dateTime;
282}
283
284/*!
285 \since 4.6
286
287 Returns all the attributes stored with this cache item.
288
289 \sa setAttributes(), QNetworkRequest::Attribute
290*/
291QNetworkCacheMetaData::AttributesMap QNetworkCacheMetaData::attributes() const
292{
293 return d->attributes;
294}
295
296/*!
297 \since 4.6
298
299 Sets all attributes of this cache item to be the map \a attributes.
300
301 \sa attributes(), QNetworkRequest::setAttribute()
302*/
303void QNetworkCacheMetaData::setAttributes(const AttributesMap &attributes)
304{
305 d->attributes = attributes;
306}
307
308/*!
309 \relates QNetworkCacheMetaData
310 \since 4.5
311
312 Writes \a metaData to the \a out stream.
313
314 \sa {Serializing Qt Data Types}
315*/
316QDataStream &operator<<(QDataStream &out, const QNetworkCacheMetaData &metaData)
317{
318 QNetworkCacheMetaDataPrivate::save(out, metaData);
319 return out;
320}
321
322static inline QDataStream &operator<<(QDataStream &out, const QNetworkCacheMetaData::AttributesMap &hash)
323{
324 out << quint32(hash.size());
325 QNetworkCacheMetaData::AttributesMap::ConstIterator it = hash.begin();
326 QNetworkCacheMetaData::AttributesMap::ConstIterator end = hash.end();
327 while (it != end) {
328 out << int(it.key()) << it.value();
329 ++it;
330 }
331 return out;
332}
333
334void QNetworkCacheMetaDataPrivate::save(QDataStream &out, const QNetworkCacheMetaData &metaData)
335{
336 // note: if you change the contents of the meta data here
337 // remember to bump the cache version in qnetworkdiskcache.cpp CurrentCacheVersion
338 out << metaData.url();
339 out << metaData.expirationDate();
340 out << metaData.lastModified();
341 out << metaData.saveToDisk();
342 out << metaData.attributes();
343 out << metaData.headers();
344}
345
346/*!
347 \relates QNetworkCacheMetaData
348 \since 4.5
349
350 Reads a QNetworkCacheMetaData from the stream \a in into \a metaData.
351
352 \sa {Serializing Qt Data Types}
353*/
354QDataStream &operator>>(QDataStream &in, QNetworkCacheMetaData &metaData)
355{
356 QNetworkCacheMetaDataPrivate::load(in, metaData);
357 return in;
358}
359
360static inline QDataStream &operator>>(QDataStream &in, QNetworkCacheMetaData::AttributesMap &hash)
361{
362 hash.clear();
363 QDataStream::Status oldStatus = in.status();
364 in.resetStatus();
365 hash.clear();
366
367 quint32 n;
368 in >> n;
369
370 for (quint32 i = 0; i < n; ++i) {
371 if (in.status() != QDataStream::Ok)
372 break;
373
374 int k;
375 QVariant t;
376 in >> k >> t;
377 hash.insert(QNetworkRequest::Attribute(k), t);
378 }
379
380 if (in.status() != QDataStream::Ok)
381 hash.clear();
382 if (oldStatus != QDataStream::Ok)
383 in.setStatus(oldStatus);
384 return in;
385}
386
387void QNetworkCacheMetaDataPrivate::load(QDataStream &in, QNetworkCacheMetaData &metaData)
388{
389 auto *p = metaData.d.data();
390 in >> p->url;
391 in >> p->expirationDate;
392 in >> p->lastModified;
393 in >> p->saveToDisk;
394 in >> p->attributes;
395 in >> p->headers;
396}
397
398/*!
399 \class QAbstractNetworkCache
400 \since 4.5
401 \inmodule QtNetwork
402
403 \brief The QAbstractNetworkCache class provides the interface for cache implementations.
404
405 QAbstractNetworkCache is the base class for every standard cache that is used by
406 QNetworkAccessManager. QAbstractNetworkCache is an abstract class and cannot be
407 instantiated.
408
409 \sa QNetworkDiskCache
410*/
411
412/*!
413 Constructs an abstract network cache with the given \a parent.
414*/
415QAbstractNetworkCache::QAbstractNetworkCache(QObject *parent)
416 : QObject(*new QAbstractNetworkCachePrivate, parent)
417{
418}
419
420/*!
421 \internal
422*/
423QAbstractNetworkCache::QAbstractNetworkCache(QAbstractNetworkCachePrivate &dd, QObject *parent)
424 : QObject(dd, parent)
425{
426}
427
428/*!
429 Destroys the cache.
430
431 Any operations that have not been inserted are discarded.
432
433 \sa insert()
434 */
435QAbstractNetworkCache::~QAbstractNetworkCache()
436{
437}
438
439/*!
440 \fn QNetworkCacheMetaData QAbstractNetworkCache::metaData(const QUrl &url) = 0
441 Returns the meta data for the url \a url.
442
443 If the url is valid and the cache contains the data for url,
444 a valid QNetworkCacheMetaData is returned.
445
446 In the base class this is a pure virtual function.
447
448 \sa updateMetaData(), data()
449*/
450
451/*!
452 \fn void QAbstractNetworkCache::updateMetaData(const QNetworkCacheMetaData &metaData) = 0
453 Updates the cache meta date for the metaData's url to \a metaData
454
455 If the cache does not contains a cache item for the url then no action is taken.
456
457 In the base class this is a pure virtual function.
458
459 \sa metaData(), prepare()
460*/
461
462/*!
463 \fn QIODevice *QAbstractNetworkCache::data(const QUrl &url) = 0
464 Returns the data associated with \a url.
465
466 It is up to the application that requests the data to delete
467 the QIODevice when done with it.
468
469 If there is no cache for \a url, the url is invalid, or if there
470 is an internal cache error \nullptr is returned.
471
472 In the base class this is a pure virtual function.
473
474 \sa metaData(), prepare()
475*/
476
477/*!
478 \fn bool QAbstractNetworkCache::remove(const QUrl &url) = 0
479 Removes the cache entry for \a url, returning true if success otherwise false.
480
481 In the base class this is a pure virtual function.
482
483 \sa clear(), prepare()
484*/
485
486/*!
487 \fn QIODevice *QAbstractNetworkCache::prepare(const QNetworkCacheMetaData &metaData) = 0
488 Returns the device that should be populated with the data for
489 the cache item \a metaData. When all of the data has been written
490 insert() should be called. If metaData is invalid or the url in
491 the metadata is invalid \nullptr is returned.
492
493 The cache owns the device and will take care of deleting it when
494 it is inserted or removed.
495
496 To cancel a prepared inserted call remove() on the metadata's url.
497
498 In the base class this is a pure virtual function.
499
500 \sa remove(), updateMetaData(), insert()
501*/
502
503/*!
504 \fn void QAbstractNetworkCache::insert(QIODevice *device) = 0
505 Inserts the data in \a device and the prepared meta data into the cache.
506 After this function is called the data and meta data should be retrievable
507 using data() and metaData().
508
509 To cancel a prepared inserted call remove() on the metadata's url.
510
511 In the base class this is a pure virtual function.
512
513 \sa prepare(), remove()
514*/
515
516/*!
517 \fn qint64 QAbstractNetworkCache::cacheSize() const = 0
518 Returns the current size taken up by the cache. Depending upon
519 the cache implementation this might be disk or memory size.
520
521 In the base class this is a pure virtual function.
522
523 \sa clear()
524*/
525
526/*!
527 \fn void QAbstractNetworkCache::clear() = 0
528 Removes all items from the cache. Unless there was failures
529 clearing the cache cacheSize() should return 0 after a call to clear.
530
531 In the base class this is a pure virtual function.
532
533 \sa cacheSize(), remove()
534*/
535
536QT_END_NAMESPACE
537
538#include "moc_qabstractnetworkcache.cpp"
static void save(QDataStream &out, const QNetworkCacheMetaData &metaData)
bool operator==(const QNetworkCacheMetaDataPrivate &other) const
static void load(QDataStream &in, QNetworkCacheMetaData &metaData)
Combined button and popup list for selecting options.