source: trunk/src/qt3support/network/q3http.h@ 367

Last change on this file since 367 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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