Qt 6.x
The Qt SDK
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:
25 {
26 valid.ref();
27 }
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
238{
239}
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
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
575{
576 return d_func()->m_reply->operation;
577}
578
586{
587 return d_func()->m_canCache;
588}
589
597{
598 d_func()->m_canCache = canCache;
599}
600
610 const QVariant &value)
611{
613 if (value.isValid())
614 d->m_reply->attributes.insert(attribute, value);
615 else
616 d->m_reply->attributes.remove(attribute);
617}
618
629{
631
632 if (d->m_reply->outgoingDataBuffer)
633 d->uploadByteDevice =
634 QNonContiguousByteDeviceFactory::createShared(d->m_reply->outgoingDataBuffer);
635 else if (d->m_reply->outgoingData) {
636 d->uploadByteDevice =
637 QNonContiguousByteDeviceFactory::createShared(d->m_reply->outgoingData);
638 } else {
639 return nullptr;
640 }
641
642 // We want signal emissions only for normal asynchronous uploads
643 if (!isSynchronous()) {
644 connect(d->uploadByteDevice.get(), &QNonContiguousByteDevice::readProgress, this,
645 [this](qint64 a, qint64 b) {
646 Q_D(QNetworkAccessBackend);
647 if (!d->m_reply->isFinished)
648 d->m_reply->emitUploadProgress(a, b);
649 });
650 }
651
652 d->wrappedUploadByteDevice = QNonContiguousByteDeviceFactory::wrap(d->uploadByteDevice.get());
653 return d->wrappedUploadByteDevice;
654}
655
663{
664 return d_func()->wrappedUploadByteDevice;
665}
666
672bool QNetworkAccessBackend::isSynchronous() const
673{
674 return d_func()->m_isSynchronous;
675}
676
682void QNetworkAccessBackend::setSynchronous(bool synchronous)
683{
685 return;
686 d_func()->m_isSynchronous = synchronous;
687}
688
694{
695 d_func()->m_reply->backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite);
696}
697
704{
705 d_func()->m_reply->finished();
706}
707
716{
717 Q_ASSERT(!d_func()->m_reply->isFinished);
718 d_func()->m_reply->error(code, errorString);
719}
720
721#ifndef QT_NO_NETWORKPROXY
734 QAuthenticator *authenticator)
735{
737 Q_ASSERT(authenticator);
738 d->m_manager->proxyAuthenticationRequired(QUrl(), proxy, isSynchronous(), authenticator,
739 &d->m_reply->lastProxyAuthentication);
740}
741#endif
742
753{
755 Q_ASSERT(authenticator);
756 d->m_manager->authenticationRequired(authenticator, d->m_reply->q_func(), isSynchronous(),
757 d->m_reply->url, &d->m_reply->urlForLastAuthentication);
758}
759
765{
766 d_func()->m_reply->metaDataChanged();
767}
768
774{
775 d_func()->m_reply->redirectionRequested(destination);
776}
777
781void QNetworkAccessBackend::setReplyPrivate(QNetworkReplyImplPrivate *reply)
782{
783 d_func()->m_reply = reply;
784}
785
789void QNetworkAccessBackend::setManagerPrivate(QNetworkAccessManagerPrivate *manager)
790{
791 d_func()->m_manager = manager;
792}
793
799{
800 return d_func()->m_manager->networkCache;
801}
802
803// -- QNetworkAccessBackendFactory
808{
809 if (factoryData())
810 factoryData->append(this);
811}
812
817{
818 if (factoryData.exists())
819 factoryData->removeAll(this);
820};
821
823
824#include "moc_qnetworkaccessbackend_p.cpp"
The QAbstractNetworkCache class provides the interface for cache implementations.
The QAuthenticator class provides an authentication object.
bool ref() noexcept
bool deref() noexcept
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore \reentrant
Definition qiodevice.h:34
Definition qlist.h:74
\inmodule QtCore
Definition qmutex.h:317
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 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:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
\inmodule QtCore
Definition qmutex.h:313
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:127
\inmodule QtCore
Definition qurl.h:94
QString url(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2814
\inmodule QtCore
Definition qvariant.h:64
T value() const &
Definition qvariant.h:511
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define Q_BASIC_ATOMIC_INITIALIZER(a)
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,...)
#define qWarning
Definition qlogging.h:162
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLuint end
GLsizei GLsizei GLfloat distance
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_UNUSED(x)
long long qint64
Definition qtypes.h:55
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]