| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #ifndef QHTTPNETWORKREPLY_H
|
|---|
| 43 | #define QHTTPNETWORKREPLY_H
|
|---|
| 44 |
|
|---|
| 45 | //
|
|---|
| 46 | // W A R N I N G
|
|---|
| 47 | // -------------
|
|---|
| 48 | //
|
|---|
| 49 | // This file is not part of the Qt API. It exists for the convenience
|
|---|
| 50 | // of the Network Access API. This header file may change from
|
|---|
| 51 | // version to version without notice, or even be removed.
|
|---|
| 52 | //
|
|---|
| 53 | // We mean it.
|
|---|
| 54 | //
|
|---|
| 55 | #include <qplatformdefs.h>
|
|---|
| 56 | #ifndef QT_NO_HTTP
|
|---|
| 57 |
|
|---|
| 58 | #ifndef QT_NO_COMPRESS
|
|---|
| 59 | # include <zlib.h>
|
|---|
| 60 | static const unsigned char gz_magic[2] = {0x1f, 0x8b}; // gzip magic header
|
|---|
| 61 | // gzip flag byte
|
|---|
| 62 | #define HEAD_CRC 0x02 // bit 1 set: header CRC present
|
|---|
| 63 | #define EXTRA_FIELD 0x04 // bit 2 set: extra field present
|
|---|
| 64 | #define ORIG_NAME 0x08 // bit 3 set: original file name present
|
|---|
| 65 | #define COMMENT 0x10 // bit 4 set: file comment present
|
|---|
| 66 | #define RESERVED 0xE0 // bits 5..7: reserved
|
|---|
| 67 | #define CHUNK 16384
|
|---|
| 68 | #endif
|
|---|
| 69 |
|
|---|
| 70 | #include <QtNetwork/qtcpsocket.h>
|
|---|
| 71 | // it's safe to include these even if SSL support is not enabled
|
|---|
| 72 | #include <QtNetwork/qsslsocket.h>
|
|---|
| 73 | #include <QtNetwork/qsslerror.h>
|
|---|
| 74 |
|
|---|
| 75 | #include <QtNetwork/qnetworkrequest.h>
|
|---|
| 76 | #include <QtNetwork/qnetworkreply.h>
|
|---|
| 77 | #include <qbuffer.h>
|
|---|
| 78 |
|
|---|
| 79 | #include <private/qobject_p.h>
|
|---|
| 80 | #include <private/qhttpnetworkheader_p.h>
|
|---|
| 81 | #include <private/qhttpnetworkrequest_p.h>
|
|---|
| 82 | #include <private/qauthenticator_p.h>
|
|---|
| 83 | #include <private/qringbuffer_p.h>
|
|---|
| 84 | #include <private/qbytedata_p.h>
|
|---|
| 85 |
|
|---|
| 86 | QT_BEGIN_NAMESPACE
|
|---|
| 87 |
|
|---|
| 88 | class QHttpNetworkConnection;
|
|---|
| 89 | class QHttpNetworkRequest;
|
|---|
| 90 | class QHttpNetworkConnectionPrivate;
|
|---|
| 91 | class QHttpNetworkReplyPrivate;
|
|---|
| 92 | class Q_AUTOTEST_EXPORT QHttpNetworkReply : public QObject, public QHttpNetworkHeader
|
|---|
| 93 | {
|
|---|
| 94 | Q_OBJECT
|
|---|
| 95 | public:
|
|---|
| 96 |
|
|---|
| 97 | explicit QHttpNetworkReply(const QUrl &url = QUrl(), QObject *parent = 0);
|
|---|
| 98 | virtual ~QHttpNetworkReply();
|
|---|
| 99 |
|
|---|
| 100 | QUrl url() const;
|
|---|
| 101 | void setUrl(const QUrl &url);
|
|---|
| 102 |
|
|---|
| 103 | int majorVersion() const;
|
|---|
| 104 | int minorVersion() const;
|
|---|
| 105 |
|
|---|
| 106 | qint64 contentLength() const;
|
|---|
| 107 | void setContentLength(qint64 length);
|
|---|
| 108 |
|
|---|
| 109 | QList<QPair<QByteArray, QByteArray> > header() const;
|
|---|
| 110 | QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue = QByteArray()) const;
|
|---|
| 111 | void setHeaderField(const QByteArray &name, const QByteArray &data);
|
|---|
| 112 | void parseHeader(const QByteArray &header); // mainly for testing
|
|---|
| 113 |
|
|---|
| 114 | QHttpNetworkRequest request() const;
|
|---|
| 115 | void setRequest(const QHttpNetworkRequest &request);
|
|---|
| 116 |
|
|---|
| 117 | int statusCode() const;
|
|---|
| 118 | void setStatusCode(int code);
|
|---|
| 119 |
|
|---|
| 120 | QString errorString() const;
|
|---|
| 121 | void setErrorString(const QString &error);
|
|---|
| 122 |
|
|---|
| 123 | QString reasonPhrase() const;
|
|---|
| 124 |
|
|---|
| 125 | qint64 bytesAvailable() const;
|
|---|
| 126 | qint64 bytesAvailableNextBlock() const;
|
|---|
| 127 | QByteArray readAny();
|
|---|
| 128 |
|
|---|
| 129 | bool isFinished() const;
|
|---|
| 130 |
|
|---|
| 131 | bool isPipeliningUsed() const;
|
|---|
| 132 |
|
|---|
| 133 | #ifndef QT_NO_OPENSSL
|
|---|
| 134 | QSslConfiguration sslConfiguration() const;
|
|---|
| 135 | void setSslConfiguration(const QSslConfiguration &config);
|
|---|
| 136 | void ignoreSslErrors();
|
|---|
| 137 | void ignoreSslErrors(const QList<QSslError> &errors);
|
|---|
| 138 |
|
|---|
| 139 | Q_SIGNALS:
|
|---|
| 140 | void sslErrors(const QList<QSslError> &errors);
|
|---|
| 141 | #endif
|
|---|
| 142 |
|
|---|
| 143 | Q_SIGNALS:
|
|---|
| 144 | void readyRead();
|
|---|
| 145 | void finished();
|
|---|
| 146 | void finishedWithError(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
|
|---|
| 147 | void headerChanged();
|
|---|
| 148 | void dataReadProgress(int done, int total);
|
|---|
| 149 | void dataSendProgress(qint64 done, qint64 total);
|
|---|
| 150 |
|
|---|
| 151 | private:
|
|---|
| 152 | Q_DECLARE_PRIVATE(QHttpNetworkReply)
|
|---|
| 153 | friend class QHttpNetworkConnection;
|
|---|
| 154 | friend class QHttpNetworkConnectionPrivate;
|
|---|
| 155 | friend class QHttpNetworkConnectionChannel;
|
|---|
| 156 | };
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | class QHttpNetworkReplyPrivate : public QObjectPrivate, public QHttpNetworkHeaderPrivate
|
|---|
| 160 | {
|
|---|
| 161 | public:
|
|---|
| 162 | QHttpNetworkReplyPrivate(const QUrl &newUrl = QUrl());
|
|---|
| 163 | ~QHttpNetworkReplyPrivate();
|
|---|
| 164 | qint64 readStatus(QAbstractSocket *socket);
|
|---|
| 165 | bool parseStatus(const QByteArray &status);
|
|---|
| 166 | qint64 readHeader(QAbstractSocket *socket);
|
|---|
| 167 | void parseHeader(const QByteArray &header);
|
|---|
| 168 | qint64 readBody(QAbstractSocket *socket, QByteDataBuffer *out);
|
|---|
| 169 | qint64 readBodyFast(QAbstractSocket *socket, QByteDataBuffer *rb);
|
|---|
| 170 | bool findChallenge(bool forProxy, QByteArray &challenge) const;
|
|---|
| 171 | QAuthenticatorPrivate::Method authenticationMethod(bool isProxy) const;
|
|---|
| 172 | void clear();
|
|---|
| 173 |
|
|---|
| 174 | qint64 readReplyBodyRaw(QIODevice *in, QByteDataBuffer *out, qint64 size);
|
|---|
| 175 | qint64 readReplyBodyChunked(QIODevice *in, QByteDataBuffer *out);
|
|---|
| 176 | qint64 getChunkSize(QIODevice *in, qint64 *chunkSize);
|
|---|
| 177 |
|
|---|
| 178 | void appendUncompressedReplyData(QByteArray &qba);
|
|---|
| 179 | void appendUncompressedReplyData(QByteDataBuffer &data);
|
|---|
| 180 | void appendCompressedReplyData(QByteDataBuffer &data);
|
|---|
| 181 |
|
|---|
| 182 | bool shouldEmitSignals();
|
|---|
| 183 | bool expectContent();
|
|---|
| 184 | void eraseData();
|
|---|
| 185 |
|
|---|
| 186 | qint64 bytesAvailable() const;
|
|---|
| 187 | bool isChunked();
|
|---|
| 188 | bool isConnectionCloseEnabled();
|
|---|
| 189 | bool isGzipped();
|
|---|
| 190 | #ifndef QT_NO_COMPRESS
|
|---|
| 191 | bool gzipCheckHeader(QByteArray &content, int &pos);
|
|---|
| 192 | int gunzipBodyPartially(QByteArray &compressed, QByteArray &inflated);
|
|---|
| 193 | #endif
|
|---|
| 194 | void removeAutoDecompressHeader();
|
|---|
| 195 |
|
|---|
| 196 | enum ReplyState {
|
|---|
| 197 | NothingDoneState,
|
|---|
| 198 | ReadingStatusState,
|
|---|
| 199 | ReadingHeaderState,
|
|---|
| 200 | ReadingDataState,
|
|---|
| 201 | AllDoneState
|
|---|
| 202 | } state;
|
|---|
| 203 |
|
|---|
| 204 | QHttpNetworkRequest request;
|
|---|
| 205 | int statusCode;
|
|---|
| 206 | int majorVersion;
|
|---|
| 207 | int minorVersion;
|
|---|
| 208 | QString errorString;
|
|---|
| 209 | QString reasonPhrase;
|
|---|
| 210 | qint64 bodyLength;
|
|---|
| 211 | qint64 contentRead;
|
|---|
| 212 | qint64 totalProgress;
|
|---|
| 213 | QByteArray fragment; // used for header, status, chunk header etc, not for reply data
|
|---|
| 214 | bool chunkedTransferEncoding;
|
|---|
| 215 | bool connectionCloseEnabled;
|
|---|
| 216 | bool forceConnectionCloseEnabled;
|
|---|
| 217 | qint64 currentChunkSize;
|
|---|
| 218 | qint64 currentChunkRead;
|
|---|
| 219 | QPointer<QHttpNetworkConnection> connection;
|
|---|
| 220 | bool initInflate;
|
|---|
| 221 | bool streamEnd;
|
|---|
| 222 | #ifndef QT_NO_COMPRESS
|
|---|
| 223 | z_stream inflateStrm;
|
|---|
| 224 | #endif
|
|---|
| 225 | bool autoDecompress;
|
|---|
| 226 |
|
|---|
| 227 | QByteDataBuffer responseData; // uncompressed body
|
|---|
| 228 | QByteArray compressedData; // compressed body (temporary)
|
|---|
| 229 | bool requestIsPrepared;
|
|---|
| 230 |
|
|---|
| 231 | bool pipeliningUsed;
|
|---|
| 232 | };
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | QT_END_NAMESPACE
|
|---|
| 238 |
|
|---|
| 239 | //Q_DECLARE_METATYPE(QHttpNetworkReply)
|
|---|
| 240 |
|
|---|
| 241 | #endif // QT_NO_HTTP
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 | #endif // QHTTPNETWORKREPLY_H
|
|---|