1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 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 | void setDownstreamLimited(bool t);
|
---|
129 |
|
---|
130 | bool isFinished() const;
|
---|
131 |
|
---|
132 | bool isPipeliningUsed() const;
|
---|
133 |
|
---|
134 | #ifndef QT_NO_OPENSSL
|
---|
135 | QSslConfiguration sslConfiguration() const;
|
---|
136 | void setSslConfiguration(const QSslConfiguration &config);
|
---|
137 | void ignoreSslErrors();
|
---|
138 | void ignoreSslErrors(const QList<QSslError> &errors);
|
---|
139 |
|
---|
140 | Q_SIGNALS:
|
---|
141 | void sslErrors(const QList<QSslError> &errors);
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | Q_SIGNALS:
|
---|
145 | void readyRead();
|
---|
146 | void finished();
|
---|
147 | void finishedWithError(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
|
---|
148 | void headerChanged();
|
---|
149 | void dataReadProgress(int done, int total);
|
---|
150 | void dataSendProgress(qint64 done, qint64 total);
|
---|
151 |
|
---|
152 | private:
|
---|
153 | Q_DECLARE_PRIVATE(QHttpNetworkReply)
|
---|
154 | friend class QHttpNetworkConnection;
|
---|
155 | friend class QHttpNetworkConnectionPrivate;
|
---|
156 | friend class QHttpNetworkConnectionChannel;
|
---|
157 | };
|
---|
158 |
|
---|
159 |
|
---|
160 | class QHttpNetworkReplyPrivate : public QObjectPrivate, public QHttpNetworkHeaderPrivate
|
---|
161 | {
|
---|
162 | public:
|
---|
163 | QHttpNetworkReplyPrivate(const QUrl &newUrl = QUrl());
|
---|
164 | ~QHttpNetworkReplyPrivate();
|
---|
165 | qint64 readStatus(QAbstractSocket *socket);
|
---|
166 | bool parseStatus(const QByteArray &status);
|
---|
167 | qint64 readHeader(QAbstractSocket *socket);
|
---|
168 | void parseHeader(const QByteArray &header);
|
---|
169 | qint64 readBody(QAbstractSocket *socket, QByteDataBuffer *out);
|
---|
170 | qint64 readBodyFast(QAbstractSocket *socket, QByteDataBuffer *rb);
|
---|
171 | bool findChallenge(bool forProxy, QByteArray &challenge) const;
|
---|
172 | QAuthenticatorPrivate::Method authenticationMethod(bool isProxy) const;
|
---|
173 | void clear();
|
---|
174 |
|
---|
175 | qint64 readReplyBodyRaw(QIODevice *in, QByteDataBuffer *out, qint64 size);
|
---|
176 | qint64 readReplyBodyChunked(QIODevice *in, QByteDataBuffer *out);
|
---|
177 | qint64 getChunkSize(QIODevice *in, qint64 *chunkSize);
|
---|
178 |
|
---|
179 | void appendUncompressedReplyData(QByteArray &qba);
|
---|
180 | void appendUncompressedReplyData(QByteDataBuffer &data);
|
---|
181 | void appendCompressedReplyData(QByteDataBuffer &data);
|
---|
182 |
|
---|
183 | bool shouldEmitSignals();
|
---|
184 | bool expectContent();
|
---|
185 | void eraseData();
|
---|
186 |
|
---|
187 | qint64 bytesAvailable() const;
|
---|
188 | bool isChunked();
|
---|
189 | bool isConnectionCloseEnabled();
|
---|
190 | bool isGzipped();
|
---|
191 | #ifndef QT_NO_COMPRESS
|
---|
192 | bool gzipCheckHeader(QByteArray &content, int &pos);
|
---|
193 | int gunzipBodyPartially(QByteArray &compressed, QByteArray &inflated);
|
---|
194 | #endif
|
---|
195 | void removeAutoDecompressHeader();
|
---|
196 |
|
---|
197 | enum ReplyState {
|
---|
198 | NothingDoneState,
|
---|
199 | ReadingStatusState,
|
---|
200 | ReadingHeaderState,
|
---|
201 | ReadingDataState,
|
---|
202 | AllDoneState
|
---|
203 | } state;
|
---|
204 |
|
---|
205 | QHttpNetworkRequest request;
|
---|
206 | int statusCode;
|
---|
207 | int majorVersion;
|
---|
208 | int minorVersion;
|
---|
209 | QString errorString;
|
---|
210 | QString reasonPhrase;
|
---|
211 | qint64 bodyLength;
|
---|
212 | qint64 contentRead;
|
---|
213 | qint64 totalProgress;
|
---|
214 | QByteArray fragment; // used for header, status, chunk header etc, not for reply data
|
---|
215 | bool chunkedTransferEncoding;
|
---|
216 | bool connectionCloseEnabled;
|
---|
217 | bool forceConnectionCloseEnabled;
|
---|
218 | qint64 currentChunkSize;
|
---|
219 | qint64 currentChunkRead;
|
---|
220 | QPointer<QHttpNetworkConnection> connection;
|
---|
221 | bool initInflate;
|
---|
222 | bool streamEnd;
|
---|
223 | #ifndef QT_NO_COMPRESS
|
---|
224 | z_stream inflateStrm;
|
---|
225 | #endif
|
---|
226 | bool autoDecompress;
|
---|
227 |
|
---|
228 | QByteDataBuffer responseData; // uncompressed body
|
---|
229 | QByteArray compressedData; // compressed body (temporary)
|
---|
230 | bool requestIsPrepared;
|
---|
231 |
|
---|
232 | bool pipeliningUsed;
|
---|
233 | bool downstreamLimited;
|
---|
234 | };
|
---|
235 |
|
---|
236 |
|
---|
237 |
|
---|
238 |
|
---|
239 | QT_END_NAMESPACE
|
---|
240 |
|
---|
241 | //Q_DECLARE_METATYPE(QHttpNetworkReply)
|
---|
242 |
|
---|
243 | #endif // QT_NO_HTTP
|
---|
244 |
|
---|
245 |
|
---|
246 | #endif // QHTTPNETWORKREPLY_H
|
---|