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 |
39 | Q_MOC_INCLUDE(<QtNetwork/QNetworkProxy>) |
40 | #endif |
41 | Q_MOC_INCLUDE(<QtNetwork/QAuthenticator>) |
42 | |
43 | #include <private/qdecompresshelper_p.h> |
44 | #include <QtNetwork/qhttpheaders.h> |
45 | |
46 | #include <QtCore/qpointer.h> |
47 | |
48 | QT_REQUIRE_CONFIG(http); |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | class QHttpNetworkConnection; |
53 | class QHttpNetworkConnectionChannel; |
54 | class QHttpNetworkRequest; |
55 | class QHttpNetworkConnectionPrivate; |
56 | class QHttpNetworkReplyPrivate; |
57 | class Q_NETWORK_EXPORT QHttpNetworkReply : public QObject, public QHttpNetworkHeader |
58 | { |
59 | Q_OBJECT |
60 | public: |
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 () const override; |
77 | QByteArray (QByteArrayView name, const QByteArray &defaultValue = QByteArray()) const override; |
78 | void (const QByteArray &name, const QByteArray &data) override; |
79 | void (const QByteArray &name, const QByteArray &data); |
80 | void (QByteArrayView ); // 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 | |
137 | Q_SIGNALS: |
138 | void encrypted(); |
139 | void sslErrors(const QList<QSslError> &errors); |
140 | void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator); |
141 | #endif |
142 | |
143 | Q_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 (); |
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); |
158 | private: |
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 | |
170 | class Q_AUTOTEST_EXPORT QHttpNetworkReplyPrivate : public QObjectPrivate, public QHttpNetworkHeaderPrivate |
171 | { |
172 | public: |
173 | QHttpNetworkReplyPrivate(const QUrl &newUrl = QUrl()); |
174 | ~QHttpNetworkReplyPrivate(); |
175 | qint64 readStatus(QIODevice *socket); |
176 | bool parseStatus(QByteArrayView status); |
177 | qint64 (QIODevice *socket); |
178 | void (QByteArrayView ); |
179 | void (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 (); |
201 | |
202 | enum ReplyState { |
203 | NothingDoneState, |
204 | ReadingStatusState, |
205 | , |
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 | |
251 | QT_END_NAMESPACE |
252 | |
253 | #endif // QHTTPNETWORKREPLY_H |
254 | |