20 #include <tdeio/global.h>
22 #include <tdelocale.h>
24 #include "httpfilter.h"
26 HTTPFilterBase::HTTPFilterBase()
31 HTTPFilterBase::~HTTPFilterBase()
37 HTTPFilterBase::chain(HTTPFilterBase *previous)
40 connect(last, TQ_SIGNAL(output(
const TQByteArray &)),
41 this, TQ_SLOT(slotInput(
const TQByteArray &)));
44 HTTPFilterChain::HTTPFilterChain()
50 HTTPFilterChain::addFilter(HTTPFilterBase *filter)
58 disconnect(last, TQ_SIGNAL(output(
const TQByteArray &)), 0, 0);
62 connect(filter, TQ_SIGNAL(output(
const TQByteArray &)),
63 this, TQ_SIGNAL(output(
const TQByteArray &)));
64 connect(filter, TQ_SIGNAL(error(
int,
const TQString &)),
65 this, TQ_SIGNAL(error(
int,
const TQString &)));
69 HTTPFilterChain::slotInput(
const TQByteArray &d)
77 HTTPFilterMD5::HTTPFilterMD5()
84 return TQString::fromLatin1(context.base64Digest());
88 HTTPFilterMD5::slotInput(
const TQByteArray &d)
95 HTTPFilterGZip::HTTPFilterGZip()
103 zstr.next_in = (Bytef *) Z_NULL;
105 zstr.zalloc = Z_NULL;
107 zstr.opaque = Z_NULL;
109 inflateInit2(&zstr, -MAX_WBITS);
115 HTTPFilterGZip::~HTTPFilterGZip()
158 HTTPFilterGZip::get_byte()
161 if (bEof)
return EOF;
162 if (zstr.avail_in == 0)
169 return *(zstr.next_in)++;
177 static int gz_magic[2] = {0x1f, 0x8b};
180 #define ASCII_FLAG 0x01
181 #define HEAD_CRC 0x02
182 #define EXTRA_FIELD 0x04
183 #define ORIG_NAME 0x08
185 #define RESERVED 0xE0
192 HTTPFilterGZip::checkHeader()
199 for (len = 0; len < 2; len++) {
201 if (c != gz_magic[len]) {
216 int method = get_byte();
217 int flags = get_byte();
219 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
224 for (len = 0; len < 6; len++) (
void)get_byte();
226 if ((flags & EXTRA_FIELD) != 0) {
227 len = (uInt)get_byte();
228 len += ((uInt)get_byte())<<8;
230 while (len-- != 0 && get_byte() != EOF) ;
232 if ((flags & ORIG_NAME) != 0) {
233 while ((c = get_byte()) != 0 && c != EOF) ;
235 if ((flags & COMMENT) != 0) {
236 while ((c = get_byte()) != 0 && c != EOF) ;
238 if ((flags & HEAD_CRC) != 0) {
239 for (len = 0; len < 2; len++) (
void)get_byte();
249 HTTPFilterGZip::slotInput(
const TQByteArray &d)
265 TQByteArray flush(4);
268 if (!bHasFinished && !bHasHeader)
271 emit output(headerData);
274 emit output(TQByteArray());
278 emit error( TDEIO::ERR_SLAVE_DEFINED, i18n(
"Unexpected end of data, some information may be lost."));
286 iTrailer -= d.size();
291 emit output(TQByteArray());
301 int orig_size = headerData.size();
302 headerData.resize(orig_size+d.size());
303 memcpy(headerData.data()+orig_size, d.data(), d.size());
305 zstr.avail_in = headerData.size();
306 zstr.next_in = (Bytef *) headerData.data();
308 int result = checkHeader();
323 zstr.avail_in = d.size();
324 zstr.next_in = (Bytef *) d.data();
327 while( zstr.avail_in )
330 zstr.next_out = (Bytef *) buf;
331 zstr.avail_out = 8192;
332 int result = inflate( &zstr, Z_NO_FLUSH );
333 if ((result != Z_OK) && (result != Z_STREAM_END))
335 emit error( TDEIO::ERR_SLAVE_DEFINED, i18n(
"Receiving corrupt data."));
338 int bytesOut = 8192 - zstr.avail_out;
342 d.setRawData( buf, bytesOut );
344 d.resetRawData( buf, bytesOut );
346 if (result == Z_STREAM_END)
356 emit output(TQByteArray());
364 HTTPFilterDeflate::HTTPFilterDeflate()
372 #include "httpfilter.moc"