Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
5#ifndef QHTTPTHREADDELEGATE_H
6#define QHTTPTHREADDELEGATE_H
7
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists for the convenience
14// of the Network Access API. This header file may change from
15// version to version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QtNetwork/private/qtnetworkglobal_p.h>
21#include <QObject>
22#include <QThreadStorage>
23#include <QNetworkProxy>
24#include <QSslConfiguration>
25#include <QSslError>
26#include <QList>
27#include <QNetworkReply>
32#include <QSharedPointer>
33#include "private/qnoncontiguousbytedevice_p.h"
35#include <QtNetwork/private/http2protocol_p.h>
36#include <QtNetwork/qhttpheaders.h>
38
39#ifndef QT_NO_SSL
40#include <memory>
41#endif
42
44
45QT_BEGIN_NAMESPACE
46
47class QAuthenticator;
48class QHttpNetworkReply;
49class QEventLoop;
52
54{
56public:
57 explicit QHttpThreadDelegate(QObject *parent = nullptr);
58
60
61 // incoming
62 bool ssl;
63#ifndef QT_NO_SSL
65#endif
66 QHttpNetworkRequest httpRequest;
70 // From backend, modified by us for signal compression
73#ifndef QT_NO_NETWORKPROXY
74 QNetworkProxy cacheProxy;
75 QNetworkProxy transparentProxy;
76#endif
80
81 // outgoing, Retrieved in the synchronous HTTP case
88 bool isCompressed = false;
95 QTcpKeepAliveConfiguration tcpKeepAliveParameters = {};
96
97protected:
98 // The zerocopy download buffer, if used:
100 // The QHttpNetworkConnection that is used
103 QHttpNetworkReply *httpReply;
104
105 // Used for implementing the synchronous HTTP, see startRequestSynchronously()
107
108signals:
110#ifndef QT_NO_NETWORKPROXY
111 void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *);
112#endif
113#ifndef QT_NO_SSL
114 void encrypted();
115 void sslErrors(const QList<QSslError> &, bool *, QList<QSslError> *);
116 void sslConfigurationChanged(const QSslConfiguration &);
117 void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *);
118#endif
121 void downloadMetaData(const QHttpHeaders &, int, const QString &, bool,
122 QSharedPointer<char>, qint64, qint64, bool, bool);
125 void error(QNetworkReply::NetworkError, const QString &);
127 void redirected(const QUrl &url, int httpStatus, int maxRedirectsRemainig);
128
129public slots:
130 // This are called via QueuedConnection from user thread
131 void startRequest();
132 void abortRequest();
133 void readBufferSizeChanged(qint64 size);
134 void readBufferFreed(qint64 size);
135
136 // This is called with a BlockingQueuedConnection from user thread
138protected slots:
139 // From QHttp*
140 void readyReadSlot();
141 void finishedSlot();
142 void finishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
144 void synchronousFinishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
145 void headerChangedSlot();
147 void dataReadProgressSlot(qint64 done, qint64 total);
148 void cacheCredentialsSlot(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
149#ifndef QT_NO_SSL
150 void encryptedSlot();
151 void sslErrorsSlot(const QList<QSslError> &errors);
152 void preSharedKeyAuthenticationRequiredSlot(QSslPreSharedKeyAuthenticator *authenticator);
153#endif
154
155 void synchronousAuthenticationRequiredSlot(const QHttpNetworkRequest &request, QAuthenticator *);
156#ifndef QT_NO_NETWORKPROXY
157 void synchronousProxyAuthenticationRequiredSlot(const QNetworkProxy &, QAuthenticator *);
158#endif
159
160protected:
161 // Cache for all the QHttpNetworkConnection objects.
162 // This is per thread.
164
165};
166
167// This QNonContiguousByteDevice is connected to the QNetworkAccessHttpBackend
168// and represents the PUT/POST data.
170{
172protected:
173 bool wantDataPending = false;
175 char *m_data = nullptr;
177 bool m_atEnd = false;
179 qint64 m_pos = 0; // to match calls of haveDataSlot with the expected position
180public:
183 m_atEnd(aE),
184 m_size(s)
185 {
186 }
187
191
192 qint64 pos() const override
193 {
194 return m_pos;
195 }
196
197 const char* readPointer(qint64 maximumLength, qint64 &len) override
198 {
199 if (m_amount > 0) {
200 len = m_amount;
201 return m_data;
202 }
203
204 if (m_atEnd) {
205 len = -1;
206 } else if (!wantDataPending) {
207 len = 0;
208 wantDataPending = true;
209 emit wantData(maximumLength);
210 } else {
211 // Do nothing, we already sent a wantData signal and wait for results
212 len = 0;
213 }
214 return nullptr;
215 }
216
217 bool advanceReadPointer(qint64 a) override
218 {
219 if (m_data == nullptr)
220 return false;
221
222 m_amount -= a;
223 m_data += a;
224 m_pos += a;
225
226 // To main thread to inform about our state. The m_pos will be sent as a sanity check.
227 emit processedData(m_pos, a);
228
229 return true;
230 }
231
232 bool atEnd() const override
233 {
234 if (m_amount > 0)
235 return false;
236 else
237 return m_atEnd;
238 }
239
241 {
242 m_amount = 0;
243 m_data = nullptr;
244 m_dataArray.clear();
245
246 if (wantDataPending) {
247 // had requested the user thread to send some data (only 1 in-flight at any moment)
248 wantDataPending = false;
249 }
250
251 // Communicate as BlockingQueuedConnection
252 bool b = false;
253 emit resetData(&b);
254 if (b) {
255 // the reset succeeded, we're at pos 0 again
256 m_pos = 0;
257 m_atEnd = false;
258 // the HTTP code will anyway abort the request if !b.
259 }
260 return b;
261 }
262
263 qint64 size() const override
264 {
265 return m_size;
266 }
267
268public slots:
269 // From user thread:
271 {
272 if (pos != m_pos) {
273 // Sometimes when re-sending a request in the qhttpnetwork* layer there is a pending haveData from the
274 // user thread on the way to us. We need to ignore it since it is the data for the wrong(later) chunk.
275 return;
276 }
277 wantDataPending = false;
278
280 m_data = const_cast<char*>(m_dataArray.constData());
282
285
286 // This will tell the HTTP code (QHttpNetworkConnectionChannel) that we have data available now
287 emit readyRead();
288 }
289
290signals:
291 // void readyRead(); in parent class
292 // void readProgress(qint64 current, qint64 total); happens in the main thread with the real bytedevice
293
294 // to main thread:
296 void processedData(qint64 pos, qint64 amount);
297 void resetData(bool *b);
298};
299
300QT_END_NAMESPACE
301
302#endif // QHTTPTHREADDELEGATE_H
The QHttp1Configuration class controls HTTP/1 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()
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
void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)
void sslErrors(const QList< QSslError > &, bool *, QList< QSslError > *)
void error(QNetworkReply::NetworkError, const QString &)
void synchronousProxyAuthenticationRequiredSlot(const QNetworkProxy &, QAuthenticator *)
QTcpKeepAliveConfiguration tcpKeepAliveParameters
void finishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail=QString())
std::unique_ptr< QSslConfiguration > incomingSslConfiguration
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 &)
QNetworkAccessCachedHttpConnection(quint16 connectionCount, const QString &hostName, quint16 port, bool encrypt, bool isLocalSocket, QHttpNetworkConnection::ConnectionType connectionType)
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.
bool reset() override
Moves the internal read pointer back to the beginning.
\inmodule QtCore
Combined button and popup list for selecting options.
QT_REQUIRE_CONFIG(http)
static QNetworkReply::NetworkError statusCodeFromHttp(int httpStatusCode, const QUrl &url)
static QByteArray makeCacheKey(QUrl &url, QNetworkProxy *proxy, const QString &peerVerifyName)
static QString makeServerErrorString(int code, const QUrl &url, const QString &reasonPhrase)