1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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 QHttpNetworkConnectionChannel;
|
---|
90 | class QHttpNetworkRequest;
|
---|
91 | class QHttpNetworkConnectionPrivate;
|
---|
92 | class QHttpNetworkReplyPrivate;
|
---|
93 | class Q_AUTOTEST_EXPORT QHttpNetworkReply : public QObject, public QHttpNetworkHeader
|
---|
94 | {
|
---|
95 | Q_OBJECT
|
---|
96 | public:
|
---|
97 |
|
---|
98 | explicit QHttpNetworkReply(const QUrl &url = QUrl(), QObject *parent = 0);
|
---|
99 | virtual ~QHttpNetworkReply();
|
---|
100 |
|
---|
101 | QUrl url() const;
|
---|
102 | void setUrl(const QUrl &url);
|
---|
103 |
|
---|
104 | int majorVersion() const;
|
---|
105 | int minorVersion() const;
|
---|
106 |
|
---|
107 | qint64 contentLength() const;
|
---|
108 | void setContentLength(qint64 length);
|
---|
109 |
|
---|
110 | QList<QPair<QByteArray, QByteArray> > header() const;
|
---|
111 | QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue = QByteArray()) const;
|
---|
112 | void setHeaderField(const QByteArray &name, const QByteArray &data);
|
---|
113 | void parseHeader(const QByteArray &header); // mainly for testing
|
---|
114 |
|
---|
115 | QHttpNetworkRequest request() const;
|
---|
116 | void setRequest(const QHttpNetworkRequest &request);
|
---|
117 |
|
---|
118 | int statusCode() const;
|
---|
119 | void setStatusCode(int code);
|
---|
120 |
|
---|
121 | QString errorString() const;
|
---|
122 | void setErrorString(const QString &error);
|
---|
123 |
|
---|
124 | QString reasonPhrase() const;
|
---|
125 |
|
---|
126 | qint64 bytesAvailable() const;
|
---|
127 | qint64 bytesAvailableNextBlock() const;
|
---|
128 | QByteArray readAny();
|
---|
129 | QByteArray readAll();
|
---|
130 | void setDownstreamLimited(bool t);
|
---|
131 |
|
---|
132 | bool isFinished() const;
|
---|
133 |
|
---|
134 | bool isPipeliningUsed() const;
|
---|
135 |
|
---|
136 | QHttpNetworkConnection* connection();
|
---|
137 |
|
---|
138 | #ifndef QT_NO_OPENSSL
|
---|
139 | QSslConfiguration sslConfiguration() const;
|
---|
140 | void setSslConfiguration(const QSslConfiguration &config);
|
---|
141 | void ignoreSslErrors();
|
---|
142 | void ignoreSslErrors(const QList<QSslError> &errors);
|
---|
143 |
|
---|
144 | Q_SIGNALS:
|
---|
145 | void sslErrors(const QList<QSslError> &errors);
|
---|
146 | #endif
|
---|
147 |
|
---|
148 | Q_SIGNALS:
|
---|
149 | void readyRead();
|
---|
150 | void finished();
|
---|
151 | void finishedWithError(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
|
---|
152 | void headerChanged();
|
---|
153 | void dataReadProgress(int done, int total);
|
---|
154 | void dataSendProgress(qint64 done, qint64 total);
|
---|
155 | void cacheCredentials(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
|
---|
156 | #ifndef QT_NO_NETWORKPROXY
|
---|
157 | void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
|
---|
158 | #endif
|
---|
159 | void authenticationRequired(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
|
---|
160 | private:
|
---|
161 | Q_DECLARE_PRIVATE(QHttpNetworkReply)
|
---|
162 | friend class QHttpNetworkConnection;
|
---|
163 | friend class QHttpNetworkConnectionPrivate;
|
---|
164 | friend class QHttpNetworkConnectionChannel;
|
---|
165 | };
|
---|
166 |
|
---|
167 |
|
---|
168 | class QHttpNetworkReplyPrivate : public QObjectPrivate, public QHttpNetworkHeaderPrivate
|
---|
169 | {
|
---|
170 | public:
|
---|
171 | QHttpNetworkReplyPrivate(const QUrl &newUrl = QUrl());
|
---|
172 | ~QHttpNetworkReplyPrivate();
|
---|
173 | qint64 readStatus(QAbstractSocket *socket);
|
---|
174 | bool parseStatus(const QByteArray &status);
|
---|
175 | qint64 readHeader(QAbstractSocket *socket);
|
---|
176 | void parseHeader(const QByteArray &header);
|
---|
177 | qint64 readBody(QAbstractSocket *socket, QByteDataBuffer *out);
|
---|
178 | qint64 readBodyFast(QAbstractSocket *socket, QByteDataBuffer *rb);
|
---|
179 | bool findChallenge(bool forProxy, QByteArray &challenge) const;
|
---|
180 | QAuthenticatorPrivate::Method authenticationMethod(bool isProxy) const;
|
---|
181 | void clear();
|
---|
182 | void clearHttpLayerInformation();
|
---|
183 |
|
---|
184 | qint64 readReplyBodyRaw(QIODevice *in, QByteDataBuffer *out, qint64 size);
|
---|
185 | qint64 readReplyBodyChunked(QIODevice *in, QByteDataBuffer *out);
|
---|
186 | qint64 getChunkSize(QIODevice *in, qint64 *chunkSize);
|
---|
187 |
|
---|
188 | void appendUncompressedReplyData(QByteArray &qba);
|
---|
189 | void appendUncompressedReplyData(QByteDataBuffer &data);
|
---|
190 | void appendCompressedReplyData(QByteDataBuffer &data);
|
---|
191 |
|
---|
192 | bool shouldEmitSignals();
|
---|
193 | bool expectContent();
|
---|
194 | void eraseData();
|
---|
195 |
|
---|
196 | qint64 bytesAvailable() const;
|
---|
197 | bool isChunked();
|
---|
198 | bool isConnectionCloseEnabled();
|
---|
199 | bool isGzipped();
|
---|
200 | #ifndef QT_NO_COMPRESS
|
---|
201 | bool gzipCheckHeader(QByteArray &content, int &pos);
|
---|
202 | int gunzipBodyPartially(QByteArray &compressed, QByteArray &inflated);
|
---|
203 | #endif
|
---|
204 | void removeAutoDecompressHeader();
|
---|
205 |
|
---|
206 | enum ReplyState {
|
---|
207 | NothingDoneState,
|
---|
208 | ReadingStatusState,
|
---|
209 | ReadingHeaderState,
|
---|
210 | ReadingDataState,
|
---|
211 | AllDoneState
|
---|
212 | } state;
|
---|
213 |
|
---|
214 | QHttpNetworkRequest request;
|
---|
215 | int statusCode;
|
---|
216 | int majorVersion;
|
---|
217 | int minorVersion;
|
---|
218 | QString errorString;
|
---|
219 | QString reasonPhrase;
|
---|
220 | qint64 bodyLength;
|
---|
221 | qint64 contentRead;
|
---|
222 | qint64 totalProgress;
|
---|
223 | QByteArray fragment; // used for header, status, chunk header etc, not for reply data
|
---|
224 | bool chunkedTransferEncoding;
|
---|
225 | bool connectionCloseEnabled;
|
---|
226 | bool forceConnectionCloseEnabled;
|
---|
227 | qint64 currentChunkSize;
|
---|
228 | qint64 currentChunkRead;
|
---|
229 | QPointer<QHttpNetworkConnection> connection;
|
---|
230 | QPointer<QHttpNetworkConnectionChannel> connectionChannel;
|
---|
231 | bool initInflate;
|
---|
232 | bool streamEnd;
|
---|
233 | #ifndef QT_NO_COMPRESS
|
---|
234 | z_stream inflateStrm;
|
---|
235 | #endif
|
---|
236 | bool autoDecompress;
|
---|
237 |
|
---|
238 | QByteDataBuffer responseData; // uncompressed body
|
---|
239 | QByteArray compressedData; // compressed body (temporary)
|
---|
240 | bool requestIsPrepared;
|
---|
241 |
|
---|
242 | bool pipeliningUsed;
|
---|
243 | bool downstreamLimited;
|
---|
244 | };
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 |
|
---|
249 | QT_END_NAMESPACE
|
---|
250 |
|
---|
251 | //Q_DECLARE_METATYPE(QHttpNetworkReply)
|
---|
252 |
|
---|
253 | #endif // QT_NO_HTTP
|
---|
254 |
|
---|
255 |
|
---|
256 | #endif // QHTTPNETWORKREPLY_H
|
---|