Qt 4.8
qhttp.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
52 
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;
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);
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);
113 
114 private:
116 };
117 
120 {
121 public:
124  QHttpResponseHeader(const QString &str);
125  QHttpResponseHeader(int code, const QString &text = QString(), int majorVer = 1, int minorVer = 1);
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:
144 };
145 
148 {
149 public:
151  QHttpRequestHeader(const QString &method, const QString &path, int majorVer = 1, int minorVer = 1);
152  QHttpRequestHeader(const QHttpRequestHeader &header);
153  QHttpRequestHeader(const QString &str);
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:
171 };
172 
174 {
175  Q_OBJECT
176 
177 public:
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 {
195  Closing
196  };
197  enum Error {
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:
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 
307 };
308 
309 #endif // QT_NO_HTTP
310 
312 
314 
315 #endif // QHTTP_H
int type
Definition: qmetatype.cpp:239
virtual bool parseLine(const QString &line, int number)
Parses the single HTTP header line line which has the format key, colon, space, value, and adds key/value to the headers.
Definition: qhttp.cpp:905
ConnectionMode
This enum is used to specify the mode of connection to use:
Definition: qhttp.h:178
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define error(msg)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual int minorVersion() const =0
Returns the minor protocol-version of the HTTP header.
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
QScopedPointer< QHttpHeaderPrivate > d_ptr
Definition: qhttp.h:112
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual int majorVersion() const =0
Returns the major protocol-version of the HTTP header.
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
QStringList keys
Q_GUI_EXPORT QString errorString(EGLint code=eglGetError())
Definition: qegl.cpp:743
The QNetworkProxy class provides a network layer proxy.
static QString toString(Register *reg, int type, bool *ok=0)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
unsigned __int64 quint64
Definition: qglobal.h:943
virtual QString toString() const
Returns a string representation of the HTTP header.
Definition: qhttp.cpp:923
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
unsigned short quint16
Definition: qglobal.h:936
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
The QTcpSocket class provides a TCP socket.
Definition: qtcpsocket.h:56
__int64 qint64
Definition: qglobal.h:942
The State element defines configurations of objects and properties.
quint16 values[128]
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QAuthenticator class provides an authentication object.
QHttpHeader & operator=(const QHttpHeader &h)
Assigns h and returns a reference to this http header.
Definition: qhttp.cpp:647
The QHttp class provides an implementation of the HTTP protocol.
Definition: qhttp.h:173
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
Error
This enum identifies the error that occurred.
Definition: qhttp.h:197
#define Q_NETWORK_EXPORT
Definition: qglobal.h:1452
int key
The QHttpRequestHeader class contains request header information for HTTP.
Definition: qhttp.h:147
The QHttpHeader class contains header information for HTTP.
Definition: qhttp.h:69
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
The QSslError class provides an SSL error.
Definition: qsslerror.h:58
#define class
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
#define QT_END_HEADER
Definition: qglobal.h:137
The QAbstractSocket class provides the base functionality common to all socket types.
The QHttpResponseHeader class contains response header information for HTTP.
Definition: qhttp.h:119
#define text
Definition: qobjectdefs.h:80
The QList class is a template class that provides lists.
Definition: qdatastream.h:62