1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia 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 | #include <QtCore/qscopedpointer.h>
|
---|
50 |
|
---|
51 | QT_BEGIN_HEADER
|
---|
52 |
|
---|
53 | QT_BEGIN_NAMESPACE
|
---|
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;
|
---|
69 | class Q_NETWORK_EXPORT QHttpHeader
|
---|
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);
|
---|
82 | QList<QPair<QString, QString> > values() const;
|
---|
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);
|
---|
112 | QScopedPointer<QHttpHeaderPrivate> d_ptr;
|
---|
113 |
|
---|
114 | private:
|
---|
115 | Q_DECLARE_PRIVATE(QHttpHeader)
|
---|
116 | };
|
---|
117 |
|
---|
118 | class QHttpResponseHeaderPrivate;
|
---|
119 | class Q_NETWORK_EXPORT QHttpResponseHeader : public QHttpHeader
|
---|
120 | {
|
---|
121 | public:
|
---|
122 | QHttpResponseHeader();
|
---|
123 | QHttpResponseHeader(const QHttpResponseHeader &header);
|
---|
124 | QHttpResponseHeader(const QString &str);
|
---|
125 | QHttpResponseHeader(int code, const QString &text = QString(), int majorVer = 1, int minorVer = 1);
|
---|
126 | QHttpResponseHeader &operator=(const QHttpResponseHeader &header);
|
---|
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:
|
---|
142 | Q_DECLARE_PRIVATE(QHttpResponseHeader)
|
---|
143 | friend class QHttpPrivate;
|
---|
144 | };
|
---|
145 |
|
---|
146 | class QHttpRequestHeaderPrivate;
|
---|
147 | class Q_NETWORK_EXPORT QHttpRequestHeader : public QHttpHeader
|
---|
148 | {
|
---|
149 | public:
|
---|
150 | QHttpRequestHeader();
|
---|
151 | QHttpRequestHeader(const QString &method, const QString &path, int majorVer = 1, int minorVer = 1);
|
---|
152 | QHttpRequestHeader(const QHttpRequestHeader &header);
|
---|
153 | QHttpRequestHeader(const QString &str);
|
---|
154 | QHttpRequestHeader &operator=(const QHttpRequestHeader &header);
|
---|
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:
|
---|
170 | Q_DECLARE_PRIVATE(QHttpRequestHeader)
|
---|
171 | };
|
---|
172 |
|
---|
173 | class Q_NETWORK_EXPORT QHttp : public QObject
|
---|
174 | {
|
---|
175 | Q_OBJECT
|
---|
176 |
|
---|
177 | public:
|
---|
178 | enum ConnectionMode {
|
---|
179 | ConnectionModeHttp,
|
---|
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 {
|
---|
189 | Unconnected,
|
---|
190 | HostLookup,
|
---|
191 | Connecting,
|
---|
192 | Sending,
|
---|
193 | Reading,
|
---|
194 | Connected,
|
---|
195 | Closing
|
---|
196 | };
|
---|
197 | enum Error {
|
---|
198 | NoError,
|
---|
199 | UnknownError,
|
---|
|
---|