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 QHTTPNETWORKREPLY_H
5#define QHTTPNETWORKREPLY_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of the Network Access API. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19
20#include <qplatformdefs.h>
21
22#include <QtNetwork/qtcpsocket.h>
23// it's safe to include these even if SSL support is not enabled
24#include <QtNetwork/qsslsocket.h>
25#include <QtNetwork/qsslerror.h>
26
27#include <QtNetwork/qnetworkrequest.h>
28#include <QtNetwork/qnetworkreply.h>
29#include <qbuffer.h>
30
31#include <private/qobject_p.h>
32#include <private/qhttpnetworkheader_p.h>
33#include <private/qhttpnetworkrequest_p.h>
34#include <private/qauthenticator_p.h>
35#include <private/qringbuffer_p.h>
36#include <private/qbytedata_p.h>
37
38#ifndef QT_NO_NETWORKPROXY
39Q_MOC_INCLUDE(<QtNetwork/QNetworkProxy>)
40#endif
41Q_MOC_INCLUDE(<QtNetwork/QAuthenticator>)
42
43#include <private/qdecompresshelper_p.h>
44#include <QtNetwork/qhttpheaders.h>
45
46#include <QtCore/qpointer.h>
47
48QT_REQUIRE_CONFIG(http);
49
50QT_BEGIN_NAMESPACE
51
52class QHttpNetworkConnection;
53class QHttpNetworkConnectionChannel;
54class QHttpNetworkRequest;
55class QHttpNetworkConnectionPrivate;
56class QHttpNetworkReplyPrivate;
57class Q_NETWORK_EXPORT QHttpNetworkReply : public QObject, public QHttpNetworkHeader
58{
59 Q_OBJECT
60public:
61
62 explicit QHttpNetworkReply(const QUrl &url = QUrl(), QObject *parent = nullptr);
63 ~QHttpNetworkReply() override;
64
65 QUrl url() const override;
66 void setUrl(const QUrl &url) override;
67
68 int majorVersion() const override;
69 int minorVersion() const override;
70 void setMajorVersion(int version);
71 void setMinorVersion(int version);
72
73 qint64 contentLength() const override;
74 void setContentLength(qint64 length) override;
75
76 QHttpHeaders header() const override;
77 QByteArray headerField(QByteArrayView name, const QByteArray &defaultValue = QByteArray()) const override;
78 void setHeaderField(const QByteArray &name, const QByteArray &data) override;
79 void appendHeaderField(const QByteArray &name, const QByteArray &data);
80 void parseHeader(QByteArrayView header); // used for testing
81
82 QHttpNetworkRequest request() const;
83 void setRequest(const QHttpNetworkRequest &request);
84
85 int statusCode() const;
86 void setStatusCode(int code);
87
88 QString errorString() const;
89 void setErrorString(const QString &error);
90
91 QNetworkReply::NetworkError errorCode() const;
92
93 QString reasonPhrase() const;
94 void setReasonPhrase(const QString &reason);
95
96 qint64 bytesAvailable() const;
97 qint64 bytesAvailableNextBlock() const;
98 bool readAnyAvailable() const;
99 QByteArray readAny();
100 QByteArray readAll();
101 QByteArray read(qint64 amount);
102 qint64 sizeNextBlock();
103 void setDownstreamLimited(bool t);
104 void setReadBufferSize(qint64 size);
105
106 bool supportsUserProvidedDownloadBuffer();
107 void setUserProvidedDownloadBuffer(char*);
108 char* userProvidedDownloadBuffer();
109
110 void abort();
111
112 bool isAborted() const;
113 bool isFinished() const;
114
115 bool isPipeliningUsed() const;
116 bool isHttp2Used() const;
117 void setHttp2WasUsed(bool h2Used);
118 qint64 removedContentLength() const;
119
120 bool isRedirecting() const;
121
122 QHttpNetworkConnection* connection();
123
124 QUrl redirectUrl() const;
125 void setRedirectUrl(const QUrl &url);
126
127 static bool isHttpRedirect(int statusCode);
128
129 bool isCompressed() const;
130
131#ifndef QT_NO_SSL
132 QSslConfiguration sslConfiguration() const;
133 void setSslConfiguration(const QSslConfiguration &config);
134 void ignoreSslErrors();
135 void ignoreSslErrors(const QList<QSslError> &errors);
136
137Q_SIGNALS:
138 void encrypted();
139 void sslErrors(const QList<QSslError> &errors);
140 void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator);
141#endif
142
143Q_SIGNALS:
144 void socketStartedConnecting();
145 void requestSent();
146 void readyRead();
147 void finished();
148 void finishedWithError(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
149 void headerChanged();
150 void dataReadProgress(qint64 done, qint64 total);
151 void dataSendProgress(qint64 done, qint64 total);
152 void cacheCredentials(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
153#ifndef QT_NO_NETWORKPROXY
154 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
155#endif
156 void authenticationRequired(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
157 void redirected(const QUrl &url, int httpStatus, int maxRedirectsRemaining);
158private:
159 Q_DECLARE_PRIVATE(QHttpNetworkReply)
160 friend class QHttpSocketEngine;
161 friend class QHttpNetworkConnection;
162 friend class QHttpNetworkConnectionPrivate;
163 friend class QHttpNetworkConnectionChannel;
164 friend class QHttp2ProtocolHandler;
165 friend class QHttpProtocolHandler;
166 friend class QSpdyProtocolHandler;
167};
168
169
170class Q_AUTOTEST_EXPORT QHttpNetworkReplyPrivate : public QObjectPrivate, public QHttpNetworkHeaderPrivate
171{
172public:
173 QHttpNetworkReplyPrivate(const QUrl &newUrl = QUrl());
174 ~QHttpNetworkReplyPrivate();
175 qint64 readStatus(QIODevice *socket);
176 bool parseStatus(QByteArrayView status);
177 qint64 readHeader(QIODevice *socket);
178 void parseHeader(QByteArrayView header);
179 void appendHeaderField(const QByteArray &name, const QByteArray &data);
180 qint64 readBody(QIODevice *socket, QByteDataBuffer *out);
181 qint64 readBodyVeryFast(QIODevice *socket, char *b);
182 qint64 readBodyFast(QIODevice *socket, QByteDataBuffer *rb);
183 void clear();
184 void clearHttpLayerInformation();
185
186 qint64 readReplyBodyRaw(QIODevice *in, QByteDataBuffer *out, qint64 size);
187 qint64 readReplyBodyChunked(QIODevice *in, QByteDataBuffer *out);
188 qint64 getChunkSize(QIODevice *in, qint64 *chunkSize);
189
190 bool isRedirecting() const;
191 bool shouldEmitSignals();
192 bool expectContent();
193 void eraseData();
194
195 qint64 bytesAvailable() const;
196 bool isChunked();
197 bool isConnectionCloseEnabled();
198
199 bool isCompressed() const;
200 void removeAutoDecompressHeader();
201
202 enum ReplyState {
203 NothingDoneState,
204 ReadingStatusState,
205 ReadingHeaderState,
206 ReadingDataState,
207 AllDoneState,
208 SPDYSYNSent,
209 SPDYUploading,
210 SPDYHalfClosed,
211 SPDYClosed,
212 Aborted
213 } state;
214
215 QHttpNetworkRequest request;
216 bool ssl;
217 QString errorString;
218 qint64 bodyLength;
219 qint64 contentRead;
220 qint64 totalProgress;
221 QByteArray fragment; // used for header, status, chunk header etc, not for reply data
222 bool chunkedTransferEncoding;
223 bool connectionCloseEnabled;
224 bool forceConnectionCloseEnabled;
225 bool lastChunkRead;
226 qint64 currentChunkSize;
227 qint64 currentChunkRead;
228 qint64 readBufferMaxSize;
229 qint64 totallyUploadedData; // HTTP/2
230 qint64 removedContentLength;
231 QPointer<QHttpNetworkConnection> connection;
232 QPointer<QHttpNetworkConnectionChannel> connectionChannel;
233 QNetworkReply::NetworkError httpErrorCode = QNetworkReply::NoError;
234
235 bool autoDecompress;
236
237 QByteDataBuffer responseData; // uncompressed body
238 bool requestIsPrepared;
239
240 bool pipeliningUsed;
241 bool h2Used;
242 bool downstreamLimited;
243
244 char* userProvidedDownloadBuffer;
245 QUrl redirectUrl;
246};
247
248
249
250
251QT_END_NAMESPACE
252
253#endif // QHTTPNETWORKREPLY_H
254

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtbase/src/network/access/qhttpnetworkreply_p.h