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