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 | };
|
---|
|
---|