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
qnetworkaccessbackend.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 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
7#include "qnetworkrequest.h"
8#include "qnetworkreply.h"
9#include "qnetworkreply_p.h"
10#include "QtCore/qmutex.h"
11#include "QtCore/qstringlist.h"
12
15#include "qhostinfo.h"
16
17#include "private/qnoncontiguousbytedevice_p.h"
18
20
21class QNetworkAccessBackendFactoryData: public QList<QNetworkAccessBackendFactory *>
22{
23public:
29 {
30 QMutexLocker locker(&mutex); // why do we need to lock?
31 valid.deref();
32 }
33
35 //this is used to avoid (re)constructing factory data from destructors of other global classes
37};
40
42{
43public:
44 QNetworkAccessBackend::TargetTypes m_targetTypes;
45 QNetworkAccessBackend::SecurityFeatures m_securityFeatures;
46 QNetworkAccessBackend::IOFeatures m_ioFeatures;
47 std::shared_ptr<QNonContiguousByteDevice> uploadByteDevice;
51
52 bool m_canCache = false;
53 bool m_isSynchronous = false;
54};
55
59{
61 QMutexLocker locker(&factoryData()->mutex);
63 end = factoryData()->constEnd();
64 while (it != end) {
65 QNetworkAccessBackend *backend = (*it)->create(op, request);
66 if (backend) {
67 backend->setManagerPrivate(this);
68 return backend; // found a factory that handled our request
69 }
70 ++it;
71 }
72 }
73 return nullptr;
74}
75
77{
79 QMutexLocker locker(&factoryData()->mutex);
81 QNetworkAccessBackendFactoryData::ConstIterator end = factoryData()->constEnd();
82 QStringList schemes;
83 while (it != end) {
84 schemes += (*it)->supportedSchemes();
85 ++it;
86 }
87 return schemes;
88 }
89 return QStringList();
90}
91
101
108
223 SecurityFeatures securityFeatures,
224 IOFeatures ioFeatures)
226{
228 d->m_targetTypes = targetTypes;
229 d->m_securityFeatures = securityFeatures;
230 d->m_ioFeatures = ioFeatures;
231}
232
240
245 SecurityFeatures securityFeatures)
246 : QNetworkAccessBackend(targetTypes, securityFeatures, IOFeature::None)
247{
248}
249
253QNetworkAccessBackend::QNetworkAccessBackend(TargetTypes targetTypes, IOFeatures ioFeatures)
254 : QNetworkAccessBackend(targetTypes, SecurityFeature::None, ioFeatures)
255{
256}
257
262
269QNetworkAccessBackend::SecurityFeatures QNetworkAccessBackend::securityFeatures() const noexcept
270{
271 return d_func()->m_securityFeatures;
272}
273
279QNetworkAccessBackend::TargetTypes QNetworkAccessBackend::targetTypes() const noexcept
280{
281 return d_func()->m_targetTypes;
282}
283
289QNetworkAccessBackend::IOFeatures QNetworkAccessBackend::ioFeatures() const noexcept
290{
291 return d_func()->m_ioFeatures;
292}
293
301{
303#ifndef QT_NO_NETWORKPROXY
305 d->m_reply->proxyList = d->m_manager->queryProxy(QNetworkProxyQuery(url()));
306#endif
307
308 // now start the request
309 open();
310 return true;
311}
312
354#if QT_CONFIG(ssl)
362void QNetworkAccessBackend::setSslConfiguration(const QSslConfiguration &configuration)
363{
364 Q_UNUSED(configuration);
366 qWarning("Backend (%s) claiming to use TLS hasn't overridden setSslConfiguration.",
367 metaObject()->className());
368 }
369}
370
377QSslConfiguration QNetworkAccessBackend::sslConfiguration() const
378{
380 qWarning("Backend (%s) claiming to use TLS hasn't overridden sslConfiguration.",
381 metaObject()->className());
382 }
383 return {};
384}
385#endif
386
395{
397 qWarning("Backend (%s) claiming to use TLS hasn't overridden ignoreSslErrors.",
398 metaObject()->className());
399 }
400}
401
408void QNetworkAccessBackend::ignoreSslErrors(const QList<QSslError> &errors)
409{
410 Q_UNUSED(errors);
412 qWarning("Backend (%s) claiming to use TLS hasn't overridden ignoreSslErrors.",
413 metaObject()->className());
414 }
415}
416
428{
430 qWarning("Backend (%s) claiming to support ZeroCopy hasn't overridden readPointer.",
431 metaObject()->className());
432 }
433 return {};
434}
435
447{
450 qWarning("Backend (%s) claiming to support ZeroCopy hasn't overridden advanceReadPointer.",
451 metaObject()->className());
452 }
453}
454
464{
465 Q_UNUSED(data);
466 Q_UNUSED(maxlen);
467 if ((ioFeatures() & IOFeature::ZeroCopy) == 0) {
468 qWarning("Backend (%s) is not ZeroCopy and has not implemented read(...)!",
469 metaObject()->className());
470 }
471 return 0;
472}
473
482{
483 // Base implementation does nothing
484 return false;
485}
486
487#if QT_CONFIG(networkproxy)
495QList<QNetworkProxy> QNetworkAccessBackend::proxyList() const
496{
498 return d_func()->m_reply->proxyList;
499}
500#endif
501
506{
507 return d_func()->m_reply->url;
508}
509
515{
516 d_func()->m_reply->url = url;
517}
518
527{
528 return d_func()->m_reply->cookedHeaders.value(header);
529}
530
540{
541 d_func()->m_reply->setCookedHeader(header, value);
542}
543
552{
553 return d_func()->m_reply->q_func()->rawHeader(header);
554}
555
566{
567 d_func()->m_reply->setRawHeader(header, value);
568}
569
578{
579 return d_func()->m_reply->headers();
580}
581
594{
595 d_func()->m_reply->setHeaders(std::move(newHeaders));
596}
597
603{
604 d_func()->m_reply->setHeaders(newHeaders);
605}
606
612{
613 return d_func()->m_reply->operation;
614}
615
623{
624 return d_func()->m_canCache;
625}
626
634{
635 d_func()->m_canCache = canCache;
636}
637
647 const QVariant &value)
648{
650 if (value.isValid())
651 d->m_reply->attributes.insert(attribute, value);
652 else
653 d->m_reply->attributes.remove(attribute);
654}
655
666{
668
669 if (d->m_reply->outgoingDataBuffer)
670 d->uploadByteDevice =
671 QNonContiguousByteDeviceFactory::createShared(d->m_reply->outgoingDataBuffer);
672 else if (d->m_reply->outgoingData) {
673 d->uploadByteDevice =
674 QNonContiguousByteDeviceFactory::createShared(d->m_reply->outgoingData);
675 } else {
676 return nullptr;
677 }
678
679 // We want signal emissions only for normal asynchronous uploads
680 if (!isSynchronous()) {
681 connect(d->uploadByteDevice.get(), &QNonContiguousByteDevice::readProgress, this,
682 [this](qint64 a, qint64 b) {
683 Q_D(QNetworkAccessBackend);
684 if (!d->m_reply->isFinished)
685 d->m_reply->emitUploadProgress(a, b);
686 });
687 }
688
689 d->wrappedUploadByteDevice = QNonContiguousByteDeviceFactory::wrap(d->uploadByteDevice.get());
690 return d->wrappedUploadByteDevice;
691}
692
700{
701 return d_func()->wrappedUploadByteDevice;
702}
703
709bool QNetworkAccessBackend::isSynchronous() const
710{
711 return d_func()->m_isSynchronous;
712}
713
719void QNetworkAccessBackend::setSynchronous(bool synchronous)
720{
722 return;
723 d_func()->m_isSynchronous = synchronous;
724}
725
734
741{
742 d_func()->m_reply->finished();
743}
744
753{
754 Q_ASSERT(!d_func()->m_reply->isFinished);
755 d_func()->m_reply->error(code, errorString);
756}
757
758#ifndef QT_NO_NETWORKPROXY
771 QAuthenticator *authenticator)
772{
774 Q_ASSERT(authenticator);
775 d->m_manager->proxyAuthenticationRequired(QUrl(), proxy, isSynchronous(), authenticator,
776 &d->m_reply->lastProxyAuthentication);
777}
778#endif
779
790{
792 Q_ASSERT(authenticator);
793 d->m_manager->authenticationRequired(authenticator, d->m_reply->q_func(), isSynchronous(),
794 d->m_reply->url, &d->m_reply->urlForLastAuthentication);
795}
796
802{
803 d_func()->m_reply->metaDataChanged();
804}
805
811{
812 d_func()->m_reply->redirectionRequested(destination);
813}
814
818void QNetworkAccessBackend::setReplyPrivate(QNetworkReplyImplPrivate *reply)
819{
820 d_func()->m_reply = reply;
821}
822
826void QNetworkAccessBackend::setManagerPrivate(QNetworkAccessManagerPrivate *manager)
827{
828 d_func()->m_manager = manager;
829}
830
836{
837 return d_func()->m_manager->networkCache;
838}
839
840// -- QNetworkAccessBackendFactory
845{
846 if (factoryData())
847 factoryData->append(this);
848}
849
854{
855 if (factoryData.exists())
856 factoryData->removeAll(this);
857};
858
860
861#include "moc_qnetworkaccessbackend_p.cpp"
The QAbstractNetworkCache class provides the interface for cache implementations.
The QAuthenticator class provides an authentication object.
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore \reentrant
Definition qiodevice.h:34
Definition qlist.h:75
\inmodule QtCore
Definition qmutex.h:313
QNetworkAccessBackendFactory()
Constructs QNetworkAccessBackendFactory.
virtual ~QNetworkAccessBackendFactory()
Destructs QNetworkAccessBackendFactory.
std::shared_ptr< QNonContiguousByteDevice > uploadByteDevice
QNetworkReplyImplPrivate * m_reply
QNetworkAccessBackend::SecurityFeatures m_securityFeatures
QNetworkAccessManagerPrivate * m_manager
QNetworkAccessBackend::IOFeatures m_ioFeatures
QNetworkAccessBackend::TargetTypes m_targetTypes
QNetworkAccessBackend is the base class for implementing support for schemes used by QNetworkAccessMa...
virtual ~QNetworkAccessBackend()
Destructs the QNetworkAccessBackend base class.
QIODevice * uploadByteDevice()
Returns the upload byte device associated with the current request.
IOFeature
Use the values in this enum to specify what type of IO features the plugin may utilize.
bool isCachingEnabled() const
Returns true if setCachingEnabled was previously called with true.
void authenticationRequired(QAuthenticator *auth)
Call this slot if the remote resource requests authentication.
QNetworkAccessManager::Operation operation() const
Returns the operation which was requested when calling QNetworkAccessManager.
virtual qint64 read(char *data, qint64 maxlen)
Implement this function to support reading from the resource made available by your plugin.
void readyRead()
Call this slot when you have more data available to notify the backend that we can attempt to read ag...
virtual QByteArrayView readPointer()
The data which the returned value views must stay valid until at least the next call to a non-const f...
virtual bool start()
Prepares the backend and calls open().
QByteArray rawHeader(const QByteArray &header) const
Returns the value of the header.
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...
void setRawHeader(const QByteArray &header, const QByteArray &value)
Sets the value of the header to value.
QNetworkAccessBackend(TargetTypes targetTypes, SecurityFeatures securityFeatures, IOFeatures ioFeatures)
Constructs the QNetworkAccessBackend.
virtual bool wantToRead()
This is called before we read if there are no bytes available and we are ready to read more.
QAbstractNetworkCache * networkCache() const
Returns the network cache object that was available when the request was started.
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
Sets the value of the header to value.
IOFeatures ioFeatures() const noexcept
Returns the I/O features that the backend claims to support.
void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth)
Call this slot if, when connecting through a proxy, it requests authentication.
virtual void open()=0
You must implement this in your derived class.
QUrl url() const
Returns the current URL of the reply.
QIODevice * createUploadByteDevice()
Creates a QIODevice for the data provided to upload, if any.
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.
TargetTypes targetTypes() const noexcept
Returns the TargetTypes that the backend claims to target.
virtual void ignoreSslErrors()
This function will be called when the user wants to ignore all TLS handshake errors.
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 setUrl(const QUrl &url)
Sets the URL of the reply.
virtual void advanceReadPointer(qint64 distance)
This function is to notify your class that distance bytes have been read using readPointer and next t...
SecurityFeature
Use the values in this enum to specify what type of security features the plugin may utilize.
void setAttribute(QNetworkRequest::Attribute attribute, const QVariant &value)
Set attribute to value.
SecurityFeatures securityFeatures() const noexcept
Returns the security related features that the backend claims to support.
QStringList backendSupportedSchemes() const
QNetworkAccessBackend * findBackend(QNetworkAccessManager::Operation op, const QNetworkRequest &request)
Operation
Indicates the operation this reply is processing.
The QNetworkProxyQuery class is used to query the proxy settings for a socket.
The QNetworkProxy class provides a network layer proxy.
NetworkError
Indicates all possible error conditions found during the processing of the request.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
KnownHeaders
List of known header types that QNetworkRequest parses.
static std::shared_ptr< QNonContiguousByteDevice > createShared(QIODevice *device)
Create a QNonContiguousByteDevice out of a QIODevice, return it in a std::shared_ptr.
static QIODevice * wrap(QNonContiguousByteDevice *byteDevice)
Wrap the byteDevice (possibly again) into a QIODevice.
void readProgress(qint64 current, qint64 total)
Emitted when data has been "read" by advancing the read pointer.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore
Definition qmutex.h:309
const_iterator constBegin() const noexcept
Definition qset.h:139
The QSslConfiguration class holds the configuration and state of an SSL connection.
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qurl.h:94
QString url(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2817
\inmodule QtCore
Definition qvariant.h:65
T value() const &
Definition qvariant.h:516
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define Q_BASIC_ATOMIC_INITIALIZER(a)
QList< QString > QStringList
Constructs a string list that contains the given string, str.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char * destination
static QString header(const QString &name)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
EGLOutputLayerEXT EGLint attribute
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
@ None
Definition qhash.cpp:531
#define qWarning
Definition qlogging.h:166
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLuint end
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLsizei GLsizei GLfloat distance
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_UNUSED(x)
long long qint64
Definition qtypes.h:60
const char className[16]
[1]
Definition qwizard.cpp:100
QUrl url("example.com")
[constructor-url-reference]
obj metaObject() -> className()
QObject::connect nullptr
QMutex mutex
[2]
QNetworkAccessManager manager
QNetworkRequest request(url)
QNetworkReply * reply
QNetworkProxy proxy
[0]