Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
15using namespace Qt::StringLiterals;
16
21
25
27{
28 if (operation() != QNetworkAccessManager::GetOperation || !sendCacheContents()) {
29 QString msg = QCoreApplication::translate("QNetworkAccessCacheBackend", "Error opening %1")
30 .arg(this->url().toString());
32 } else {
34 }
35 finished();
36}
37
38bool QNetworkAccessCacheBackend::sendCacheContents()
39{
40 setCachingEnabled(false);
42 if (!nc)
43 return false;
44
45 QNetworkCacheMetaData item = nc->metaData(url());
46 if (!item.isValid())
47 return false;
48
49 QNetworkCacheMetaData::AttributesMap attributes = item.attributes();
54
55 // set the headers
56 auto headers = item.headers();
57 const auto cacheControlValue = QLatin1StringView(
59 // RFC 9111 Section 5.2 Cache Control
60 if (cacheControlValue.contains("must-revalidate"_L1, Qt::CaseInsensitive)
61 || cacheControlValue.contains("no-cache"_L1, Qt::CaseInsensitive)) {
62 return false;
63 }
64 setHeaders(std::move(headers));
65
66 // handle a possible redirect
67 QVariant redirectionTarget = attributes.value(QNetworkRequest::RedirectionTargetAttribute);
68 if (redirectionTarget.isValid()) {
70 redirectionRequested(redirectionTarget.toUrl());
71 }
72
73 // signal we're open
75
77 device = nc->data(url());
78 if (!device)
79 return false;
80 device->setParent(this);
81 readyRead();
82 }
83
84#if defined(QNETWORKACCESSCACHEBACKEND_DEBUG)
85 qDebug() << "Successfully sent cache:" << url();
86#endif
87 return true;
88}
89
91{
92 open();
93 return true;
94}
95
97
99{
100 return device ? device->bytesAvailable() : qint64(0);
101}
102
104{
105 return device ? device->read(data, maxlen) : qint64(0);
106}
107
109
The QAbstractNetworkCache class provides the interface for cache implementations.
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:1054
Q_NETWORK_EXPORT QByteArrayView value(QAnyStringView name, QByteArrayView defaultValue={}) const noexcept
Returns the value of the (first) header name, or defaultValue if it doesn't exist.
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 setHeaders(const QHttpHeaders &newHeaders)
This is an overloaded member function, provided for convenience. It differs from the above function o...
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.
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:2195
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
Combined button and popup list for selecting options.
@ CaseInsensitive
DBusConnection const char DBusError * error
#define qDebug
[1]
Definition qlogging.h:164
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
long long qint64
Definition qtypes.h:60
QGraphicsItem * item
char * toString(const MyType &t)
[31]