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>
37
38#ifndef QT_NO_SSL
39#include <memory>
40#endif
41
43
44QT_BEGIN_NAMESPACE
45
46class QAuthenticator;
47class QHttpNetworkReply;
48class QEventLoop;
51
53{
55public:
56 explicit QHttpThreadDelegate(QObject *parent = nullptr);
57
59
60 // incoming
61 bool ssl;
62#ifndef QT_NO_SSL
64#endif
65 QHttpNetworkRequest httpRequest;
69 // From backend, modified by us for signal compression
72#ifndef QT_NO_NETWORKPROXY
73 QNetworkProxy cacheProxy;
74 QNetworkProxy transparentProxy;
75#endif
79
80 // outgoing, Retrieved in the synchronous HTTP case
87 bool isCompressed = false;
94
95protected:
96 // The zerocopy download buffer, if used:
98 // The QHttpNetworkConnection that is used
101 QHttpNetworkReply *httpReply;
102
103 // Used for implementing the synchronous HTTP, see startRequestSynchronously()
105
106signals:
108#ifndef QT_NO_NETWORKPROXY
109 void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *);
110#endif
111#ifndef QT_NO_SSL
112 void encrypted();
113 void sslErrors(const QList<QSslError> &, bool *, QList<QSslError> *);
114 void sslConfigurationChanged(const QSslConfiguration &);
115 void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *);
116#endif
119 void downloadMetaData(const QHttpHeaders &, int, const QString &, bool,
120 QSharedPointer<char>, qint64, qint64, bool, bool);
123 void error(QNetworkReply::NetworkError, const QString &);
125 void redirected(const QUrl &url, int httpStatus, int maxRedirectsRemainig);
126
127public slots:
128 // This are called via QueuedConnection from user thread
129 void startRequest();
130 void abortRequest();
131 void readBufferSizeChanged(qint64 size);
132 void readBufferFreed(qint64 size);
133
134 // This is called with a BlockingQueuedConnection from user thread
136protected slots:
137 // From QHttp*
138 void readyReadSlot();
139 void finishedSlot();
140 void finishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
142 void synchronousFinishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
143 void headerChangedSlot();
145 void dataReadProgressSlot(qint64 done, qint64 total);
146 void cacheCredentialsSlot(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
147#ifndef QT_NO_SSL
148 void encryptedSlot();
149 void sslErrorsSlot(const QList<QSslError> &errors);
150 void preSharedKeyAuthenticationRequiredSlot(QSslPreSharedKeyAuthenticator *authenticator);
151#endif
152
153 void synchronousAuthenticationRequiredSlot(const QHttpNetworkRequest &request, QAuthenticator *);
154#ifndef QT_NO_NETWORKPROXY
155 void synchronousProxyAuthenticationRequiredSlot(const QNetworkProxy &, QAuthenticator *);
156#endif
157
158protected:
159 // Cache for all the QHttpNetworkConnection objects.
160 // This is per thread.
162
163};
164
165// This QNonContiguousByteDevice is connected to the QNetworkAccessHttpBackend
166// and represents the PUT/POST data.
168{
170protected:
171 bool wantDataPending = false;
173 char *m_data = nullptr;
175 bool m_atEnd = false;
177 qint64 m_pos = 0; // to match calls of haveDataSlot with the expected position
178public:
181 m_atEnd(aE),
182 m_size(s)
183 {
184 }
185
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
215 bool advanceReadPointer(qint64 a) override
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.
225 emit processedData(m_pos, a);
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
239 {
240 m_amount = 0;
241 m_data = nullptr;
242 m_dataArray.clear();
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 m_atEnd = false;
256 // the HTTP code will anyway abort the request if !b.
257 }
258 return b;
259 }
260
261 qint64 size() const override
262 {
263 return m_size;
264 }
265
266public slots:
267 // From user thread:
269 {
270 if (pos != m_pos) {
271 // Sometimes when re-sending a request in the qhttpnetwork* layer there is a pending haveData from the
272 // user thread on the way to us. We need to ignore it since it is the data for the wrong(later) chunk.
273 return;
274 }
275 wantDataPending = false;
276
278 m_data = const_cast<char*>(m_dataArray.constData());
280
283
284 // This will tell the HTTP code (QHttpNetworkConnectionChannel) that we have data available now
285 emit readyRead();
286 }
287
288signals:
289 // void readyRead(); in parent class
290 // void readProgress(qint64 current, qint64 total); happens in the main thread with the real bytedevice
291
292 // to main thread:
294 void processedData(qint64 pos, qint64 amount);
295 void resetData(bool *b);
296};
297
298QT_END_NAMESPACE
299
300#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 *)
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
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)