| 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 QHTTP_H
|
|---|
| 43 | #define QHTTP_H
|
|---|
| 44 |
|
|---|
| 45 | #include <QtCore/qobject.h>
|
|---|
| 46 | #include <QtCore/qstringlist.h>
|
|---|
| 47 | #include <QtCore/qmap.h>
|
|---|
| 48 | #include <QtCore/qpair.h>
|
|---|
| 49 | #include <QtCore/qscopedpointer.h>
|
|---|
| 50 |
|
|---|
| 51 | QT_BEGIN_HEADER
|
|---|
| 52 |
|
|---|
| 53 | QT_BEGIN_NAMESPACE
|
|---|
| 54 |
|
|---|
| 55 | QT_MODULE(Network)
|
|---|
| 56 |
|
|---|
| 57 | #ifndef QT_NO_HTTP
|
|---|
| 58 |
|
|---|
| 59 | class QTcpSocket;
|
|---|
| 60 | class QTimerEvent;
|
|---|
| 61 | class QIODevice;
|
|---|
| 62 | class QAuthenticator;
|
|---|
| 63 | class QNetworkProxy;
|
|---|
| 64 | class QSslError;
|
|---|
| 65 |
|
|---|
| 66 | class QHttpPrivate;
|
|---|
| 67 |
|
|---|
| 68 | class QHttpHeaderPrivate;
|
|---|
| 69 | class Q_NETWORK_EXPORT QHttpHeader
|
|---|
| 70 | {
|
|---|
| 71 | public:
|
|---|
| 72 | QHttpHeader();
|
|---|
| 73 | QHttpHeader(const QHttpHeader &header);
|
|---|
| 74 | QHttpHeader(const QString &str);
|
|---|
| 75 | virtual ~QHttpHeader();
|
|---|
| 76 |
|
|---|
| 77 | QHttpHeader &operator=(const QHttpHeader &h);
|
|---|
| 78 |
|
|---|
| 79 | void setValue(const QString &key, const QString &value);
|
|---|
| 80 | void setValues(const QList<QPair<QString, QString> > &values);
|
|---|
| 81 | void addValue(const QString &key, const QString &value);
|
|---|
| 82 | QList<QPair<QString, QString> > values() const;
|
|---|
| 83 | bool hasKey(const QString &key) const;
|
|---|
| 84 | QStringList keys() const;
|
|---|
| 85 | QString value(const QString &key) const;
|
|---|
| 86 | QStringList allValues(const QString &key) const;
|
|---|
| 87 | void removeValue(const QString &key);
|
|---|
| 88 | void removeAllValues(const QString &key);
|
|---|
| 89 |
|
|---|
| 90 | // ### Qt 5: change to qint64
|
|---|
| 91 | bool hasContentLength() const;
|
|---|
| 92 | uint contentLength() const;
|
|---|
| 93 | void setContentLength(int len);
|
|---|
| 94 |
|
|---|
| 95 | bool hasContentType() const;
|
|---|
| 96 | QString contentType() const;
|
|---|
| 97 | void setContentType(const QString &type);
|
|---|
| 98 |
|
|---|
| 99 | virtual QString toString() const;
|
|---|
| 100 | bool isValid() const;
|
|---|
| 101 |
|
|---|
| 102 | virtual int majorVersion() const = 0;
|
|---|
| 103 | virtual int minorVersion() const = 0;
|
|---|
| 104 |
|
|---|
| 105 | protected:
|
|---|
| 106 | virtual bool parseLine(const QString &line, int number);
|
|---|
| 107 | bool parse(const QString &str);
|
|---|
| 108 | void setValid(bool);
|
|---|
| 109 |
|
|---|
| 110 | QHttpHeader(QHttpHeaderPrivate &dd, const QString &str = QString());
|
|---|
| 111 | QHttpHeader(QHttpHeaderPrivate &dd, const QHttpHeader &header);
|
|---|
| 112 | QScopedPointer<QHttpHeaderPrivate> d_ptr;
|
|---|
| 113 |
|
|---|
| 114 | private:
|
|---|
| 115 | Q_DECLARE_PRIVATE(QHttpHeader)
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 | class QHttpResponseHeaderPrivate;
|
|---|
| 119 | class Q_NETWORK_EXPORT QHttpResponseHeader : public QHttpHeader
|
|---|
| 120 | {
|
|---|
| 121 | public:
|
|---|
| 122 | QHttpResponseHeader();
|
|---|
| 123 | QHttpResponseHeader(const QHttpResponseHeader &header);
|
|---|
| 124 | QHttpResponseHeader(const QString &str);
|
|---|
| 125 | QHttpResponseHeader(int code, const QString &text = QString(), int majorVer = 1, int minorVer = 1);
|
|---|
| 126 | QHttpResponseHeader &operator=(const QHttpResponseHeader &header);
|
|---|
| 127 |
|
|---|
| 128 | void setStatusLine(int code, const QString &text = QString(), int majorVer = 1, int minorVer = 1);
|
|---|
| 129 |
|
|---|
| 130 | int statusCode() const;
|
|---|
| 131 | QString reasonPhrase() const;
|
|---|
| 132 |
|
|---|
| 133 | int majorVersion() const;
|
|---|
| 134 | int minorVersion() const;
|
|---|
| 135 |
|
|---|
| 136 | QString toString() const;
|
|---|
| 137 |
|
|---|
| 138 | protected:
|
|---|
| 139 | bool parseLine(const QString &line, int number);
|
|---|
| 140 |
|
|---|
| 141 | private:
|
|---|
| 142 | Q_DECLARE_PRIVATE(QHttpResponseHeader)
|
|---|
| 143 | friend class QHttpPrivate;
|
|---|
| 144 | };
|
|---|
| 145 |
|
|---|
| 146 | class QHttpRequestHeaderPrivate;
|
|---|
| 147 | class Q_NETWORK_EXPORT QHttpRequestHeader : public QHttpHeader
|
|---|
| 148 | {
|
|---|
| 149 | public:
|
|---|
| 150 | QHttpRequestHeader();
|
|---|
| 151 | QHttpRequestHeader(const QString &method, const QString &path, int majorVer = 1, int minorVer = 1);
|
|---|
| 152 | QHttpRequestHeader(const QHttpRequestHeader &header);
|
|---|
| 153 | QHttpRequestHeader(const QString &str);
|
|---|
| 154 | QHttpRequestHeader &operator=(const QHttpRequestHeader &header);
|
|---|
| 155 |
|
|---|
| 156 | void setRequest(const QString &method, const QString &path, int majorVer = 1, int minorVer = 1);
|
|---|
| 157 |
|
|---|
| 158 | QString method() const;
|
|---|
| 159 | QString path() const;
|
|---|
| 160 |
|
|---|
| 161 | int majorVersion() const;
|
|---|
| 162 | int minorVersion() const;
|
|---|
| 163 |
|
|---|
| 164 | QString toString() const;
|
|---|
| 165 |
|
|---|
| 166 | protected:
|
|---|
| 167 | bool parseLine(const QString &line, int number);
|
|---|
| 168 |
|
|---|
| 169 | private:
|
|---|
| 170 | Q_DECLARE_PRIVATE(QHttpRequestHeader)
|
|---|
| 171 | };
|
|---|
| 172 |
|
|---|
| 173 | class Q_NETWORK_EXPORT QHttp : public QObject
|
|---|
| 174 | {
|
|---|
| 175 | Q_OBJECT
|
|---|
| 176 |
|
|---|
| 177 | public:
|
|---|
| 178 | enum ConnectionMode {
|
|---|
| 179 | ConnectionModeHttp,
|
|---|
| 180 | ConnectionModeHttps
|
|---|
| 181 | };
|
|---|
| 182 |
|
|---|
| 183 | explicit QHttp(QObject *parent = 0);
|
|---|
| 184 | QHttp(const QString &hostname, quint16 port = 80, QObject *parent = 0);
|
|---|
| 185 | QHttp(const QString &hostname, ConnectionMode mode, quint16 port = 0, QObject *parent = 0);
|
|---|
| 186 | virtual ~QHttp();
|
|---|
| 187 |
|
|---|
| 188 | enum State {
|
|---|
| 189 | Unconnected,
|
|---|
| 190 | HostLookup,
|
|---|
| 191 | Connecting,
|
|---|
| 192 | Sending,
|
|---|
| 193 | Reading,
|
|---|
| 194 | Connected,
|
|---|
| 195 | Closing
|
|---|
| 196 | };
|
|---|
| 197 | enum Error {
|
|---|
| 198 | NoError,
|
|---|
| 199 | UnknownError,
|
|---|
| 200 | HostNotFound,
|
|---|
| 201 | ConnectionRefused,
|
|---|
| 202 | UnexpectedClose,
|
|---|
| 203 | InvalidResponseHeader,
|
|---|
| 204 | WrongContentLength,
|
|---|
| 205 | Aborted,
|
|---|
| 206 | AuthenticationRequiredError,
|
|---|
| 207 | ProxyAuthenticationRequiredError
|
|---|
| 208 | };
|
|---|
| 209 |
|
|---|
| 210 | int setHost(const QString &hostname, quint16 port = 80);
|
|---|
| 211 | int setHost(const QString &hostname, ConnectionMode mode, quint16 port = 0);
|
|---|
| 212 |
|
|---|
| 213 | int setSocket(QTcpSocket *socket);
|
|---|
| 214 | int setUser(const QString &username, const QString &password = QString());
|
|---|
| 215 |
|
|---|
| 216 | #ifndef QT_NO_NETWORKPROXY
|
|---|
| 217 | int setProxy(const QString &host, int port,
|
|---|
| 218 | const QString &username = QString(),
|
|---|
| 219 | const QString &password = QString());
|
|---|
| 220 | int setProxy(const QNetworkProxy &proxy);
|
|---|
| 221 | #endif
|
|---|
| 222 |
|
|---|
| 223 | int get(const QString &path, QIODevice *to=0);
|
|---|
| 224 | int post(const QString &path, QIODevice *data, QIODevice *to=0 );
|
|---|
| 225 | int post(const QString &path, const QByteArray &data, QIODevice *to=0);
|
|---|
| 226 | int head(const QString &path);
|
|---|
| 227 | int request(const QHttpRequestHeader &header, QIODevice *device=0, QIODevice *to=0);
|
|---|
| 228 | int request(const QHttpRequestHeader &header, const QByteArray &data, QIODevice *to=0);
|
|---|
| 229 |
|
|---|
| 230 | int closeConnection();
|
|---|
| 231 | int close();
|
|---|
| 232 |
|
|---|
| 233 | qint64 bytesAvailable() const;
|
|---|
| 234 | qint64 read(char *data, qint64 maxlen);
|
|---|
| 235 | #ifdef QT3_SUPPORT
|
|---|
| 236 | inline QT3_SUPPORT qint64 readBlock(char *data, quint64 maxlen)
|
|---|
| 237 | { return read(data, qint64(maxlen)); }
|
|---|
| 238 | #endif
|
|---|
| 239 | QByteArray readAll();
|
|---|
| 240 |
|
|---|
| 241 | int currentId() const;
|
|---|
| 242 | QIODevice *currentSourceDevice() const;
|
|---|
| 243 | QIODevice *currentDestinationDevice() const;
|
|---|
| 244 | QHttpRequestHeader currentRequest() const;
|
|---|
| 245 | QHttpResponseHeader lastResponse() const;
|
|---|
| 246 | bool hasPendingRequests() const;
|
|---|
| 247 | void clearPendingRequests();
|
|---|
| 248 |
|
|---|
| 249 | State state() const;
|
|---|
| 250 |
|
|---|
| 251 | Error error() const;
|
|---|
| 252 | QString errorString() const;
|
|---|
| 253 |
|
|---|
| 254 | public Q_SLOTS:
|
|---|
| 255 | void abort();
|
|---|
| 256 |
|
|---|
| 257 | #ifndef QT_NO_OPENSSL
|
|---|
| 258 | void ignoreSslErrors();
|
|---|
| 259 | #endif
|
|---|
| 260 |
|
|---|
| 261 | Q_SIGNALS:
|
|---|
| 262 | void stateChanged(int);
|
|---|
| 263 | void responseHeaderReceived(const QHttpResponseHeader &resp);
|
|---|
| 264 | void readyRead(const QHttpResponseHeader &resp);
|
|---|
| 265 |
|
|---|
| 266 | // ### Qt 5: change to qint64
|
|---|
| 267 | void dataSendProgress(int, int);
|
|---|
| 268 | void dataReadProgress(int, int);
|
|---|
| 269 |
|
|---|
| 270 | void requestStarted(int);
|
|---|
| 271 | void requestFinished(int, bool);
|
|---|
| 272 | void done(bool);
|
|---|
| 273 |
|
|---|
| 274 | #ifndef QT_NO_NETWORKPROXY
|
|---|
| 275 | void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *);
|
|---|
| 276 | #endif
|
|---|
| 277 | void authenticationRequired(const QString &hostname, quint16 port, QAuthenticator *);
|
|---|
| 278 |
|
|---|
| 279 | #ifndef QT_NO_OPENSSL
|
|---|
| 280 | void sslErrors(const QList<QSslError> &errors);
|
|---|
| 281 | #endif
|
|---|
| 282 |
|
|---|
| 283 | private:
|
|---|
| 284 | Q_DISABLE_COPY(QHttp)
|
|---|
| 285 | Q_DECLARE_PRIVATE(QHttp)
|
|---|
| 286 |
|
|---|
| 287 | Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest())
|
|---|
| 288 | Q_PRIVATE_SLOT(d_func(), void _q_slotReadyRead())
|
|---|
| 289 | Q_PRIVATE_SLOT(d_func(), void _q_slotConnected())
|
|---|
| 290 | Q_PRIVATE_SLOT(d_func(), void _q_slotError(QAbstractSocket::SocketError))
|
|---|
| 291 | Q_PRIVATE_SLOT(d_func(), void _q_slotClosed())
|
|---|
| 292 | Q_PRIVATE_SLOT(d_func(), void _q_slotBytesWritten(qint64 numBytes))
|
|---|
| 293 | #ifndef QT_NO_OPENSSL
|
|---|
| 294 | Q_PRIVATE_SLOT(d_func(), void _q_slotEncryptedBytesWritten(qint64 numBytes))
|
|---|
| 295 | #endif
|
|---|
| 296 | Q_PRIVATE_SLOT(d_func(), void _q_slotDoFinished())
|
|---|
| 297 | Q_PRIVATE_SLOT(d_func(), void _q_slotSendRequest())
|
|---|
| 298 | Q_PRIVATE_SLOT(d_func(), void _q_continuePost())
|
|---|
| 299 |
|
|---|
| 300 | friend class QHttpNormalRequest;
|
|---|
| 301 | friend class QHttpSetHostRequest;
|
|---|
| 302 | friend class QHttpSetSocketRequest;
|
|---|
| 303 | friend class QHttpSetUserRequest;
|
|---|
| 304 | friend class QHttpSetProxyRequest;
|
|---|
| 305 | friend class QHttpCloseRequest;
|
|---|
| 306 | friend class QHttpPGHRequest;
|
|---|
| 307 | };
|
|---|
| 308 |
|
|---|
| 309 | #endif // QT_NO_HTTP
|
|---|
| 310 |
|
|---|
| 311 | QT_END_NAMESPACE
|
|---|
| 312 |
|
|---|
| 313 | QT_END_HEADER
|
|---|
| 314 |
|
|---|
| 315 | #endif // QHTTP_H
|
|---|