KVIrc  4.9.2
DeveloperAPIs
qhttp.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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 <qobject.h>
46 #include <qstringlist.h>
47 #include <qmap.h>
48 #include <qpair.h>
49 #include <qscopedpointer.h>
50 #include <qtcpsocket.h>
51 
52 QT_BEGIN_HEADER
53 
54 class QTcpSocket;
55 class QTimerEvent;
56 class QIODevice;
57 class QAuthenticator;
58 class QNetworkProxy;
59 class QSslError;
60 
61 class QHttpPrivate;
62 
63 class QHttpHeaderPrivate;
65 {
66 public:
67  QHttpHeader();
68  QHttpHeader(const QHttpHeader & header);
69  QHttpHeader(const QString & str);
70  virtual ~QHttpHeader();
71 
73 
74  void setValue(const QString & key, const QString & value);
75  void setValues(const QList<QPair<QString, QString>> & values);
76  void addValue(const QString & key, const QString & value);
77  QList<QPair<QString, QString>> values() const;
78  bool hasKey(const QString & key) const;
79  QStringList keys() const;
80  QString value(const QString & key) const;
81  QStringList allValues(const QString & key) const;
82  void removeValue(const QString & key);
83  void removeAllValues(const QString & key);
84 
85  // ### Qt 5: change to qint64
86  bool hasContentLength() const;
87  uint contentLength() const;
88  void setContentLength(int len);
89 
90  bool hasContentType() const;
91  QString contentType() const;
92  void setContentType(const QString & type);
93 
94  virtual QString toString() const;
95  bool isValid() const;
96 
97  virtual int majorVersion() const = 0;
98  virtual int minorVersion() const = 0;
99 
100 protected:
101  virtual bool parseLine(const QString & line, int number);
102  bool parse(const QString & str);
103  void setValid(bool);
104 
105  QHttpHeader(QHttpHeaderPrivate & dd, const QString & str = QString());
106  QHttpHeader(QHttpHeaderPrivate & dd, const QHttpHeader & header);
107  QScopedPointer<QHttpHeaderPrivate> d_ptr;
108 
109 private:
110  Q_DECLARE_PRIVATE(QHttpHeader)
111 };
112 
115 {
116 public:
119  QHttpResponseHeader(const QString & str);
120  QHttpResponseHeader(int code, const QString & text = QString(), int majorVer = 1, int minorVer = 1);
122 
123  void setStatusLine(int code, const QString & text = QString(), int majorVer = 1, int minorVer = 1);
124 
125  int statusCode() const;
126  QString reasonPhrase() const;
127 
128  int majorVersion() const;
129  int minorVersion() const;
130 
131  QString toString() const;
132 
133 protected:
134  bool parseLine(const QString & line, int number);
135 
136 private:
137  Q_DECLARE_PRIVATE(QHttpResponseHeader)
138  friend class QHttpPrivate;
139 };
140 
143 {
144 public:
146  QHttpRequestHeader(const QString & method, const QString & path, int majorVer = 1, int minorVer = 1);
147  QHttpRequestHeader(const QHttpRequestHeader & header);
148  QHttpRequestHeader(const QString & str);
150 
151  void setRequest(const QString & method, const QString & path, int majorVer = 1, int minorVer = 1);
152 
153  QString method() const;
154  QString path() const;
155 
156  int majorVersion() const;
157  int minorVersion() const;
158 
159  QString toString() const;
160 
161 protected:
162  bool parseLine(const QString & line, int number);
163 
164 private:
165  Q_DECLARE_PRIVATE(QHttpRequestHeader)
166 };
167 
168 class QHttp : public QObject
169 {
170  Q_OBJECT
171 
172 public:
174  {
176  ConnectionModeHttps
177  };
178 
179  explicit QHttp(QObject * parent = 0);
180  QHttp(const QString & hostname, quint16 port = 80, QObject * parent = 0);
181  QHttp(const QString & hostname, ConnectionMode mode, quint16 port = 0, QObject * parent = 0);
182  virtual ~QHttp();
183 
184  enum State
185  {
192  Closing
193  };
194  enum Error
195  {
205  ProxyAuthenticationRequiredError
206  };
207 
208  int setHost(const QString & hostname, quint16 port = 80);
209  int setHost(const QString & hostname, ConnectionMode mode, quint16 port = 0);
210 
211  int setSocket(QTcpSocket * socket);
212  int setUser(const QString & username, const QString & password = QString());
213 
214 #ifndef QT_NO_NETWORKPROXY
215  int setProxy(const QString & host, int port,
216  const QString & username = QString(),
217  const QString & password = QString());
218  int setProxy(const QNetworkProxy & proxy);
219 #endif
220 
221  int get(const QString & path, QIODevice * to = 0);
222  int post(const QString & path, QIODevice * data, QIODevice * to = 0);
223  int post(const QString & path, const QByteArray & data, QIODevice * to = 0);
224  int head(const QString & path);
225  int request(const QHttpRequestHeader & header, QIODevice * device = 0, QIODevice * to = 0);
226  int request(const QHttpRequestHeader & header, const QByteArray & data, QIODevice * to = 0);
227 
228  int closeConnection();
229  int close();
230 
231  qint64 bytesAvailable() const;
232  qint64 read(char * data, qint64 maxlen);
233  QByteArray readAll();
234 
235  int currentId() const;
236  QIODevice * currentSourceDevice() const;
237  QIODevice * currentDestinationDevice() const;
238  QHttpRequestHeader currentRequest() const;
239  QHttpResponseHeader lastResponse() const;
240  bool hasPendingRequests() const;
241  void clearPendingRequests();
242 
243  State state() const;
244 
245  Error error() const;
246  QString errorString() const;
247 
248 public Q_SLOTS:
249  void abort();
250 
251 #ifndef QT_NO_OPENSSL
252  void ignoreSslErrors();
253 #endif
254 
255 Q_SIGNALS:
256  void stateChanged(int);
257  void responseHeaderReceived(const QHttpResponseHeader & resp);
258  void readyRead(const QHttpResponseHeader & resp);
259 
260  // ### Qt 5: change to qint64
261  void dataSendProgress(int, int);
262  void dataReadProgress(int, int);
263 
264  void requestStarted(int);
265  void requestFinished(int, bool);
266  void done(bool);
267 
268 #ifndef QT_NO_NETWORKPROXY
269  void proxyAuthenticationRequired(const QNetworkProxy & proxy, QAuthenticator *);
270 #endif
271  void authenticationRequired(const QString & hostname, quint16 port, QAuthenticator *);
272 
273 #ifndef QT_NO_OPENSSL
274  void sslErrors(const QList<QSslError> & errors);
275 #endif
276 
277 private:
278  Q_DISABLE_COPY(QHttp)
279  QScopedPointer<QHttpPrivate> d;
280 
281  void _q_startNextRequest();
282  void _q_slotReadyRead();
283  void _q_slotConnected();
284  void _q_slotError(QAbstractSocket::SocketError e);
285  void _q_slotClosed();
286  void _q_slotBytesWritten(qint64 numBytes);
287 #ifndef QT_NO_OPENSSL
288  void _q_slotEncryptedBytesWritten(qint64 numBytes);
289 #endif
290  void _q_slotDoFinished();
291  void _q_slotSendRequest();
292  void _q_continuePost();
293 
294  friend class QHttpNormalRequest;
295  friend class QHttpSetHostRequest;
296  friend class QHttpSetSocketRequest;
297  friend class QHttpSetUserRequest;
298  friend class QHttpSetProxyRequest;
299  friend class QHttpCloseRequest;
300  friend class QHttpPGHRequest;
301 };
302 
303 QT_END_HEADER
304 
305 #endif // QHTTP_H
Definition: qhttp.h:186
State
Definition: qhttp.h:184
virtual bool parseLine(const QString &line, int number)
Definition: qhttp.cpp:938
ConnectionMode
Definition: qhttp.h:173
QStringList keys() const
Definition: qhttp.cpp:795
QString toString() const
Definition: qhttp.cpp:1249
bool parseLine(const QString &line, int number)
Definition: qhttp.cpp:1212
bool hasContentLength() const
Definition: qhttp.cpp:979
QList< QPair< QString, QString > > values() const
Definition: qhttp.cpp:884
Definition: qhttp.h:204
State
Definition: NotifierSettings.h:61
bool hasKey(const QString &key) const
Definition: qhttp.cpp:821
virtual int minorVersion() const =0
Definition: qhttp.h:189
Definition: qhttp.cpp:1046
virtual ~QHttpHeader()
Definition: qhttp.cpp:396
QScopedPointer< QHttpHeaderPrivate > d_ptr
Definition: qhttp.h:107
void setValid(bool)
Definition: qhttp.cpp:745
QString reasonPhrase() const
Definition: qhttp.cpp:1182
Definition: qhttp.h:196
virtual int majorVersion() const =0
void setValues(const QList< QPair< QString, QString >> &values)
Definition: qhttp.cpp:866
void setContentLength(int len)
Definition: qhttp.cpp:1001
Definition: qhttp.cpp:315
Definition: qhttp.cpp:475
QString type()
Definition: KviRuntimeInfo.cpp:646
Definition: qhttp.cpp:346
#define e
Definition: detector.cpp:69
int minorVersion() const
Definition: qhttp.cpp:1204
bool isValid() const
Definition: qhttp.cpp:680
bool parse(const QString &str)
Definition: qhttp.cpp:695
QString contentType() const
Definition: qhttp.cpp:1022
Definition: qhttp.h:199
Definition: qhttp.cpp:509
Definition: qhttp.h:187
void setStatusLine(int code, const QString &text=QString(), int majorVer=1, int minorVer=1)
Definition: qhttp.cpp:1156
void setValue(const QString &key, const QString &value)
Definition: qhttp.cpp:845
Definition: qhttp.h:197
Definition: qhttp.h:203
Definition: qhttp.h:200
#define h
Definition: detector.cpp:72
char s d
Definition: KviIrcNumericCodes.h:391
void done()
Definition: KviKvs.cpp:50
void setContentType(const QString &type)
Definition: qhttp.cpp:1041
Definition: qhttp.h:191
Definition: qhttp.h:175
Definition: qhttp.h:202
void addValue(const QString &key, const QString &value)
Definition: qhttp.cpp:875
Definition: qhttp.cpp:434
bool hasContentType() const
Definition: qhttp.cpp:1012
Definition: qhttp.h:190
Definition: qhttp.cpp:1256
QHttpResponseHeader & operator=(const QHttpResponseHeader &header)
Definition: qhttp.cpp:1108
QHttpHeader & operator=(const QHttpHeader &h)
Definition: qhttp.cpp:667
The QHttp class provides an implementation of the HTTP protocol.
Definition: qhttp.h:168
int majorVersion() const
Definition: qhttp.cpp:1193
Error
Definition: qhttp.h:194
Definition: qhttp.cpp:532
The QHttpRequestHeader class contains request header information for HTTP.
Definition: qhttp.h:142
QStringList allValues(const QString &key) const
Definition: qhttp.cpp:775
QHttpHeader()
Definition: qhttp.cpp:599
void removeAllValues(const QString &key)
Definition: qhttp.cpp:914
The QHttpHeader class contains header information for HTTP.
Definition: qhttp.h:64
virtual QString toString() const
Definition: qhttp.cpp:956
int statusCode() const
Definition: qhttp.cpp:1171
QString value(const QString &key) const
Definition: qhttp.cpp:757
Definition: qhttp.h:188
Definition: qhttp.cpp:95
The QHttpResponseHeader class contains response header information for HTTP.
Definition: qhttp.h:114
Definition: qhttp.h:198
QHttpResponseHeader()
Definition: qhttp.cpp:1086
void removeValue(const QString &key)
Definition: qhttp.cpp:895
Definition: qhttp.h:201
Definition: qhttp.cpp:203
QString hostname()
Definition: KviRuntimeInfo.cpp:720
uint contentLength() const
Definition: qhttp.cpp:990