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#ifndef QT_NO_COMPRESS
59struct z_stream_s;
60#endif
61
62#include <QtNetwork/qtcpsocket.h>
63// it's safe to include these even if SSL support is not enabled
64#include <QtNetwork/qsslsocket.h>
65#include <QtNetwork/qsslerror.h>
66
67#include <QtNetwork/qnetworkrequest.h>
68#include <QtNetwork/qnetworkreply.h>
69#include <qbuffer.h>
70
71#include <private/qobject_p.h>
72#include <private/qhttpnetworkheader_p.h>
73#include <private/qhttpnetworkrequest_p.h>
74#include <private/qauthenticator_p.h>
75#include <private/qringbuffer_p.h>
76#include <private/qbytedata_p.h>
77
78QT_REQUIRE_CONFIG(http);
79
80QT_BEGIN_NAMESPACE
81
82class QHttpNetworkConnection;
83class QHttpNetworkConnectionChannel;
84class QHttpNetworkRequest;
85class QHttpNetworkConnectionPrivate;
86class QHttpNetworkReplyPrivate;
87class Q_AUTOTEST_EXPORT QHttpNetworkReply : public QObject, public QHttpNetworkHeader
88{
89 Q_OBJECT
90public:
91
92 explicit QHttpNetworkReply(const QUrl &url = QUrl(), QObject *parent = nullptr);
93 virtual ~QHttpNetworkReply();
94
95 QUrl url() const override;
96 void setUrl(const QUrl &url) override;
97
98 int majorVersion() const override;
99 int minorVersion() const override;
100
101 qint64 contentLength() const override;
102 void setContentLength(qint64 length) override;
103
104 QList<QPair<QByteArray, QByteArray> > header() const override;
105 QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue = QByteArray()) const override;
106 void setHeaderField(const QByteArray &name, const QByteArray &data) override;
107 void parseHeader(const QByteArray &header); // mainly for testing
108
109 QHttpNetworkRequest request() const;
110 void setRequest(const QHttpNetworkRequest &request);
111
112 int statusCode() const;
113 void setStatusCode(int code);
114
115 QString errorString() const;
116 void setErrorString(const QString &error);
117
118 QString reasonPhrase() const;
119
120 qint64 bytesAvailable() const;
121 qint64 bytesAvailableNextBlock() const;
122 bool readAnyAvailable() const;
123 QByteArray readAny();
124 QByteArray readAll();
125 QByteArray read(qint64 amount);
126 qint64 sizeNextBlock();
127 void setDownstreamLimited(bool t);
128 void setReadBufferSize(qint64 size);
129
130 bool supportsUserProvidedDownloadBuffer();
131 void setUserProvidedDownloadBuffer(char*);
132 char* userProvidedDownloadBuffer();
133
134 void abort();
135
136 bool isAborted() const;
137 bool isFinished() const;
138
139 bool isPipeliningUsed() const;
140 bool isSpdyUsed() const;
141 void setSpdyWasUsed(bool spdy);
142 qint64 removedContentLength() const;
143
144 bool isRedirecting() const;
145
146 QHttpNetworkConnection* connection();
147
148 QUrl redirectUrl() const;
149 void setRedirectUrl(const QUrl &url);
150
151 static bool isHttpRedirect(int statusCode);
152
153#ifndef QT_NO_SSL
154 QSslConfiguration sslConfiguration() const;
155 void setSslConfiguration(const QSslConfiguration &config);
156 void ignoreSslErrors();
157 void ignoreSslErrors(const QList<QSslError> &errors);
158
159Q_SIGNALS:
160 void encrypted();
161 void sslErrors(const QList<QSslError> &errors);
162 void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator);
163#endif
164
165Q_SIGNALS:
166 void readyRead();
167 void finished();
168 void finishedWithError(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
169 void headerChanged();
170 void dataReadProgress(qint64 done, qint64 total);
171 void dataSendProgress(qint64 done, qint64 total);
172 void cacheCredentials(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
173#ifndef QT_NO_NETWORKPROXY
174 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
175#endif
176 void authenticationRequired(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
177 void redirected(const QUrl &url, int httpStatus, int maxRedirectsRemaining);
178private:
179 Q_DECLARE_PRIVATE(QHttpNetworkReply)
180 friend class QHttpSocketEngine;
181 friend class QHttpNetworkConnection;
182 friend class QHttpNetworkConnectionPrivate;
183 friend class QHttpNetworkConnectionChannel;
184 friend class QHttp2ProtocolHandler;
185 friend class QHttpProtocolHandler;
186 friend class QSpdyProtocolHandler;
187};
188
189
190class Q_AUTOTEST_EXPORT QHttpNetworkReplyPrivate : public QObjectPrivate, public QHttpNetworkHeaderPrivate
191{
192public:
193 QHttpNetworkReplyPrivate(const QUrl &newUrl = QUrl());
194 ~QHttpNetworkReplyPrivate();
195 qint64 readStatus(QAbstractSocket *socket);
196 bool parseStatus(const QByteArray &status);
197 qint64 readHeader(QAbstractSocket *socket);
198 void parseHeader(const QByteArray &header);
199 qint64 readBody(QAbstractSocket *socket, QByteDataBuffer *out);
200 qint64 readBodyVeryFast(QAbstractSocket *socket, char *b);
201 qint64 readBodyFast(QAbstractSocket *socket, QByteDataBuffer *rb);
202 bool findChallenge(bool forProxy, QByteArray &challenge) const;
203 QAuthenticatorPrivate::Method authenticationMethod(bool isProxy) const;
204 void clear();
205 void clearHttpLayerInformation();
206
207 qint64 readReplyBodyRaw(QAbstractSocket *in, QByteDataBuffer *out, qint64 size);
208 qint64 readReplyBodyChunked(QAbstractSocket *in, QByteDataBuffer *out);
209 qint64 getChunkSize(QAbstractSocket *in, qint64 *chunkSize);
210
211 bool isRedirecting() const;
212 bool shouldEmitSignals();
213 bool expectContent();
214 void eraseData();
215
216 qint64 bytesAvailable() const;
217 bool isChunked();
218 bool isConnectionCloseEnabled();
219
220 bool isCompressed();
221 void removeAutoDecompressHeader();
222
223 enum ReplyState {
224 NothingDoneState,
225 ReadingStatusState,
226 ReadingHeaderState,
227 ReadingDataState,
228 AllDoneState,
229 SPDYSYNSent,
230 SPDYUploading,
231 SPDYHalfClosed,
232 SPDYClosed,
233 Aborted
234 } state;
235
236 QHttpNetworkRequest request;
237 bool ssl;
238 int statusCode;
239 int majorVersion;
240 int minorVersion;
241 QString errorString;
242 QString reasonPhrase;
243 qint64 bodyLength;
244 qint64 contentRead;
245 qint64 totalProgress;
246 QByteArray fragment; // used for header, status, chunk header etc, not for reply data
247 bool chunkedTransferEncoding;
248 bool connectionCloseEnabled;
249 bool forceConnectionCloseEnabled;
250 bool lastChunkRead;
251 qint64 currentChunkSize;
252 qint64 currentChunkRead;
253 qint64 readBufferMaxSize;
254 qint32 windowSizeDownload; // only for SPDY
255 qint32 windowSizeUpload; // only for SPDY
256 qint32 currentlyReceivedDataInWindow; // only for SPDY
257 qint32 currentlyUploadedDataInWindow; // only for SPDY
258 qint64 totallyUploadedData; // only for SPDY
259 qint64 removedContentLength;
260 QPointer<QHttpNetworkConnection> connection;
261 QPointer<QHttpNetworkConnectionChannel> connectionChannel;
262
263 bool autoDecompress;
264
265 QByteDataBuffer responseData; // uncompressed body
266 QByteArray compressedData; // compressed body (temporary)
267 bool requestIsPrepared;
268
269 bool pipeliningUsed;
270 bool spdyUsed;
271 bool downstreamLimited;
272
273 char* userProvidedDownloadBuffer;
274 QUrl redirectUrl;
275
276#ifndef QT_NO_COMPRESS
277 z_stream_s *inflateStrm;
278 int initializeInflateStream();
279 qint64 uncompressBodyData(QByteDataBuffer *in, QByteDataBuffer *out);
280#endif
281};
282
283
284
285
286QT_END_NAMESPACE
287
288#endif // QHTTPNETWORKREPLY_H
289