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 Qt3Support 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 Q3HTTP_H
|
---|
43 | #define Q3HTTP_H
|
---|
44 |
|
---|
45 | #include <QtCore/qobject.h>
|
---|
46 | #include <Qt3Support/q3networkprotocol.h>
|
---|
47 | #include <QtCore/qmap.h>
|
---|
48 | #include <QtCore/qstringlist.h>
|
---|
49 |
|
---|
50 | QT_BEGIN_HEADER
|
---|
51 |
|
---|
52 | QT_BEGIN_NAMESPACE
|
---|
53 |
|
---|
54 | QT_MODULE(Qt3Support)
|
---|
55 | #ifndef QT_NO_HTTP
|
---|
56 |
|
---|
57 | class Q3Socket;
|
---|
58 | class QTimerEvent;
|
---|
59 | class QTextStream;
|
---|
60 | class QIODevice;
|
---|
61 |
|
---|
62 | class Q3HttpPrivate;
|
---|
63 | class Q3HttpRequest;
|
---|
64 |
|
---|
65 | class Q_COMPAT_EXPORT Q3HttpHeader
|
---|
66 | {
|
---|
67 | public:
|
---|
68 | Q3HttpHeader();
|
---|
69 | Q3HttpHeader( const Q3HttpHeader& header );
|
---|
70 | Q3HttpHeader( const QString& str );
|
---|
71 | virtual ~Q3HttpHeader();
|
---|
72 |
|
---|
73 | Q3HttpHeader& operator=( const Q3HttpHeader& h );
|
---|
74 |
|
---|
75 | QString value( const QString& key ) const;
|
---|
76 | void setValue( const QString& key, const QString& value );
|
---|
77 | void removeValue( const QString& key );
|
---|
78 |
|
---|
79 | QStringList keys() const;
|
---|
80 | bool hasKey( const QString& key ) const;
|
---|
81 |
|
---|
82 | bool hasContentLength() const;
|
---|
83 | uint contentLength() const;
|
---|
84 | void setContentLength( int len );
|
---|
85 |
|
---|
86 | bool hasContentType() const;
|
---|
87 | QString contentType() const;
|
---|
88 | void setContentType( const QString& type );
|
---|
89 |
|
---|
90 | virtual QString toString() const;
|
---|
91 | bool isValid() const;
|
---|
92 |
|
---|
93 | virtual int majorVersion() const = 0;
|
---|
94 | virtual int minorVersion() const = 0;
|
---|
95 |
|
---|
96 | protected:
|
---|
97 | virtual bool parseLine( const QString& line, int number );
|
---|
98 | bool parse( const QString& str );
|
---|
99 | void setValid( bool );
|
---|
100 |
|
---|
101 | private:
|
---|
102 | QMap<QString,QString> values;
|
---|
103 | bool valid;
|
---|
104 | };
|
---|
105 |
|
---|
106 | class Q_COMPAT_EXPORT Q3HttpResponseHeader : public Q3HttpHeader
|
---|
107 | {
|
---|
108 | private:
|
---|
109 | Q3HttpResponseHeader( int code, const QString& text = QString(), int majorVer = 1, int minorVer = 1 );
|
---|
110 | Q3HttpResponseHeader( const QString& str );
|
---|
111 |
|
---|
112 | void setStatusLine( int code, const QString& text = QString(), int majorVer = 1, int minorVer = 1 );
|
---|
113 |
|
---|
114 | public:
|
---|
115 | Q3HttpResponseHeader();
|
---|
116 | Q3HttpResponseHeader( const Q3HttpResponseHeader& header );
|
---|
117 |
|
---|
118 | int statusCode() const;
|
---|
119 | QString reasonPhrase() const;
|
---|
120 |
|
---|
121 | int majorVersion() const;
|
---|
122 | int minorVersion() const;
|
---|
123 |
|
---|
124 | QString toString() const;
|
---|
125 |
|
---|
126 | protected:
|
---|
127 | bool parseLine( const QString& line, int number );
|
---|
128 |
|
---|
129 | private:
|
---|
130 | int statCode;
|
---|
131 | QString reasonPhr;
|
---|
132 | int majVer;
|
---|
133 | int minVer;
|
---|
134 |
|
---|
135 | friend class Q3Http;
|
---|
136 | };
|
---|
137 |
|
---|
138 | class Q_COMPAT_EXPORT Q3HttpRequestHeader : public Q3HttpHeader
|
---|
139 | {
|
---|
140 | public:
|
---|
141 | Q3HttpRequestHeader();
|
---|
142 | Q3HttpRequestHeader( const QString& method, const QString& path, int majorVer = 1, int minorVer = 1 );
|
---|
143 | Q3HttpRequestHeader( const Q3HttpRequestHeader& header );
|
---|
144 | Q3HttpRequestHeader( const QString& str );
|
---|
145 |
|
---|
146 | void setRequest( const QString& method, const QString& path, int majorVer = 1, int minorVer = 1 );
|
---|
147 |
|
---|
148 | QString method() const;
|
---|
149 | QString path() const;
|
---|
150 |
|
---|
151 | int majorVersion() const;
|
---|
152 | int minorVersion() const;
|
---|
153 |
|
---|
154 | QString toString() const;
|
---|
155 |
|
---|
156 | protected:
|
---|
157 | bool parseLine( const QString& line, int number );
|
---|
158 |
|
---|
159 | private:
|
---|
160 | QString m;
|
---|
161 | QString p;
|
---|
162 | int majVer;
|
---|
163 | int minVer;
|
---|
164 | };
|
---|
165 |
|
---|
166 | class Q_COMPAT_EXPORT Q3Http : public Q3NetworkProtocol
|
---|
167 | {
|
---|
168 | Q_OBJECT
|
---|
169 |
|
---|
170 | public:
|
---|
171 | Q3Http();
|
---|
172 | Q3Http( QObject* parent, const char* name = 0 ); // ### Qt 4.0: make parent=0 and get rid of the Q3Http() constructor
|
---|
173 | Q3Http( const QString &hostname, Q_UINT16 port=80, QObject* parent=0, const char* name = 0 );
|
---|
174 | virtual ~Q3Http();
|
---|
175 |
|
---|
176 | int supportedOperations() const;
|
---|
177 |
|
---|
178 | enum State { Unconnected, HostLookup, Connecting, Sending, Reading, Connected, Closing };
|
---|
179 | enum Error {
|
---|
180 | NoError,
|
---|
181 | UnknownError,
|
---|
182 | HostNotFound,
|
---|
183 | ConnectionRefused,
|
---|
184 | UnexpectedClose,
|
---|
185 | InvalidResponseHeader,
|
---|
186 | WrongContentLength,
|
---|
187 | Aborted
|
---|
188 | };
|
---|
189 |
|
---|
190 | int setHost(const QString &hostname, Q_UINT16 port=80 );
|
---|
191 |
|
---|
192 | int get( const QString& path, QIODevice* to=0 );
|
---|
193 | int post( const QString& path, QIODevice* data, QIODevice* to=0 );
|
---|
194 | int post( const QString& path, const QByteArray& data, QIODevice* to=0 );
|
---|
195 | int head( const QString& path );
|
---|
196 | int request( const Q3HttpRequestHeader &header, QIODevice *device=0, QIODevice *to=0 );
|
---|
197 | int request( const Q3HttpRequestHeader &header, const QByteArray &data, QIODevice *to=0 );
|
---|
198 |
|
---|
199 | int closeConnection();
|
---|
200 |
|
---|
201 | Q_ULONG bytesAvailable() const;
|
---|
202 | Q_LONG readBlock( char *data, Q_ULONG maxlen );
|
---|
203 | QByteArray readAll();
|
---|
204 |
|
---|
205 | int currentId() const;
|
---|
206 | QIODevice* currentSourceDevice() const;
|
---|
207 | QIODevice* currentDestinationDevice() const;
|
---|
208 | Q3HttpRequestHeader currentRequest() const;
|
---|
209 | bool hasPendingRequests() const;
|
---|
210 | void clearPendingRequests();
|
---|
211 |
|
---|
212 | State state() const;
|
---|
213 |
|
---|
214 | Error error() const;
|
---|
215 | QString errorString() const;
|
---|
216 |
|
---|
217 | public Q_SLOTS:
|
---|
218 | void abort();
|
---|
219 |
|
---|
220 | Q_SIGNALS:
|
---|
221 | void stateChanged( int );
|
---|
222 | void responseHeaderReceived( const Q3HttpResponseHeader& resp );
|
---|
223 | void readyRead( const Q3HttpResponseHeader& resp );
|
---|
224 | void dataSendProgress( int, int );
|
---|
225 | void dataReadProgress( int, int );
|
---|
226 |
|
---|
227 | void requestStarted( int );
|
---|
228 | void requestFinished( int, bool );
|
---|
229 | void done( bool );
|
---|
230 |
|
---|
231 | protected:
|
---|
232 | void operationGet( Q3NetworkOperation *op );
|
---|
233 | void operationPut( Q3NetworkOperation *op );
|
---|
234 |
|
---|
235 | void timerEvent( QTimerEvent * );
|
---|
236 |
|
---|
237 | private Q_SLOTS:
|
---|
238 | void clientReply( const Q3HttpResponseHeader &rep );
|
---|
239 | void clientDone( bool );
|
---|
240 | void clientStateChanged( int );
|
---|
241 |
|
---|
242 | void startNextRequest();
|
---|
243 | void slotReadyRead();
|
---|
244 | void slotConnected();
|
---|
245 | void slotError( int );
|
---|
246 | void slotClosed();
|
---|
247 | void slotBytesWritten( int );
|
---|
248 |
|
---|
249 | private:
|
---|
250 | Q3HttpPrivate *d;
|
---|
251 | void *unused; // ### Qt 4.0: remove this (in for binary compatibility)
|
---|
252 | int bytesRead;
|
---|
253 |
|
---|
254 | int addRequest( Q3HttpRequest * );
|
---|
255 | void sendRequest();
|
---|
256 | void finishedWithSuccess();
|
---|
257 | void finishedWithError( const QString& detail, int errorCode );
|
---|
258 |
|
---|
259 | void killIdleTimer();
|
---|
260 |
|
---|
261 | void init();
|
---|
262 | void setState( int );
|
---|
263 | void close();
|
---|
264 |
|
---|
265 | friend class Q3HttpNormalRequest;
|
---|
266 | friend class Q3HttpSetHostRequest;
|
---|
267 | friend class Q3HttpCloseRequest;
|
---|
268 | friend class Q3HttpPGHRequest;
|
---|
269 | };
|
---|
270 |
|
---|
271 | #endif // QT_NO_HTTP
|
---|
272 |
|
---|
273 | QT_END_NAMESPACE
|
---|
274 |
|
---|
275 | QT_END_HEADER
|
---|
276 |
|
---|
277 | #endif // Q3HTTP_H
|
---|