1 | /****************************************************************************
|
---|
2 | ** $Id: qhttp.h 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Definition of QHttp and related classes.
|
---|
5 | **
|
---|
6 | ** Created : 970521
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1997-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the network module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition licenses may use this
|
---|
22 | ** file in accordance with the Qt Commercial License Agreement provided
|
---|
23 | ** with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #ifndef QHTTP_H
|
---|
39 | #define QHTTP_H
|
---|
40 |
|
---|
41 | #ifndef QT_H
|
---|
42 | #include "qobject.h"
|
---|
43 | #include "qnetworkprotocol.h"
|
---|
44 | #include "qstringlist.h"
|
---|
45 | #endif // QT_H
|
---|
46 |
|
---|
47 | #if !defined( QT_MODULE_NETWORK ) || defined( QT_LICENSE_PROFESSIONAL ) || defined( QT_INTERNAL_NETWORK )
|
---|
48 | #define QM_EXPORT_HTTP
|
---|
49 | #define QM_TEMPLATE_EXTERN_HTTP
|
---|
50 | #else
|
---|
51 | #define QM_EXPORT_HTTP Q_EXPORT
|
---|
52 | #define QM_TEMPLATE_EXTERN_HTTP Q_TEMPLATE_EXTERN
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #ifndef QT_NO_NETWORKPROTOCOL_HTTP
|
---|
56 |
|
---|
57 | class QSocket;
|
---|
58 | class QTimerEvent;
|
---|
59 | class QTextStream;
|
---|
60 | class QIODevice;
|
---|
61 |
|
---|
62 | class QHttpPrivate;
|
---|
63 | class QHttpRequest;
|
---|
64 |
|
---|
65 | class QM_EXPORT_HTTP QHttpHeader
|
---|
66 | {
|
---|
67 | public:
|
---|
68 | QHttpHeader();
|
---|
69 | QHttpHeader( const QHttpHeader& header );
|
---|
70 | QHttpHeader( const QString& str );
|
---|
71 | virtual ~QHttpHeader();
|
---|
72 |
|
---|
73 | QHttpHeader& operator=( const QHttpHeader& 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 QM_EXPORT_HTTP QHttpResponseHeader : public QHttpHeader
|
---|
107 | {
|
---|
108 | private:
|
---|
109 | QHttpResponseHeader( int code, const QString& text = QString::null, int majorVer = 1, int minorVer = 1 );
|
---|
110 | QHttpResponseHeader( const QString& str );
|
---|
111 |
|
---|
112 | void setStatusLine( int code, const QString& text = QString::null, int majorVer = 1, int minorVer = 1 );
|
---|
113 |
|
---|
114 | public:
|
---|
115 | QHttpResponseHeader();
|
---|
116 | QHttpResponseHeader( const QHttpResponseHeader& 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 QHttp;
|
---|
136 | };
|
---|
137 |
|
---|
138 | class QM_EXPORT_HTTP QHttpRequestHeader : public QHttpHeader
|
---|
139 | {
|
---|
140 | public:
|
---|
141 | QHttpRequestHeader();
|
---|
142 | QHttpRequestHeader( const QString& method, const QString& path, int majorVer = 1, int minorVer = 1 );
|
---|
143 | QHttpRequestHeader( const QHttpRequestHeader& header );
|
---|
144 | QHttpRequestHeader( 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 QM_EXPORT_HTTP QHttp : public QNetworkProtocol
|
---|
167 | {
|
---|
168 | Q_OBJECT
|
---|
169 |
|
---|
170 | public:
|
---|
171 | QHttp();
|
---|
172 | QHttp( QObject* parent, const char* name = 0 ); // ### Qt 4.0: make parent=0 and get rid of the QHttp() constructor
|
---|
173 | QHttp( const QString &hostname, Q_UINT16 port=80, QObject* parent=0, const char* name = 0 );
|
---|
174 | virtual ~QHttp();
|
---|
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 QHttpRequestHeader &header, QIODevice *device=0, QIODevice *to=0 );
|
---|
197 | int request( const QHttpRequestHeader &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 | QHttpRequestHeader 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 slots:
|
---|
218 | void abort();
|
---|
219 |
|
---|
220 | signals:
|
---|
221 | void stateChanged( int );
|
---|
222 | void responseHeaderReceived( const QHttpResponseHeader& resp );
|
---|
223 | void readyRead( const QHttpResponseHeader& 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( QNetworkOperation *op );
|
---|
233 | void operationPut( QNetworkOperation *op );
|
---|
234 |
|
---|
235 | void timerEvent( QTimerEvent * );
|
---|
236 |
|
---|
237 | private slots:
|
---|
238 | void clientReply( const QHttpResponseHeader &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 | QHttpPrivate *d;
|
---|
251 | void *unused; // ### Qt 4.0: remove this (in for binary compatibility)
|
---|
252 | int bytesRead;
|
---|
253 |
|
---|
254 | int addRequest( QHttpRequest * );
|
---|
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 QHttpNormalRequest;
|
---|
266 | friend class QHttpSetHostRequest;
|
---|
267 | friend class QHttpCloseRequest;
|
---|
268 | friend class QHttpPGHRequest;
|
---|
269 | };
|
---|
270 |
|
---|
271 | #define Q_DEFINED_QHTTP
|
---|
272 | #include "qwinexport.h"
|
---|
273 | #endif
|
---|
274 | #endif
|
---|