- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qhttp.h

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    00004 ** All rights reserved.
    00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
    00006 **
    00007 ** This file is part of the QtNetwork module of the Qt Toolkit.
    00008 **
    00009 ** $QT_BEGIN_LICENSE:LGPL$
    00010 ** Commercial Usage
    00011 ** Licensees holding valid Qt Commercial licenses may use this file in
    00012 ** accordance with the Qt Commercial License Agreement provided with the
    00013 ** Software or, alternatively, in accordance with the terms contained in
    00014 ** a written agreement between you and Nokia.
    00015 **
    00016 ** GNU Lesser General Public License Usage
    00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
    00018 ** General Public License version 2.1 as published by the Free Software
    00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
    00020 ** packaging of this file.  Please review the following information to
    00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00023 **
    00024 ** In addition, as a special exception, Nokia gives you certain additional
    00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
    00027 **
    00028 ** GNU General Public License Usage
    00029 ** Alternatively, this file may be used under the terms of the GNU
    00030 ** General Public License version 3.0 as published by the Free Software
    00031 ** Foundation and appearing in the file LICENSE.GPL included in the
    00032 ** packaging of this file.  Please review the following information to
    00033 ** ensure the GNU General Public License version 3.0 requirements will be
    00034 ** met: http://www.gnu.org/copyleft/gpl.html.
    00035 **
    00036 ** If you have questions regarding the use of this file, please contact
    00037 ** Nokia at qt-info@nokia.com.
    00038 ** $QT_END_LICENSE$
    00039 **
    00040 ****************************************************************************/
    00041
    00042 #ifndef QHTTP_H
    00043 #define QHTTP_H
    00044
    00045 #include <QtCore/qobject.h>
    00046 #include <QtCore/qstringlist.h>
    00047 #include <QtCore/qmap.h>
    00048 #include <QtCore/qpair.h>
    00049 #include <QtCore/qscopedpointer.h>
    00050
    00051 QT_BEGIN_HEADER
    00052
    00053 QT_BEGIN_NAMESPACE
    00054
    00055 QT_MODULE(Network)
    00056
    00057 #ifndef QT_NO_HTTP
    00058
    00059 class QTcpSocket;
    00060 class QTimerEvent;
    00061 class QIODevice;
    00062 class QAuthenticator;
    00063 class QNetworkProxy;
    00064 class QSslError;
    00065
    00066 class QHttpPrivate;
    00067
    00068 class QHttpHeaderPrivate;
    00069 class Q_NETWORK_EXPORT QHttpHeader
    00070 {
    00071 public:
    00072     QHttpHeader();
    00073     QHttpHeader(const QHttpHeader &header);
    00074     QHttpHeader(const QString &str);
    00075     virtual ~QHttpHeader();
    00076
    00077     QHttpHeader &operator=(const QHttpHeader &h);
    00078
    00079     void setValue(const QString &key, const QString &value);
    00080     void setValues(const QList<QPair<QString, QString> > &values);
    00081     void addValue(const QString &key, const QString &value);
    00082     QList<QPair<QString, QString> > values() const;
    00083     bool hasKey(const QString &key) const;
    00084     QStringList keys() const;
    00085     QString value(const QString &key) const;
    00086     QStringList allValues(const QString &key) const;
    00087     void removeValue(const QString &key);
    00088     void removeAllValues(const QString &key);
    00089
    00090     // ### Qt 5: change to qint64
    00091     bool hasContentLength() const;
    00092     uint contentLength() const;
    00093     void setContentLength(int len);
    00094
    00095     bool hasContentType() const;
    00096     QString contentType() const;
    00097     void setContentType(const QString &type);
    00098
    00099     virtual QString toString() const;
    00100     bool isValid() const;
    00101
    00102     virtual int majorVersion() const = 0;
    00103     virtual int minorVersion() const = 0;
    00104
    00105 protected:
    00106     virtual bool parseLine(const QString &line, int number);
    00107     bool parse(const QString &str);
    00108     void setValid(bool);
    00109
    00110     QHttpHeader(QHttpHeaderPrivate &dd, const QString &str = QString());
    00111     QHttpHeader(QHttpHeaderPrivate &dd, const QHttpHeader &header);
    00112     QScopedPointer<QHttpHeaderPrivate> d_ptr;
    00113
    00114 private:
    00115     Q_DECLARE_PRIVATE(QHttpHeader)
    00116 };
    00117
    00118 class QHttpResponseHeaderPrivate;
    00119 class Q_NETWORK_EXPORT QHttpResponseHeader : public QHttpHeader
    00120 {
    00121 public:
    00122     QHttpResponseHeader();
    00123     QHttpResponseHeader(const QHttpResponseHeader &header);
    00124     QHttpResponseHeader(const QString &str);
    00125     QHttpResponseHeader(int code, const QString &text = QString(), int majorVer = 1, int minorVer = 1);
    00126     QHttpResponseHeader &operator=(const QHttpResponseHeader &header);
    00127
    00128     void setStatusLine(int code, const QString &text = QString(), int majorVer = 1, int minorVer = 1);
    00129
    00130     int statusCode() const;
    00131     QString reasonPhrase() const;
    00132
    00133     int majorVersion() const;
    00134     int minorVersion() const;
    00135
    00136     QString toString() const;
    00137
    00138 protected:
    00139     bool parseLine(const QString &line, int number);
    00140
    00141 private:
    00142     Q_DECLARE_PRIVATE(QHttpResponseHeader)
    00143     friend class QHttpPrivate;
    00144 };
    00145
    00146 class QHttpRequestHeaderPrivate;
    00147 class Q_NETWORK_EXPORT QHttpRequestHeader : public QHttpHeader
    00148 {
    00149 public:
    00150     QHttpRequestHeader();
    00151     QHttpRequestHeader(const QString &method, const QString &path, int majorVer = 1, int minorVer = 1);
    00152     QHttpRequestHeader(const QHttpRequestHeader &header);
    00153     QHttpRequestHeader(const QString &str);
    00154     QHttpRequestHeader &operator=(const QHttpRequestHeader &header);
    00155
    00156     void setRequest(const QString &method, const QString &path, int majorVer = 1, int minorVer = 1);
    00157
    00158     QString method() const;
    00159     QString path() const;
    00160
    00161     int majorVersion() const;
    00162     int minorVersion() const;
    00163
    00164     QString toString() const;
    00165
    00166 protected:
    00167     bool parseLine(const QString &line, int number);
    00168
    00169 private:
    00170     Q_DECLARE_PRIVATE(QHttpRequestHeader)
    00171 };
    00172
    00173 class Q_NETWORK_EXPORT QHttp : public QObject
    00174 {
    00175     Q_OBJECT
    00176
    00177 public:
    00178     enum ConnectionMode {
    00179         ConnectionModeHttp,
    00180         ConnectionModeHttps
    00181     };
    00182
    00183     explicit QHttp(QObject *parent = 0);
    00184     QHttp(const QString &hostname, quint16 port = 80, QObject *parent = 0);
    00185     QHttp(const QString &hostname, ConnectionMode mode, quint16 port = 0, QObject *parent = 0);
    00186     virtual ~QHttp();
    00187
    00188     enum State {
    00189         Unconnected,
    00190         HostLookup,
    00191         Connecting,
    00192         Sending,
    00193         Reading,
    00194         Connected,
    00195         Closing
    00196     };
    00197     enum Error {
    00198         NoError,
    00199         UnknownError,
    00200         HostNotFound,
    00201         ConnectionRefused,
    00202         UnexpectedClose,
    00203         InvalidResponseHeader,
    00204         WrongContentLength,
    00205         Aborted,
    00206         AuthenticationRequiredError,
    00207         ProxyAuthenticationRequiredError
    00208     };
    00209
    00210     int setHost(const QString &hostname, quint16 port = 80);
    00211     int setHost(const QString &hostname, ConnectionMode mode, quint16 port = 0);
    00212
    00213     int setSocket(QTcpSocket *socket);
    00214     int setUser(const QString &username, const QString &password = QString());
    00215
    00216 #ifndef QT_NO_NETWORKPROXY
    00217     int setProxy(const QString &host, int port,
    00218                  const QString &username = QString(),
    00219                  const QString &password = QString());
    00220     int setProxy(const QNetworkProxy &proxy);
    00221 #endif
    00222
    00223     int get(const QString &path, QIODevice *to=0);
    00224     int post(const QString &path, QIODevice *data, QIODevice *to=0 );
    00225     int post(const QString &path, const QByteArray &data, QIODevice *to=0);
    00226     int head(const QString &path);
    00227     int request(const QHttpRequestHeader &header, QIODevice *device=0, QIODevice *to=0);
    00228     int request(const QHttpRequestHeader &header, const QByteArray &data, QIODevice *to=0);
    00229
    00230     int closeConnection();
    00231     int close();
    00232
    00233     qint64 bytesAvailable() const;
    00234     qint64 read(char *data, qint64 maxlen);
    00235 #ifdef QT3_SUPPORT
    00236     inline QT3_SUPPORT qint64 readBlock(char *data, quint64 maxlen)
    00237     { return read(data, qint64(maxlen)); }
    00238 #endif
    00239     QByteArray readAll();
    00240
    00241     int currentId() const;
    00242     QIODevice *currentSourceDevice() const;
    00243     QIODevice *currentDestinationDevice() const;
    00244     QHttpRequestHeader currentRequest() const;
    00245     QHttpResponseHeader lastResponse() const;
    00246     bool hasPendingRequests() const;
    00247     void clearPendingRequests();
    00248
    00249     State state() const;
    00250
    00251     Error error() const;
    00252     QString errorString() const;
    00253
    00254 public Q_SLOTS:
    00255     void abort();
    00256
    00257 #ifndef QT_NO_OPENSSL
    00258     void ignoreSslErrors();
    00259 #endif
    00260
    00261 Q_SIGNALS:
    00262     void stateChanged(int);
    00263     void responseHeaderReceived(const QHttpResponseHeader &resp);
    00264     void readyRead(const QHttpResponseHeader &resp);
    00265
    00266     // ### Qt 5: change to qint64
    00267     void dataSendProgress(int, int);
    00268     void dataReadProgress(int, int);
    00269
    00270     void requestStarted(int);
    00271     void requestFinished(int, bool);
    00272     void done(bool);
    00273
    00274 #ifndef QT_NO_NETWORKPROXY
    00275     void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *);
    00276 #endif
    00277     void authenticationRequired(const QString &hostname, quint16 port, QAuthenticator *);
    00278
    00279 #ifndef QT_NO_OPENSSL
    00280     void sslErrors(const QList<QSslError> &errors);
    00281 #endif
    00282
    00283 private:
    00284     Q_DISABLE_COPY(QHttp)
    00285     Q_DECLARE_PRIVATE(QHttp)
    00286
    00287     Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest())
    00288     Q_PRIVATE_SLOT(d_func(), void _q_slotReadyRead())
    00289     Q_PRIVATE_SLOT(d_func(), void _q_slotConnected())
    00290     Q_PRIVATE_SLOT(d_func(), void _q_slotError(QAbstractSocket::SocketError))
    00291     Q_PRIVATE_SLOT(d_func(), void _q_slotClosed())
    00292     Q_PRIVATE_SLOT(d_func(), void _q_slotBytesWritten(qint64 numBytes))
    00293 #ifndef QT_NO_OPENSSL
    00294     Q_PRIVATE_SLOT(d_func(), void _q_slotEncryptedBytesWritten(qint64 numBytes))
    00295 #endif
    00296     Q_PRIVATE_SLOT(d_func(), void _q_slotDoFinished())
    00297     Q_PRIVATE_SLOT(d_func(), void _q_slotSendRequest())
    00298     Q_PRIVATE_SLOT(d_func(), void _q_continuePost())
    00299
    00300     friend class QHttpNormalRequest;
    00301     friend class QHttpSetHostRequest;
    00302     friend class QHttpSetSocketRequest;
    00303     friend class QHttpSetUserRequest;
    00304     friend class QHttpSetProxyRequest;
    00305     friend class QHttpCloseRequest;
    00306     friend class QHttpPGHRequest;
    00307 };
    00308
    00309 #endif // QT_NO_HTTP
    00310
    00311 QT_END_NAMESPACE
    00312
    00313 QT_END_HEADER
    00314
    00315 #endif // QHTTP_H