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
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#include <QtNetwork/qhttpheaders.h>
37
39
41
42class QAuthenticator;
44class QEventLoop;
47
49{
51public:
52 explicit QHttpThreadDelegate(QObject *parent = nullptr);
53
55
56 // incoming
57 bool ssl;
58#ifndef QT_NO_SSL
59 QScopedPointer<QSslConfiguration> incomingSslConfiguration;
60#endif
65 // From backend, modified by us for signal compression
66 std::shared_ptr<QAtomicInt> pendingDownloadData;
67 std::shared_ptr<QAtomicInt> pendingDownloadProgress;
68#ifndef QT_NO_NETWORKPROXY
71#endif
72 std::shared_ptr<QNetworkAccessAuthenticationManager> authenticationManager;
75
76 // outgoing, Retrieved in the synchronous HTTP case
89
91
92protected:
93 // The zerocopy download buffer, if used:
94 QSharedPointer<char> downloadBuffer;
95 // The QHttpNetworkConnection that is used
99
100 // Used for implementing the synchronous HTTP, see startRequestSynchronously()
102
103signals:
105#ifndef QT_NO_NETWORKPROXY
107#endif
108#ifndef QT_NO_SSL
109 void encrypted();
110 void sslErrors(const QList<QSslError> &, bool *, QList<QSslError> *);
113#endif
116 void downloadMetaData(const QHttpHeaders &, int, const QString &, bool,
117 QSharedPointer<char>, qint64, qint64, bool, bool);
122 void redirected(const QUrl &url, int httpStatus, int maxRedirectsRemainig);
123
124public slots:
125 // This are called via QueuedConnection from user thread
126 void startRequest();
127 void abortRequest();
130
131 // This is called with a BlockingQueuedConnection from user thread
133protected slots:
134 // From QHttp*
135 void readyReadSlot();
136 void finishedSlot();
140 void headerChangedSlot();
144#ifndef QT_NO_SSL
145 void encryptedSlot();
146 void sslErrorsSlot(const QList<QSslError> &errors);
148#endif
149
151#ifndef QT_NO_NETWORKPROXY
153#endif
154
155protected:
156 // Cache for all the QHttpNetworkConnection objects.
157 // This is per thread.
158 static QThreadStorage<QNetworkAccessCache *> connections;
159
160};
161
162// This QNonContiguousByteDevice is connected to the QNetworkAccessHttpBackend
163// and represents the PUT/POST data.
165{
167protected:
168 bool wantDataPending = false;
170 char *m_data = nullptr;
172 bool m_atEnd = false;
174 qint64 m_pos = 0; // to match calls of haveDataSlot with the expected position
175public:
182
186
187 qint64 pos() const override
188 {
189 return m_pos;
190 }
191
192 const char* readPointer(qint64 maximumLength, qint64 &len) override
193 {
194 if (m_amount > 0) {
195 len = m_amount;
196 return m_data;
197 }
198
199 if (m_atEnd) {
200 len = -1;
201 } else if (!wantDataPending) {
202 len = 0;
203 wantDataPending = true;
204 emit wantData(maximumLength);
205 } else {
206 // Do nothing, we already sent a wantData signal and wait for results
207 len = 0;
208 }
209 return nullptr;
210 }
211
213 {
214 if (m_data == nullptr)
215 return false;
216
217 m_amount -= a;
218 m_data += a;
219 m_pos += a;
220
221 // To main thread to inform about our state. The m_pos will be sent as a sanity check.
223
224 return true;
225 }
226
227 bool atEnd() const override
228 {
229 if (m_amount > 0)
230 return false;
231 else
232 return m_atEnd;
233 }
234
235 bool reset() override
236 {
237 m_amount = 0;
238 m_data = nullptr;
240
241 if (wantDataPending) {
242 // had requested the user thread to send some data (only 1 in-flight at any moment)
243 wantDataPending = false;
244 }
245
246 // Communicate as BlockingQueuedConnection
247 bool b = false;
248 emit resetData(&b);
249 if (b) {
250 // the reset succeeded, we're at pos 0 again
251 m_pos = 0;
252 m_atEnd = false;
253 // the HTTP code will anyway abort the request if !b.
254 }
255 return b;
256 }
257
258 qint64 size() const override
259 {
260 return m_size;
261 }
262
263public slots:
264 // From user thread:
265 void haveDataSlot(qint64 pos, const QByteArray &dataArray, bool dataAtEnd, qint64 dataSize)
266 {
267 if (pos != m_pos) {
268 // Sometimes when re-sending a request in the qhttpnetwork* layer there is a pending haveData from the
269 // user thread on the way to us. We need to ignore it since it is the data for the wrong(later) chunk.
270 return;
271 }
272 wantDataPending = false;
273
274 m_dataArray = dataArray;
275 m_data = const_cast<char*>(m_dataArray.constData());
276 m_amount = dataArray.size();
277
278 m_atEnd = dataAtEnd;
280
281 // This will tell the HTTP code (QHttpNetworkConnectionChannel) that we have data available now
282 emit readyRead();
283 }
284
285signals:
286 // void readyRead(); in parent class
287 // void readProgress(qint64 current, qint64 total); happens in the main thread with the real bytedevice
288
289 // to main thread:
292 void resetData(bool *b);
293};
294
296
297#endif // QHTTPTHREADDELEGATE_H
The QAuthenticator class provides an authentication object.
\inmodule QtCore
Definition qbytearray.h:57
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
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.
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 downloadMetaData(const QHttpHeaders &, int, const QString &, bool, QSharedPointer< char >, qint64, qint64, bool, bool)
void downloadProgress(qint64, qint64)
QHttpNetworkRequest httpRequest
void preSharedKeyAuthenticationRequiredSlot(QSslPreSharedKeyAuthenticator *authenticator)
QHttp1Configuration http1Parameters
QHttpThreadDelegate(QObject *parent=nullptr)
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 &)
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:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
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:129
\inmodule QtCore
Definition qurl.h:94
Combined button and popup list for selecting options.
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLsizei dataSize
GLdouble s
[6]
Definition qopenglext.h:235
GLenum GLsizei len
#define QT_REQUIRE_CONFIG(feature)
#define Q_OBJECT
#define slots
#define signals
#define emit
long long qint64
Definition qtypes.h:60
QUrl url("example.com")
[constructor-url-reference]
QNetworkRequest request(url)