1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QHTTPNETWORKCONNECTION_H
|
---|
43 | #define QHTTPNETWORKCONNECTION_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 <QtNetwork/qnetworkrequest.h>
|
---|
56 | #include <QtNetwork/qnetworkreply.h>
|
---|
57 | #include <QtNetwork/qabstractsocket.h>
|
---|
58 |
|
---|
59 | #ifndef QT_NO_HTTP
|
---|
60 |
|
---|
61 | #ifndef QT_NO_OPENSSL
|
---|
62 | # include <QtNetwork/qsslsocket.h>
|
---|
63 | # include <QtNetwork/qsslerror.h>
|
---|
64 | #else
|
---|
65 | # include <QtNetwork/qtcpsocket.h>
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | QT_BEGIN_NAMESPACE
|
---|
69 |
|
---|
70 | class QHttpNetworkRequest;
|
---|
71 | class QHttpNetworkReply;
|
---|
72 |
|
---|
73 | class QHttpNetworkConnectionPrivate;
|
---|
74 | class Q_AUTOTEST_EXPORT QHttpNetworkConnection : public QObject
|
---|
75 | {
|
---|
76 | Q_OBJECT
|
---|
77 | public:
|
---|
78 |
|
---|
79 | QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0);
|
---|
80 | ~QHttpNetworkConnection();
|
---|
81 |
|
---|
82 | //The hostname to which this is connected to.
|
---|
83 | QString hostName() const;
|
---|
84 | //The HTTP port in use.
|
---|
85 | quint16 port() const;
|
---|
86 |
|
---|
87 | //add a new HTTP request through this connection
|
---|
88 | QHttpNetworkReply* sendRequest(const QHttpNetworkRequest &request);
|
---|
89 |
|
---|
90 | #ifndef QT_NO_NETWORKPROXY
|
---|
91 | //set the proxy for this connection
|
---|
92 | void setCacheProxy(const QNetworkProxy &networkProxy);
|
---|
93 | QNetworkProxy cacheProxy() const;
|
---|
94 | void setTransparentProxy(const QNetworkProxy &networkProxy);
|
---|
95 | QNetworkProxy transparentProxy() const;
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | //enable encryption
|
---|
99 | void enableEncryption();
|
---|
100 | bool isEncrypted() const;
|
---|
101 |
|
---|
102 | //authentication parameters
|
---|
103 | void setProxyAuthentication(QAuthenticator *authenticator);
|
---|
104 | void setAuthentication(const QString &domain, QAuthenticator *authenticator);
|
---|
105 |
|
---|
106 | #ifndef QT_NO_OPENSSL
|
---|
107 | void setSslConfiguration(const QSslConfiguration &config);
|
---|
108 | void ignoreSslErrors(int channel = -1);
|
---|
109 |
|
---|
110 | Q_SIGNALS:
|
---|
111 | void sslErrors(const QList<QSslError> &errors);
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | Q_SIGNALS:
|
---|
115 | #ifndef QT_NO_NETWORKPROXY
|
---|
116 | //cannot be used with queued connection.
|
---|
117 | void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator,
|
---|
118 | const QHttpNetworkConnection *connection = 0);
|
---|
119 | #endif
|
---|
120 | void authenticationRequired(const QHttpNetworkRequest &request, QAuthenticator *authenticator,
|
---|
121 | const QHttpNetworkConnection *connection = 0);
|
---|
122 | void error(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
|
---|
123 |
|
---|
124 | private:
|
---|
125 | Q_DECLARE_PRIVATE(QHttpNetworkConnection)
|
---|
126 | Q_DISABLE_COPY(QHttpNetworkConnection)
|
---|
127 | friend class QHttpNetworkReply;
|
---|
128 |
|
---|
129 | Q_PRIVATE_SLOT(d_func(), void _q_bytesWritten(qint64))
|
---|
130 | Q_PRIVATE_SLOT(d_func(), void _q_readyRead())
|
---|
131 | Q_PRIVATE_SLOT(d_func(), void _q_disconnected())
|
---|
132 | Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest())
|
---|
133 | Q_PRIVATE_SLOT(d_func(), void _q_restartPendingRequest())
|
---|
134 | Q_PRIVATE_SLOT(d_func(), void _q_connected())
|
---|
135 | Q_PRIVATE_SLOT(d_func(), void _q_error(QAbstractSocket::SocketError))
|
---|
136 | #ifndef QT_NO_NETWORKPROXY
|
---|
137 | Q_PRIVATE_SLOT(d_func(), void _q_proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*))
|
---|
138 | #endif
|
---|
139 | Q_PRIVATE_SLOT(d_func(), void _q_dataReadyReadBuffer())
|
---|
140 | Q_PRIVATE_SLOT(d_func(), void _q_dataReadyReadNoBuffer())
|
---|
141 |
|
---|
142 | #ifndef QT_NO_OPENSSL
|
---|
143 | Q_PRIVATE_SLOT(d_func(), void _q_encrypted())
|
---|
144 | Q_PRIVATE_SLOT(d_func(), void _q_sslErrors(const QList<QSslError>&))
|
---|
145 | #endif
|
---|
146 | };
|
---|
147 |
|
---|
148 | class Q_AUTOTEST_EXPORT QHttpNetworkHeader
|
---|
149 | {
|
---|
150 | public:
|
---|
151 | virtual ~QHttpNetworkHeader() {};
|
---|
152 | virtual QUrl url() const = 0;
|
---|
153 | virtual void setUrl(const QUrl &url) = 0;
|
---|
154 |
|
---|
155 | virtual int majorVersion() const = 0;
|
---|
156 | virtual int minorVersion() const = 0;
|
---|
157 |
|
---|
158 | virtual qint64 contentLength() const = 0;
|
---|
159 | virtual void setContentLength(qint64 length) = 0;
|
---|
160 |
|
---|
161 | virtual QList<QPair<QByteArray, QByteArray> > header() const = 0;
|
---|
162 | virtual QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue = QByteArray()) const = 0;
|
---|
163 | virtual void setHeaderField(const QByteArray &name, const QByteArray &data) = 0;
|
---|
164 | };
|
---|
165 |
|
---|
166 | class QHttpNetworkRequestPrivate;
|
---|
167 | class Q_AUTOTEST_EXPORT QHttpNetworkRequest: public QHttpNetworkHeader
|
---|
168 | {
|
---|
169 | public:
|
---|
170 | enum Operation {
|
---|
171 | Options,
|
---|
172 | Get,
|
---|
173 | Head,
|
---|
174 | Post,
|
---|
175 | Put,
|
---|
176 | Delete,
|
---|
177 | Trace,
|
---|
178 | Connect
|
---|
179 | };
|
---|
180 |
|
---|
181 | enum Priority {
|
---|
182 | HighPriority,
|
---|
183 | NormalPriority,
|
---|
184 | LowPriority
|
---|
185 | };
|
---|
186 |
|
---|
187 | QHttpNetworkRequest(const QUrl &url = QUrl(), Operation operation = Get, Priority priority = NormalPriority);
|
---|
188 | QHttpNetworkRequest(const QHttpNetworkRequest &other);
|
---|
189 | virtual ~QHttpNetworkRequest();
|
---|
190 | QHttpNetworkRequest &operator=(const QHttpNetworkRequest &other);
|
---|
191 | bool operator==(const QHttpNetworkRequest &other) const;
|
---|
192 |
|
---|
193 | QUrl url() const;
|
---|
194 | void setUrl(const QUrl &url);
|
---|
195 |
|
---|
196 | int majorVersion() const;
|
---|
197 | int minorVersion() const;
|
---|
198 |
|
---|
199 | qint64 contentLength() const;
|
---|
200 | void setContentLength(qint64 length);
|
---|
201 |
|
---|
202 | QList<QPair<QByteArray, QByteArray> > header() const;
|
---|
203 | QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue = QByteArray()) const;
|
---|
204 | void setHeaderField(const QByteArray &name, const QByteArray &data);
|
---|
205 |
|
---|
206 | Operation operation() const;
|
---|
207 | void setOperation(Operation operation);
|
---|
208 |
|
---|
209 | Priority priority() const;
|
---|
210 | void setPriority(Priority priority);
|
---|
211 |
|
---|
212 | QIODevice *data() const;
|
---|
213 | void setData(QIODevice *data);
|
---|
214 |
|
---|
215 | private:
|
---|
216 | QSharedDataPointer<QHttpNetworkRequestPrivate> d;
|
---|
217 | friend class QHttpNetworkRequestPrivate;
|
---|
218 | friend class QHttpNetworkConnectionPrivate;
|
---|
219 | };
|
---|
220 |
|
---|
221 | class QHttpNetworkReplyPrivate;
|
---|
222 | class Q_AUTOTEST_EXPORT QHttpNetworkReply : public QObject, public QHttpNetworkHeader
|
---|
223 | {
|
---|
224 | Q_OBJECT
|
---|
225 | public:
|
---|
226 |
|
---|
227 | explicit QHttpNetworkReply(const QUrl &url = QUrl(), QObject *parent = 0);
|
---|
228 | virtual ~QHttpNetworkReply();
|
---|
229 |
|
---|
230 | QUrl url() const;
|
---|
231 | void setUrl(const QUrl &url);
|
---|
232 |
|
---|
233 | int majorVersion() const;
|
---|
234 | int minorVersion() const;
|
---|
235 |
|
---|
236 | qint64 contentLength() const;
|
---|
237 | void setContentLength(qint64 length);
|
---|
238 |
|
---|
239 | QList<QPair<QByteArray, QByteArray> > header() const;
|
---|
240 | QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue = QByteArray()) const;
|
---|
241 | void setHeaderField(const QByteArray &name, const QByteArray &data);
|
---|
242 | void parseHeader(const QByteArray &header); // mainly for testing
|
---|
243 |
|
---|
244 | QHttpNetworkRequest request() const;
|
---|
245 | void setRequest(const QHttpNetworkRequest &request);
|
---|
246 |
|
---|
247 | int statusCode() const;
|
---|
248 | void setStatusCode(int code);
|
---|
249 |
|
---|
250 | QString errorString() const;
|
---|
251 | void setErrorString(const QString &error);
|
---|
252 |
|
---|
253 | QString reasonPhrase() const;
|
---|
254 |
|
---|
255 | qint64 bytesAvailable() const;
|
---|
256 | QByteArray read(qint64 maxSize = -1);
|
---|
257 |
|
---|
258 | bool isFinished() const;
|
---|
259 |
|
---|
260 | #ifndef QT_NO_OPENSSL
|
---|
261 | QSslConfiguration sslConfiguration() const;
|
---|
262 | void setSslConfiguration(const QSslConfiguration &config);
|
---|
263 | void ignoreSslErrors();
|
---|
264 |
|
---|
265 | Q_SIGNALS:
|
---|
266 | void sslErrors(const QList<QSslError> &errors);
|
---|
267 | #endif
|
---|
268 |
|
---|
269 | Q_SIGNALS:
|
---|
270 | void readyRead();
|
---|
271 | void finished();
|
---|
272 | void finishedWithError(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
|
---|
273 | void headerChanged();
|
---|
274 | void dataReadProgress(int done, int total);
|
---|
275 | void dataSendProgress(int done, int total);
|
---|
276 |
|
---|
277 | private:
|
---|
278 | Q_DECLARE_PRIVATE(QHttpNetworkReply)
|
---|
279 | friend class QHttpNetworkConnection;
|
---|
280 | friend class QHttpNetworkConnectionPrivate;
|
---|
281 | };
|
---|
282 |
|
---|
283 | QT_END_NAMESPACE
|
---|
284 |
|
---|
285 | Q_DECLARE_METATYPE(QHttpNetworkRequest)
|
---|
286 | //Q_DECLARE_METATYPE(QHttpNetworkReply)
|
---|
287 |
|
---|
288 | #endif // QT_NO_HTTP
|
---|
289 |
|
---|
290 | #endif
|
---|