16using namespace Qt::StringLiterals;
29 if (operation() != QNetworkAccessManager::GetOperation || !sendCacheContents()) {
30 QString msg = QCoreApplication::translate(
"QNetworkAccessCacheBackend",
"Error opening %1")
31 .arg(
this->url().toString());
32 error(QNetworkReply::ContentNotFoundError, msg);
34 setAttribute(QNetworkRequest::SourceIsFromCacheAttribute,
true);
41 setCachingEnabled(
false);
42 QAbstractNetworkCache *nc = networkCache();
50 QNetworkCacheMetaData::AttributesMap attributes = item.attributes();
51 setAttribute(QNetworkRequest::HttpStatusCodeAttribute,
52 attributes.value(QNetworkRequest::HttpStatusCodeAttribute));
53 setAttribute(QNetworkRequest::HttpReasonPhraseAttribute,
54 attributes.value(QNetworkRequest::HttpReasonPhraseAttribute));
57 auto headers = item.headers();
58 const auto cacheControlValue = QLatin1StringView(
59 headers.value(QHttpHeaders::WellKnownHeader::CacheControl));
61 if (cacheControlValue.contains(
"must-revalidate"_L1, Qt::CaseInsensitive)
62 || cacheControlValue.contains(
"no-cache"_L1, Qt::CaseInsensitive)) {
65 setHeaders(
std::move(headers));
68 QVariant redirectionTarget = attributes.value(QNetworkRequest::RedirectionTargetAttribute);
69 if (redirectionTarget.isValid()) {
70 setAttribute(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget);
71 redirectionRequested(redirectionTarget.toUrl());
77 if (operation() == QNetworkAccessManager::GetOperation) {
78 device = nc->data(url());
81 device->setParent(
this);
85#if defined(QNETWORKACCESSCACHEBACKEND_DEBUG)
86 qDebug() <<
"Successfully sent cache:" << url();
101 return device ? device->bytesAvailable() : qint64(0);
106 return device ? device->read(data, maxlen) : qint64(0);
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.
~QNetworkAccessCacheBackend()
qint64 bytesAvailable() const override
You must implement this function in your derived class.
QNetworkAccessCacheBackend()
qint64 read(char *data, qint64 maxlen) override
Implement this function to support reading from the resource made available by your plugin.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Combined button and popup list for selecting options.