Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qnetworkaccesscachebackend.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
4//#define QNETWORKACCESSCACHEBACKEND_DEBUG
5
8#include "qfileinfo.h"
9#include "qdir.h"
10#include "qcoreapplication.h"
11#include "qhash.h"
12
14
17{
18}
19
21{
22}
23
25{
26 if (operation() != QNetworkAccessManager::GetOperation || !sendCacheContents()) {
27 QString msg = QCoreApplication::translate("QNetworkAccessCacheBackend", "Error opening %1")
28 .arg(this->url().toString());
30 } else {
32 }
33 finished();
34}
35
36bool QNetworkAccessCacheBackend::sendCacheContents()
37{
38 setCachingEnabled(false);
40 if (!nc)
41 return false;
42
44 if (!item.isValid())
45 return false;
46
47 QNetworkCacheMetaData::AttributesMap attributes = item.attributes();
50
51 // set the raw headers
52 const QNetworkCacheMetaData::RawHeaderList rawHeaders = item.rawHeaders();
53 for (const auto &header : rawHeaders) {
54 if (header.first.toLower() == "cache-control") {
55 const QByteArray cacheControlValue = header.second.toLower();
56 if (cacheControlValue.contains("must-revalidate")
57 || cacheControlValue.contains("no-cache")) {
58 return false;
59 }
60 }
61 setRawHeader(header.first, header.second);
62 }
63
64 // handle a possible redirect
65 QVariant redirectionTarget = attributes.value(QNetworkRequest::RedirectionTargetAttribute);
66 if (redirectionTarget.isValid()) {
68 redirectionRequested(redirectionTarget.toUrl());
69 }
70
71 // signal we're open
73
75 device = nc->data(url());
76 if (!device)
77 return false;
78 device->setParent(this);
79 readyRead();
80 }
81
82#if defined(QNETWORKACCESSCACHEBACKEND_DEBUG)
83 qDebug() << "Successfully sent cache:" << url();
84#endif
85 return true;
86}
87
89{
90 open();
91 return true;
92}
93
95
97{
98 return device ? device->bytesAvailable() : qint64(0);
99}
100
102{
103 return device ? device->read(data, maxlen) : qint64(0);
104}
105
107
The QAbstractNetworkCache class provides the interface for cache implementations.
virtual QIODevice * data(const QUrl &url)=0
Returns the data associated with url.
virtual QNetworkCacheMetaData metaData(const QUrl &url)=0
Returns the meta data for the url url.
\inmodule QtCore
Definition qbytearray.h:57
bool contains(char c) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qbytearray.h:583
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
T value(const Key &key) const noexcept
Definition qhash.h:1044
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
QNetworkAccessBackend is the base class for implementing support for schemes used by QNetworkAccessMa...
QNetworkAccessManager::Operation operation() const
Returns the operation which was requested when calling QNetworkAccessManager.
void readyRead()
Call this slot when you have more data available to notify the backend that we can attempt to read ag...
void finished()
Call this slot when there will be no more data available, regardless of whether the transfer was succ...
void redirectionRequested(const QUrl &destination)
Call this slot if, when connecting to the resource, a redirect to destination was requested.
void setRawHeader(const QByteArray &header, const QByteArray &value)
Sets the value of the header to value.
QAbstractNetworkCache * networkCache() const
Returns the network cache object that was available when the request was started.
QUrl url() const
Returns the current URL of the reply.
TargetType
Use the values in this enum to specify what type of target the plugin supports.
void metaDataChanged()
Call this slot, if appropriate, after having processed and updated metadata (e.g.
QVariant header(QNetworkRequest::KnownHeaders header) const
Returns the value of the header.
void error(QNetworkReply::NetworkError code, const QString &errorString)
Call this slot if an error occurs.
void setCachingEnabled(bool canCache)
If canCache is true then this hints to us that we can cache the reply that is created.
void setAttribute(QNetworkRequest::Attribute attribute, const QVariant &value)
Set attribute to value.
bool start() override
Prepares the backend and calls open().
void close() override
You must implement this function in your derived class.
void open() override
You must implement this in your derived class.
qint64 bytesAvailable() const override
You must implement this function in your derived class.
qint64 read(char *data, qint64 maxlen) override
Implement this function to support reading from the resource made available by your plugin.
The QNetworkCacheMetaData class provides cache information.
void setParent(QObject *parent)
Makes the object a child of parent.
Definition qobject.cpp:2142
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8606
\inmodule QtCore
Definition qvariant.h:64
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
Definition qvariant.h:707
QUrl toUrl() const
Returns the variant as a QUrl if the variant has userType() \l QMetaType::QUrl; otherwise returns an ...
Combined button and popup list for selecting options.
#define qDebug
[1]
Definition qlogging.h:160
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
long long qint64
Definition qtypes.h:55
QGraphicsItem * item
char * toString(const MyType &t)
[31]