1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#ifndef _KVI_HTTP_H_
#define _KVI_HTTP_H_
//=============================================================================
//
// File : kvi_http.h
// Creation date : Sat Aug 17 13:43:31 2002 GMT by Szymon Stefanek
//
// This file is part of the KVirc irc client distribution
// Copyright (C) 2002-2007 Szymon Stefanek (pragma at kvirc dot net)
//
// This program is FREE software. You can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your opinion) any later version.
//
// This program is distributed in the HOPE that it will be USEFUL,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, write to the Free Software Foundation,
// Inc. ,59 Temple Place - Suite 33, Boston, MA 02110-1301, USA.
//
//=============================================================================
#include "kvi_settings.h"
#include "kvi_heapobject.h"
#include "kvi_string.h"
#include "kvi_thread.h"
#include "kvi_sockettype.h"
#include "kvi_databuffer.h"
#include "kvi_inttypes.h"
#include "kvi_url.h"
#include <tqobject.h>
#include "kvi_pointerhashtable.h"
#include "kvi_file.h"
#include <tqstringlist.h>
class KviDns;
class KviSSL;
class KviHttpRequestThread;
//
// This class implements a HTTP protocol client.
// It's able to send GET, POST and HEAD requests,
// download stuff to a file or to a qt TQT_SLOT().
//
class KVILIB_API KviHttpRequest : public TQObject, public KviHeapObject
{
Q_OBJECT
public:
enum ProcessingType
{
HeadersOnly, // Download headers only (HEAD request)
WholeFile, // Emit the data as whole file (binaryData() is emitted)
Blocks, // Emit the data as blocks (binaryData() is emitted)
Lines, // Emit the data as ASCII text lines (the client must take care of decoding the data)
StoreToFile // Store the data to a file
};
enum ExistingFileAction
{
Overwrite, // Overwrite existing file
RenameIncoming, // Automatically rename the incoming file
RenameExisting, // Automatically rename the existing file
Resume // Attempt to resume the file (get partial content)
};
public:
KviHttpRequest();
~KviHttpRequest();
protected:
// data
KviUrl m_url;
TQString m_szFileName;
ProcessingType m_eProcessingType;
ExistingFileAction m_eExistingFileAction;
void * m_pPrivateData;
unsigned int m_uMaxContentLength;
unsigned int m_uContentOffset;
TQString m_szPostData;
// status
TQString m_szLastError;
unsigned int m_uTotalSize;
unsigned int m_uReceivedSize;
// internal status
TQString m_szIp;
KviDns * m_pDns;
KviHttpRequestThread * m_pThread;
KviDataBuffer * m_pBuffer;
bool m_bHeaderProcessed;
bool m_bChunkedTransferEncoding;
bool m_bGzip;
unsigned int m_uRemainingChunkSize;
bool m_bIgnoreRemainingData; // used in chunked transfer after the last chunk has been seen
KviFile * m_pFile;
protected:
bool startDnsLookup();
virtual bool event(TQEvent *e);
void processData(KviDataBuffer * data);
bool processHeader(KviStr &szHeader);
bool openFile();
void emitLines(KviDataBuffer * pDataBuffer);
void resetStatus();
void resetData();
void resetInternalStatus();
protected slots:
void dnsLookupDone(KviDns *d);
void haveServerIp();
public:
const KviUrl & url(){ return m_url; };
ProcessingType processingType(){ return m_eProcessingType; };
ExistingFileAction existingFileAction(){ return m_eExistingFileAction; };
const TQString &fileName(){ return m_szFileName; };
void * privateData(){ return m_pPrivateData; };
unsigned int maxContentLength(){ return m_uMaxContentLength; };
unsigned int contentOffset(){ return m_uContentOffset; };
unsigned int totalSize(){ return m_uTotalSize; };
unsigned int receivedSize(){ return m_uReceivedSize; };
void reset();
void setPostData(const TQString &szPostData){ m_szPostData = szPostData; };
void setUrl(const KviUrl &u){ m_url = u; };
void setProcessingType(ProcessingType t){ m_eProcessingType = t; };
void setExistingFileAction(ExistingFileAction a){ m_eExistingFileAction = a; };
void setFileName(const TQString &szFileName){ m_szFileName = szFileName; };
void setPrivateData(void * ptr){ m_pPrivateData = ptr; };
void setMaxContentLength(int uMaxContentLength){ m_uMaxContentLength = uMaxContentLength; }; //0 means unlimited
// this will work regardless of ExistingFileAction : even if the file doesn't exist
void setContentOffset(int uContentOffset){ m_uContentOffset = uContentOffset; };
bool start();
// this is a shortcut for reset()+setUrl()+setProcessingType()+setFileName()+start()
bool get(const KviUrl &u,ProcessingType p = WholeFile,const TQString &szFileName = TQString());
const TQString & lastError(){ return m_szLastError; };
void abort();
signals:
void resolvingHost(const TQString &hostname);
void contactingHost(const TQString &ipandport);
void connectionEstabilished();
void receivedResponse(const TQString &response);
void terminated(bool bSuccess);
void status(const TQString &message);
void data(const KviStr &data);
void binaryData(const KviDataBuffer &data);
void header(KviPointerHashTable<const char *,KviStr> * hdr);
void requestSent(const TQStringList &request);
};
class KviHttpRequestThread : public KviSensitiveThread
{
friend class KviHttpRequest;
public:
enum RequestMethod { Post, Get , Head };
protected:
KviHttpRequestThread(KviHttpRequest * r,
const TQString &szHost,
const TQString &szIp,
unsigned short uPort,
const TQString &szPath,
unsigned int uContentOffset,
RequestMethod m,
const TQString &szPostData = TQString(),
bool bUseSSL = false);
public:
~KviHttpRequestThread();
protected:
KviHttpRequest * m_pRequest;
TQString m_szHost;
TQString m_szIp;
TQString m_szPath;
unsigned int m_uContentOffset;
RequestMethod m_eRequestMethod;
TQString m_szPostData;
unsigned short m_uPort;
kvi_socket_t m_sock;
bool m_bUseSSL;
#ifdef COMPILE_SSL_SUPPORT
KviSSL * m_pSSL;
#endif
protected:
int selectForReadStep();
bool selectForRead(int iTimeoutInSecs);
bool readDataStep();
bool sendBuffer(const char *buffer,int bufLen,int iTimeoutInSecs);
bool failure(const char *error=0);
bool sslFailure();
bool selectForWrite(int iTimeoutInSecs);
bool connectToRemoteHost();
bool processInternalEvents();
void runInternal();
virtual void run();
};
#endif //_KVI_HTTP_H_
|