Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qhttpthreaddelegate_p.h
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#ifndef QHTTPTHREADDELEGATE_H
5#define QHTTPTHREADDELEGATE_H
6
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of the Network Access API. This header file may change from
14// version to version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtNetwork/private/qtnetworkglobal_p.h>
20#include <QObject>
21#include <QThreadStorage>
22#include <QNetworkProxy>
23#include <QSslConfiguration>
24#include <QSslError>
25#include <QList>
26#include <QNetworkReply>
29#include "qhttp1configuration.h"
30#include "qhttp2configuration.h"
31#include <QSharedPointer>
32#include <QScopedPointer>
33#include "private/qnoncontiguousbytedevice_p.h"
35#include <QtNetwork/private/http2protocol_p.h>
36
38
40
41class QAuthenticator;
43class QEventLoop;
46
48{
50public:
51 explicit QHttpThreadDelegate(QObject *parent = nullptr);
52
54
55 // incoming
56 bool ssl;
57#ifndef QT_NO_SSL
59#endif
64 // From backend, modified by us for signal compression
65 std::shared_ptr<QAtomicInt> pendingDownloadData;
66 std::shared_ptr<QAtomicInt> pendingDownloadProgress;
67#ifndef QT_NO_NETWORKPROXY
70#endif
71 std::shared_ptr<QNetworkAccessAuthenticationManager> authenticationManager;
74
75 // outgoing, Retrieved in the synchronous HTTP case
88
90
91protected:
92 // The zerocopy download buffer, if used:
94 // The QHttpNetworkConnection that is used
98
99 // Used for implementing the synchronous HTTP, see startRequestSynchronously()
101
102signals:
104#ifndef QT_NO_NETWORKPROXY
106#endif
107#ifndef QT_NO_SSL
108 void encrypted();
112#endif
115 void downloadMetaData(const QList<QPair<QByteArray,QByteArray> > &, int, const QString &, bool,
116 QSharedPointer<char>, qint64, qint64, bool, bool);
121 void redirected(const QUrl &url, int httpStatus, int maxRedirectsRemainig);
122
123public slots:
124 // This are called via QueuedConnection from user thread
125 void startRequest();
126 void abortRequest();
129
130 // This is called with a BlockingQueuedConnection from user thread
132protected slots:
133 // From QHttp*
134 void readyReadSlot();
135 void finishedSlot();
139 void headerChangedSlot();
143#ifndef QT_NO_SSL
144 void encryptedSlot();
145 void sslErrorsSlot(const QList<QSslError> &errors);
147#endif
148
150#ifndef QT_NO_NETWORKPROXY
152#endif
153
154protected:
155 // Cache for all the QHttpNetworkConnection objects.
156 // This is per thread.
158
159};
160
161// This QNonContiguousByteDevice is connected to the QNetworkAccessHttpBackend
162// and represents the PUT/POST data.
164{
166protected:
169 char *m_data;
173 qint64 m_pos; // to match calls of haveDataSlot with the expected position
174public:
178 m_amount(0),
180 m_atEnd(aE),
181 m_size(s),
182 m_pos(0)
183 {
184 }
185
187 {
188 }
189
190 qint64 pos() const override
191 {
192 return m_pos;
193 }
194
195 const char* readPointer(qint64 maximumLength, qint64 &len) override
196 {
197 if (m_amount > 0) {
198 len = m_amount;
199 return m_data;
200 }
201
202 if (m_atEnd) {
203 len = -1;
204 } else if (!wantDataPending) {
205 len = 0;
206 wantDataPending = true;
207 emit wantData(maximumLength);
208 } else {
209 // Do nothing, we already sent a wantData signal and wait for results
210 len = 0;
211 }
212 return nullptr;
213 }
214
216 {
217 if (m_data == nullptr)
218 return false;
219
220 m_amount -= a;
221 m_data += a;
222 m_pos += a;
223
224 // To main thread to inform about our state. The m_pos will be sent as a sanity check.
226
227 return true;
228 }
229
230 bool atEnd() const override
231 {
232 if (m_amount > 0)
233 return false;
234 else
235 return m_atEnd;
236 }
237
238 bool reset() override
239 {
240 m_amount = 0;
241 m_data = nullptr;
243
244 if (wantDataPending) {
245 // had requested the user thread to send some data (only 1 in-flight at any moment)
246 wantDataPending = false;
247 }
248
249 // Communicate as BlockingQueuedConnection
250 bool b = false;
251 emit resetData(&b);
252 if (b) {
253 // the reset succeeded, we're at pos 0 again
254 m_pos = 0;
255 // the HTTP code will anyway abort the request if !b.
256 }
257 return b;
258 }
259
260 qint64 size() const override
261 {
262 return m_size;
263 }
264
265public slots:
266 // From user thread:
267 void haveDataSlot(qint64 pos, const QByteArray &dataArray, bool dataAtEnd, qint64 dataSize)
268 {
269 if (pos != m_pos) {
270 // Sometimes when re-sending a request in the qhttpnetwork* layer there is a pending haveData from the
271 // user thread on the way to us. We need to ignore it since it is the data for the wrong(later) chunk.
272 return;
273 }
274 wantDataPending = false;
275
276 m_dataArray = dataArray;
277 m_data = const_cast<char*>(m_dataArray.constData());
278 m_amount = dataArray.size();
279
280 m_atEnd = dataAtEnd;
282
283 // This will tell the HTTP code (QHttpNetworkConnectionChannel) that we have data available now
284 emit readyRead();
285 }
286
287signals:
288 // void readyRead(); in parent class
289 // void readProgress(qint64 current, qint64 total); happens in the main thread with the real bytedevice
290
291 // to main thread:
294 void resetData(bool *b);
295};
296
298
299#endif // QHTTPTHREADDELEGATE_H
The QAuthenticator class provides an authentication object.
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:474
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:122
void clear()
Clears the contents of the byte array and makes it null.
\inmodule QtCore
Definition qeventloop.h:16
The QHttp1Configuration class controls HTTP/1 parameters and settings.
The QHttp2Configuration class controls HTTP/2 parameters and settings.
QList< QPair< QByteArray, QByteArray > > incomingHeaders
void cacheCredentialsSlot(const QHttpNetworkRequest &request, QAuthenticator *authenticator)
QSharedPointer< char > downloadBuffer
QHttpNetworkReply * httpReply
void readBufferSizeChanged(qint64 size)
void synchronousAuthenticationRequiredSlot(const QHttpNetworkRequest &request, QAuthenticator *)
void synchronousFinishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail=QString())
void socketStartedConnecting()
QScopedPointer< QSslConfiguration > incomingSslConfiguration
std::shared_ptr< QAtomicInt > pendingDownloadData
void sslErrorsSlot(const QList< QSslError > &errors)
QNetworkAccessCachedHttpConnection * httpConnection
void redirected(const QUrl &url, int httpStatus, int maxRedirectsRemainig)
void downloadProgress(qint64, qint64)
QHttpNetworkRequest httpRequest
void downloadMetaData(const QList< QPair< QByteArray, QByteArray > > &, int, const QString &, bool, QSharedPointer< char >, qint64, qint64, bool, bool)
void preSharedKeyAuthenticationRequiredSlot(QSslPreSharedKeyAuthenticator *authenticator)
QHttp1Configuration http1Parameters
void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)
void sslErrors(const QList< QSslError > &, bool *, QList< QSslError > *)
void error(QNetworkReply::NetworkError, const QString &)
void synchronousProxyAuthenticationRequiredSlot(const QNetworkProxy &, QAuthenticator *)
void finishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail=QString())
void authenticationRequired(const QHttpNetworkRequest &request, QAuthenticator *)
void readBufferFreed(qint64 size)
QHttp2Configuration http2Parameters
static QThreadStorage< QNetworkAccessCache * > connections
std::shared_ptr< QAtomicInt > pendingDownloadProgress
QNetworkReply::NetworkError incomingErrorCode
void downloadData(const QByteArray &)
std::shared_ptr< QNetworkAccessAuthenticationManager > authenticationManager
void dataReadProgressSlot(qint64 done, qint64 total)
void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *)
void sslConfigurationChanged(const QSslConfiguration &)
Definition qlist.h:74
The QNetworkProxy class provides a network layer proxy.
NetworkError
Indicates all possible error conditions found during the processing of the request.
qint64 size() const override
Returns the size of the complete device or -1 if unknown.
bool atEnd() const override
Returns true if everything has been read and the read pointer cannot be advanced anymore.
void processedData(qint64 pos, qint64 amount)
const char * readPointer(qint64 maximumLength, qint64 &len) override
Return a byte pointer for at most maximumLength bytes of that device.
bool advanceReadPointer(qint64 a) override
will advance the internal read pointer by amount bytes.
void haveDataSlot(qint64 pos, const QByteArray &dataArray, bool dataAtEnd, qint64 dataSize)
bool reset() override
Moves the internal read pointer back to the beginning.
void readyRead()
Emitted when there is data available.
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
\inmodule QtCore
\inmodule QtCore
The QSslConfiguration class holds the configuration and state of an SSL connection.
The QSslPreSharedKeyAuthenticator class provides authentication data for pre shared keys (PSK) cipher...
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
\inmodule QtCore
Definition qurl.h:94
Combined button and popup list for selecting options.
std::pair< T1, T2 > QPair
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLsizei dataSize
GLenum GLsizei len
GLdouble s
[6]
Definition qopenglext.h:235
#define QT_REQUIRE_CONFIG(feature)
#define Q_OBJECT
#define slots
#define signals
#define emit
long long qint64
Definition qtypes.h:55
QUrl url("example.com")
[constructor-url-reference]
QObject::connect nullptr
QNetworkRequest request(url)