1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QHTTPNETWORKREPLY_H
41#define QHTTPNETWORKREPLY_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of the Network Access API. This header file may change from
49// version to version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtNetwork/private/qtnetworkglobal_p.h>
55
56#include <qplatformdefs.h>
57
58#include <QtNetwork/qtcpsocket.h>
59// it's safe to include these even if SSL support is not enabled
60#include <QtNetwork/qsslsocket.h>
61#include <QtNetwork/qsslerror.h>
62
63#include <QtNetwork/qnetworkrequest.h>
64#include <QtNetwork/qnetworkreply.h>
65#include <qbuffer.h>
66
67#include <private/qobject_p.h>
68#include <private/qhttpnetworkheader_p.h>
69#include <private/qhttpnetworkrequest_p.h>
70#include <private/qauthenticator_p.h>
71#include <private/qringbuffer_p.h>
72#include <private/qbytedata_p.h>
73
74#ifndef QT_NO_NETWORKPROXY
75Q_MOC_INCLUDE(<QtNetwork/QNetworkProxy>)
76#endif
77Q_MOC_INCLUDE(<QtNetwork/QAuthenticator>)
78
79#include <private/qdecompresshelper_p.h>
80
81QT_REQUIRE_CONFIG(http);
82
83QT_BEGIN_NAMESPACE
84
85class QHttpNetworkConnection;
86class QHttpNetworkConnectionChannel;
87class QHttpNetworkRequest;
88class QHttpNetworkConnectionPrivate;
89class QHttpNetworkReplyPrivate;
90class Q_AUTOTEST_EXPORT QHttpNetworkReply : public QObject, public QHttpNetworkHeader
91{
92 Q_OBJECT
93public:
94
95 explicit QHttpNetworkReply(const QUrl &url = QUrl(), QObject *parent = nullptr);
96 virtual ~QHttpNetworkReply();
97
98 QUrl url() const override;
99 void setUrl(const QUrl &url) override;
100
101 int majorVersion() const override;
102 int minorVersion() const override;
103
104 qint64 contentLength() const override;
105 void setContentLength(qint64 length) override;
106
107 QList<QPair<QByteArray, QByteArray> > header() const override;
108 QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue = QByteArray()) const override;
109 void setHeaderField(const QByteArray &name, const QByteArray &data) override;
110 void parseHeader(const QByteArray &header); // mainly for testing
111
112 QHttpNetworkRequest request() const;
113 void setRequest(const QHttpNetworkRequest &request);
114
115 int statusCode() const;
116 void setStatusCode(int code);
117
118 QString errorString() const;
119 void setErrorString(const QString &error);
120
121 QNetworkReply::NetworkError errorCode() const;
122
123 QString reasonPhrase() const;
124
125 qint64 bytesAvailable() const;
126 qint64 bytesAvailableNextBlock() const;
127 bool readAnyAvailable() const;
128 QByteArray readAny();
129 QByteArray readAll();
130 QByteArray read(qint64 amount);
131 qint64 sizeNextBlock();
132 void setDownstreamLimited(bool t);
133 void setReadBufferSize(qint64 size);
134
135 bool supportsUserProvidedDownloadBuffer();
136 void setUserProvidedDownloadBuffer(char*);
137 char* userProvidedDownloadBuffer();
138
139 void abort();
140
141 bool isAborted() const;
142 bool isFinished() const;
143
144 bool isPipeliningUsed() const;
145 bool isHttp2Used() const;
146 void setHttp2WasUsed(bool h2Used);
147 qint64 removedContentLength() const;
148
149 bool isRedirecting() const;
150
151 QHttpNetworkConnection* connection();
152
153 QUrl redirectUrl() const;
154 void setRedirectUrl(const QUrl &url);
155
156 static bool isHttpRedirect(int statusCode);
157
158 bool isCompressed() const;
159
160#ifndef QT_NO_SSL
161 QSslConfiguration sslConfiguration() const;
162 void setSslConfiguration(const QSslConfiguration &config);
163 void ignoreSslErrors();
164 void ignoreSslErrors(const QList<QSslError> &errors);
165
166Q_SIGNALS:
167 void encrypted();
168 void sslErrors(const QList<QSslError> &errors);
169 void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator);
170#endif
171
172Q_SIGNALS:
173 void readyRead();
174 void finished();
175 void finishedWithError(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
176 void headerChanged();
177 void dataReadProgress(qint64 done, qint64 total);
178 void dataSendProgress(qint64 done, qint64 total);
179 void cacheCredentials(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
180#ifndef QT_NO_NETWORKPROXY
181 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
182#endif
183 void authenticationRequired(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
184 void redirected(const QUrl &url, int httpStatus, int maxRedirectsRemaining);
185private:
186 Q_DECLARE_PRIVATE(QHttpNetworkReply)
187 friend class QHttpSocketEngine;
188 friend class QHttpNetworkConnection;
189 friend class QHttpNetworkConnectionPrivate;
190 friend class QHttpNetworkConnectionChannel;
191 friend class QHttp2ProtocolHandler;
192 friend class QHttpProtocolHandler;
193 friend class QSpdyProtocolHandler;
194};
195
196
197class Q_AUTOTEST_EXPORT QHttpNetworkReplyPrivate : public QObjectPrivate, public QHttpNetworkHeaderPrivate
198{
199public:
200 QHttpNetworkReplyPrivate(const QUrl &newUrl = QUrl());
201 ~QHttpNetworkReplyPrivate();
202 qint64 readStatus(QAbstractSocket *socket);
203 bool parseStatus(const QByteArray &status);
204 qint64 readHeader(QAbstractSocket *socket);
205 void parseHeader(const QByteArray &header);
206 qint64 readBody(QAbstractSocket *socket, QByteDataBuffer *out);
207 qint64 readBodyVeryFast(QAbstractSocket *socket, char *b);
208 qint64 readBodyFast(QAbstractSocket *socket, QByteDataBuffer *rb);
209 bool findChallenge(bool forProxy, QByteArray &challenge) const;
210 QAuthenticatorPrivate::Method authenticationMethod(bool isProxy) const;
211 void clear();
212 void clearHttpLayerInformation();
213
214 qint64 readReplyBodyRaw(QAbstractSocket *in, QByteDataBuffer *out, qint64 size);
215 qint64 readReplyBodyChunked(QAbstractSocket *in, QByteDataBuffer *out);
216 qint64 getChunkSize(QAbstractSocket *in, qint64 *chunkSize);
217
218 bool isRedirecting() const;
219 bool shouldEmitSignals();
220 bool expectContent();
221 void eraseData();
222
223 qint64 bytesAvailable() const;
224 bool isChunked();
225 bool isConnectionCloseEnabled();
226
227 bool isCompressed() const;
228 void removeAutoDecompressHeader();
229
230 enum ReplyState {
231 NothingDoneState,
232 ReadingStatusState,
233 ReadingHeaderState,
234 ReadingDataState,
235 AllDoneState,
236 SPDYSYNSent,
237 SPDYUploading,
238 SPDYHalfClosed,
239 SPDYClosed,
240 Aborted
241 } state;
242
243 QHttpNetworkRequest request;
244 bool ssl;
245 int statusCode;
246 int majorVersion;
247 int minorVersion;
248 QString errorString;
249 QString reasonPhrase;
250 qint64 bodyLength;
251 qint64 contentRead;
252 qint64 totalProgress;
253 QByteArray fragment; // used for header, status, chunk header etc, not for reply data
254 bool chunkedTransferEncoding;
255 bool connectionCloseEnabled;
256 bool forceConnectionCloseEnabled;
257 bool lastChunkRead;
258 qint64 currentChunkSize;
259 qint64 currentChunkRead;
260 qint64 readBufferMaxSize;
261 qint64 totallyUploadedData; // HTTP/2
262 qint64 removedContentLength;
263 QPointer<QHttpNetworkConnection> connection;
264 QPointer<QHttpNetworkConnectionChannel> connectionChannel;
265 QNetworkReply::NetworkError httpErrorCode = QNetworkReply::NoError;
266
267 bool autoDecompress;
268
269 QByteDataBuffer responseData; // uncompressed body
270 bool requestIsPrepared;
271
272 bool pipeliningUsed;
273 bool h2Used;
274 bool downstreamLimited;
275
276 char* userProvidedDownloadBuffer;
277 QUrl redirectUrl;
278
279 QDecompressHelper decompressHelper;
280};
281
282
283
284
285QT_END_NAMESPACE
286
287#endif // QHTTPNETWORKREPLY_H
288