summaryrefslogtreecommitdiffstats
path: root/tdeio/httpfilter/httpfilter.cpp
blob: 0f6a457905e08ca88b166a294c8f365a9193d1e2 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
   This file is part of the KDE libraries
   Copyright (c) 2002 Waldo Bastian <bastian@kde.org>
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.
   
   This library 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
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <tdeio/global.h>

#include <tdelocale.h>

#include "httpfilter.h"

HTTPFilterBase::HTTPFilterBase()
 : last(0)
{
}

HTTPFilterBase::~HTTPFilterBase()
{
   delete last;
}

void
HTTPFilterBase::chain(HTTPFilterBase *previous)
{
   last = previous;
   connect(last, TQT_SIGNAL(output(const TQByteArray &)),
           this, TQT_SLOT(slotInput(const TQByteArray &)));
}

HTTPFilterChain::HTTPFilterChain()
 : first(0)
{
}

void
HTTPFilterChain::addFilter(HTTPFilterBase *filter)
{
   if (!last)
   {
      first = filter;
   }
   else
   {
      disconnect(last, TQT_SIGNAL(output(const TQByteArray &)), 0, 0);
      filter->chain(last);
   }
   last = filter;
   connect(filter, TQT_SIGNAL(output(const TQByteArray &)),
           this, TQT_SIGNAL(output(const TQByteArray &)));
   connect(filter, TQT_SIGNAL(error(int, const TQString &)),
           this, TQT_SIGNAL(error(int, const TQString &)));
}

void
HTTPFilterChain::slotInput(const TQByteArray &d)
{
   if (first)
      first->slotInput(d);
   else
      emit output(d);      
}

HTTPFilterMD5::HTTPFilterMD5()
{
}

TQString 
HTTPFilterMD5::md5()
{
   return TQString::fromLatin1(context.base64Digest());
}

void 
HTTPFilterMD5::slotInput(const TQByteArray &d)
{
   context.update(d);
   emit output(d);
}


HTTPFilterGZip::HTTPFilterGZip()
{
#ifdef DO_GZIP
  bHasHeader = false;
  bHasFinished = false;
  bPlainText = false;
  bEatTrailer = false;
  bEof = false;
  zstr.next_in = (Bytef *) Z_NULL;
  zstr.avail_in = 0;
  zstr.zalloc = Z_NULL;
  zstr.zfree = Z_NULL;
  zstr.opaque = Z_NULL;

  inflateInit2(&zstr, -MAX_WBITS);

  iTrailer = 8;
#endif
}

HTTPFilterGZip::~HTTPFilterGZip()
{
#ifdef DO_GZIP
  inflateEnd(&zstr);
#endif
  
}

/* The get_byte() and checkHeader() functions are modified version from */
/* the correpsonding functions that can be found in zlib, the following */
/* copyright notice applies to these functions:                         */

/* zlib.h -- interface of the 'zlib' general purpose compression library
  version 1.1.3, July 9th, 1998

  Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jean-loup Gailly        Mark Adler
  jloup@gzip.org          madler@alumni.caltech.edu


  The data format used by the zlib library is described by RFCs (Request for
  Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
*/

int
HTTPFilterGZip::get_byte()
{
#ifdef DO_GZIP
    if (bEof) return EOF;
    if (zstr.avail_in == 0)
    {
        bEof = true;
        return EOF;
    }
    zstr.avail_in--;
    zstr.total_in++;
    return *(zstr.next_in)++;
#else 
    return 0;
#endif
}

#ifdef DO_GZIP

static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */

/* gzip flag byte */
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define COMMENT      0x10 /* bit 4 set: file comment present */
#define RESERVED     0xE0 /* bits 5..7: reserved */
#endif

