KVIrc 5.2.6
Developer APIs
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
52class QTcpSocket;
53class QTimerEvent;
54class QIODevice;
55class QAuthenticator;
56class QNetworkProxy;
57class QSslError;
58
59class QHttpPrivate;
60
63{
64public:
66 QHttpHeader(const QHttpHeader & header);
67 QHttpHeader(const QString & str);
68 virtual ~QHttpHeader();
69
71
72 void setValue(const QString & key, const QString & value);
73 void setValues(const QList<QPair<QString, QString>> & values);
74 void addValue(const QString & key, const QString & value);
75 QList<QPair<QString, QString>> values() const;
76 bool hasKey(const QString & key) const;
77 QStringList keys() const;
78 QString value(const QString & key) const;
79 QStringList allValues(const QString & key) const;
80 void removeValue(const QString & key);
81 void removeAllValues(const QString & key);
82
83 // ### Qt 5: change to qint64
84 bool hasContentLength() const;
85 uint contentLength() const;
86 void setContentLength(int len);
87
88 bool hasContentType() const;
89 QString contentType() const;
90 void setContentType(const QString & type);
91
92 virtual QString toString() const;
93 bool isValid() const;
94
95 virtual int majorVersion() const = 0;
96 virtual int minorVersion() const = 0;
97
98protected:
99 virtual bool parseLine(const QString & line, int number);
100 bool parse(const QString & str);
101 void setValid(bool);
102
103 QHttpHeader(QHttpHeaderPrivate & dd, const QString & str = QString());
104 QHttpHeader(QHttpHeaderPrivate & dd, const QHttpHeader & header);
105 QScopedPointer<QHttpHeaderPrivate> d_ptr;
106
107private:
108 Q_DECLARE_PRIVATE(QHttpHeader)
109};
110
113{
114public:
117 QHttpResponseHeader(const QString & str);
118 QHttpResponseHeader(int code, const QString & text = QString(), int majorVer = 1, int minorVer = 1);
120
121 void setStatusLine(int code, const QString & text = QString(), int majorVer = 1, int minorVer = 1);
122
123 int statusCode() const;
124 QString reasonPhrase() const;
125
126 int majorVersion() const;
127 int minorVersion() const;
128
129 QString toString() const;
130
131protected:
132 bool parseLine(const QString & line, int number);
133
134private:
135 Q_DECLARE_PRIVATE(QHttpResponseHeader)
136 friend class QHttpPrivate;
137};
138
141{
142public:
144 QHttpRequestHeader(const QString & method, const QString & path, int majorVer = 1, int minorVer = 1);
146 QHttpRequestHeader(const QString & str);
148
149 void setRequest(const QString & method, const QString & path, int majorVer = 1, int minorVer = 1);
150
151 QString method() const;
152 QString path() const;
153
154 int majorVersion() const;
155 int minorVersion() const;
156
157 QString toString() const;
158
159protected:
160 bool parseLine(const QString & line, int number);
161
162private:
163 Q_DECLARE_PRIVATE(QHttpRequestHeader)
164};
165
166class QHttp : public QObject
167{
168 Q_OBJECT
169
170public:
172 {
174 ConnectionModeHttps
175 };
176
177 explicit QHttp(QObject * parent = 0);
178 QHttp(const QString & hostname, quint16 port = 80, QObject * parent = 0);
179 QHttp(const QString & hostname, ConnectionMode mode, quint16 port = 0, QObject * parent = 0);
180 virtual ~QHttp();
181
205
206 int setHost(const QString & hostname, quint16 port = 80);
207 int setHost(const QString & hostname, ConnectionMode mode, quint16 port = 0);
208
209 int setSocket(QTcpSocket * socket);
210 int setUser(const QString & username, const QString & password = QString());
211
212#ifndef QT_NO_NETWORKPROXY
213 int setProxy(const QString & host, int port,
214 const QString & username = QString(),
215 const QString & password = QString());
216 int setProxy(const QNetworkProxy & proxy);
217#endif
218
219 int get(const QString & path, QIODevice * to = 0);
220 int post(const QString & path, QIODevice * data, QIODevice * to = 0);
221 int post(const QString & path, const QByteArray & data, QIODevice * to = 0);
222 int head(const QString & path);
223 int request(const QHttpRequestHeader & header, QIODevice * device = 0, QIODevice * to = 0);
224 int request(const QHttpRequestHeader & header, const QByteArray & data, QIODevice * to = 0);
225
226 int closeConnection();
227 int close();
228
229 qint64 bytesAvailable() const;
230 qint64 read(char * data, qint64 maxlen);
231 QByteArray readAll();
232
233 int currentId() const;
234 QIODevice * currentSourceDevice() const;
235 QIODevice * currentDestinationDevice() const;
236 QHttpRequestHeader currentRequest() const;
237 QHttpResponseHeader lastResponse() const;
238 bool hasPendingRequests() const;
239 void clearPendingRequests();
240
241 State state() const;
242
243 Error error() const;
244 QString errorString() const;
245
246public Q_SLOTS:
247 void abort();
248
249#ifndef QT_NO_OPENSSL
250 void ignoreSslErrors();
251#endif
252
253Q_SIGNALS:
254 void stateChanged(int);
256 void readyRead(const QHttpResponseHeader & resp);
257
258 // ### Qt 5: change to qint64
259 void dataSendProgress(int, int);
260 void dataReadProgress(int, int);
261
262 void requestStarted(int);
263 void requestFinished(int, bool);
264 void done(bool);
265
266#ifndef QT_NO_NETWORKPROXY
267 void proxyAuthenticationRequired(const QNetworkProxy & proxy, QAuthenticator *);
268#endif
269 void authenticationRequired(const QString & hostname, quint16 port, QAuthenticator *);
270
271#ifndef QT_NO_OPENSSL
272 void sslErrors(const QList<QSslError> & errors);
273#endif
274
275private:
276 Q_DISABLE_COPY(QHttp)
277 QScopedPointer<QHttpPrivate> d;
278
279 void _q_startNextRequest();
280 void _q_slotReadyRead();
281 void _q_slotConnected();
282 void _q_slotError(QAbstractSocket::SocketError e);
283 void _q_slotClosed();
284 void _q_slotBytesWritten(qint64 numBytes);
285#ifndef QT_NO_OPENSSL
286 void _q_slotEncryptedBytesWritten(qint64 numBytes);
287#endif
288 void _q_slotDoFinished();
289 void _q_slotSendRequest();
290 void _q_continuePost();
291
292 friend class QHttpNormalRequest;
297 friend class QHttpCloseRequest;
298 friend class QHttpPGHRequest;
299};
300
301#endif // QHTTP_H
State
Definition NotifierSettings.h:62
Definition qhttp.cpp:525
Definition qhttp.cpp:547
The QHttpHeader class contains header information for HTTP.
Definition qhttp.h:63
bool hasKey(const QString &key) const
Definition qhttp.cpp:835
void setValue(const QString &key, const QString &value)
Definition qhttp.cpp:859
QString value(const QString &key) const
Definition qhttp.cpp:771
QScopedPointer< QHttpHeaderPrivate > d_ptr
Definition qhttp.h:105
void removeValue(const QString &key)
Definition qhttp.cpp:909
void setContentType(const QString &type)
Definition qhttp.cpp:1055
QHttpHeader & operator=(const QHttpHeader &h)
Definition qhttp.cpp:681
void addValue(const QString &key, const QString &value)
Definition qhttp.cpp:889
void removeAllValues(const QString &key)
Definition qhttp.cpp:928
QStringList keys() const
Definition qhttp.cpp:809
virtual int majorVersion() const =0
void setContentLength(int len)
Definition qhttp.cpp:1015
void setValues(const QList< QPair< QString, QString > > &values)
Definition qhttp.cpp:880
QString contentType() const
Definition qhttp.cpp:1036
virtual QString toString() const
Definition qhttp.cpp:970
bool isValid() const
Definition qhttp.cpp:694
virtual int minorVersion() const =0
QHttpHeader()
Definition qhttp.cpp:613
QStringList allValues(const QString &key) const
Definition qhttp.cpp:789
QList< QPair< QString, QString > > values() const
Definition qhttp.cpp:898
bool parse(const QString &str)
Definition qhttp.cpp:709
bool hasContentLength() const
Definition qhttp.cpp:993
virtual ~QHttpHeader()
uint contentLength() const
Definition qhttp.cpp:1004
bool hasContentType() const
Definition qhttp.cpp:1026
virtual bool parseLine(const QString &line, int number)
Definition qhttp.cpp:952
void setValid(bool)
Definition qhttp.cpp:759
Definition qhttp.cpp:215
Definition qhttp.cpp:327
Definition qhttp.cpp:98
Definition qhttp.cpp:1271
The QHttpRequestHeader class contains request header information for HTTP.
Definition qhttp.h:141
Definition qhttp.cpp:1061
The QHttpResponseHeader class contains response header information for HTTP.
Definition qhttp.h:113
bool parseLine(const QString &line, int number)
Definition qhttp.cpp:1226
int minorVersion() const
Definition qhttp.cpp:1218
QString reasonPhrase() const
Definition qhttp.cpp:1196
void setStatusLine(int code, const QString &text=QString(), int majorVer=1, int minorVer=1)
Definition qhttp.cpp:1170
QHttpResponseHeader()
Definition qhttp.cpp:1100
int statusCode() const
Definition qhttp.cpp:1185
int majorVersion() const
Definition qhttp.cpp:1207
QString toString() const
Definition qhttp.cpp:1263
QHttpResponseHeader & operator=(const QHttpResponseHeader &header)
Definition qhttp.cpp:1122
Definition qhttp.cpp:358
Definition qhttp.cpp:450
Definition qhttp.cpp:491
Definition qhttp.cpp:410
The QHttp class provides an implementation of the HTTP protocol.
Definition qhttp.h:167
void dataReadProgress(int, int)
void requestFinished(int, bool)
void done(bool)
Error
Definition qhttp.h:193
@ UnexpectedClose
Definition qhttp.h:198
@ HostNotFound
Definition qhttp.h:196
@ WrongContentLength
Definition qhttp.h:200
@ NoError
Definition qhttp.h:194
@ ConnectionRefused
Definition qhttp.h:197
@ UnknownError
Definition qhttp.h:195
@ Aborted
Definition qhttp.h:201
@ AuthenticationRequiredError
Definition qhttp.h:202
@ InvalidResponseHeader
Definition qhttp.h:199
State
Definition qhttp.h:183
@ Unconnected
Definition qhttp.h:184
@ Connected
Definition qhttp.h:189
@ Reading
Definition qhttp.h:188
@ HostLookup
Definition qhttp.h:185
@ Connecting
Definition qhttp.h:186
@ Sending
Definition qhttp.h:187
void stateChanged(int)
void responseHeaderReceived(const QHttpResponseHeader &resp)
void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *)
void dataSendProgress(int, int)
void sslErrors(const QList< QSslError > &errors)
ConnectionMode
Definition qhttp.h:172
@ ConnectionModeHttp
Definition qhttp.h:173
void readyRead(const QHttpResponseHeader &resp)
void requestStarted(int)
void authenticationRequired(const QString &hostname, quint16 port, QAuthenticator *)
#define d
Definition detector.cpp:69
#define e
Definition detector.cpp:70
#define h
Definition detector.cpp:73