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