// 0 : ok
// 1 : not gzip
// 2 : no header
int
HTTPFilterGZip::checkHeader()
{
#ifdef DO_GZIP
    uInt len;
    int c;

    /* Check the gzip magic header */
    for (len = 0; len < 2; len++) {
	c = get_byte();
	if (c != gz_magic[len]) {
	    if (len != 0) 
	    {
	       zstr.avail_in++;
	       zstr.next_in--;
	    }
	    if (c != EOF) {
		zstr.avail_in++;
		zstr.next_in--;
		return 1;
	    }
	    return 2;
	}
    }

    int method = get_byte(); /* method byte */
    int flags = get_byte(); /* flags byte */

    if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
	return bEof ? 2 : 1;
    }

    /* Discard time, xflags and OS code: */
    for (len = 0; len < 6; len++) (void)get_byte();

    if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
	len  =  (uInt)get_byte();
	len += ((uInt)get_byte())<<8;
	/* len is garbage if EOF but the loop below will quit anyway */
	while (len-- != 0 && get_byte() != EOF) ;
    }
    if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
	while ((c = get_byte()) != 0 && c != EOF) ;
    }
    if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
	while ((c = get_byte()) != 0 && c != EOF) ;
    }
    if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
	for (len = 0; len < 2; len++) (void)get_byte();
    }
    
    return bEof ? 2 : 0;
#else
    return 0;
#endif
} 

void 
HTTPFilterGZip::slotInput(const TQByteArray &d)
{
#ifdef DO_GZIP
  if (bPlainText)
  {
     emit output(d);
     return;
  }
  if (d.size() == 0)
  {
     if (bEatTrailer)
        bHasFinished = true;
     if (!bHasFinished)
     {
        // Make sure we get the last bytes still in the pipe.
        // Needed with "deflate".
        TQByteArray flush(4);
        flush.fill(0);
        slotInput(flush);
        if (!bHasFinished && !bHasHeader)
        {
           // Send as-is
           emit output(headerData);
           bHasFinished = true;
           // End of data
           emit output(TQByteArray());
        }
     }
     if (!bHasFinished)
        emit error( TDEIO::ERR_SLAVE_DEFINED, i18n("Unexpected end of data, some information may be lost."));
     return;
  }
  if (bHasFinished)
     return;

  if (bEatTrailer)
  {
     iTrailer -= d.size();
     if (iTrailer <= 0)
     {
        bHasFinished = true;
        // End of data
        emit output(TQByteArray());
     }
     return;
  }

  if (!bHasHeader)
  {
     bEof = false;

     // Add data to header.
     int orig_size = headerData.size();
     headerData.resize(orig_size+d.size());
     memcpy(headerData.data()+orig_size, d.data(), d.size());

     zstr.avail_in = headerData.size();
     zstr.next_in = (Bytef *) headerData.data();     

     int result = checkHeader();
     if (result == 1)
     {
        bPlainText = true;
        output(headerData);
        return;
     }

     if (result != 0)
        return; // next time better

     bHasHeader = true;
  }
  else
  {
     zstr.avail_in = d.size();
     zstr.next_in = (Bytef *) d.data();
  }

  while( zstr.avail_in )
  {
     char buf[8192];
     zstr.next_out = (Bytef *) buf;
     zstr.avail_out = 8192;
     int result = inflate( &zstr, Z_NO_FLUSH );
     if ((result != Z_OK) && (result != Z_STREAM_END))
     {
        emit error( TDEIO::ERR_SLAVE_DEFINED, i18n("Receiving corrupt data."));
        break;
     }
     int bytesOut = 8192 - zstr.avail_out;
     if (bytesOut)
     {
        TQByteArray d;
        d.setRawData( buf, bytesOut );
        emit output(d);
        d.resetRawData( buf, bytesOut );
     }
     if (result == Z_STREAM_END)
     {
        if (iTrailer)
        {
           bEatTrailer = true;
        }
        else
        {
           bHasFinished = true;
           // End of data
           emit output(TQByteArray());
        }
        return;
     }
  }  
#endif
}

HTTPFilterDeflate::HTTPFilterDeflate()
{
#ifdef DO_GZIP
  bHasHeader = true;
  iTrailer = 0;
#endif
}

#include "httpfilter.moc"