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

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