• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/httpfilter
 

tdeio/httpfilter

  • tdeio
  • httpfilter
httpfilter.cpp
1 /*
2  This file is part of the KDE libraries
3  Copyright (c) 2002 Waldo Bastian <bastian@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include <tdeio/global.h>
21 
22 #include <tdelocale.h>
23 
24 #include "httpfilter.h"
25 
26 HTTPFilterBase::HTTPFilterBase()
27  : last(0)
28 {
29 }
30 
31 HTTPFilterBase::~HTTPFilterBase()
32 {
33  delete last;
34 }
35 
36 void
37 HTTPFilterBase::chain(HTTPFilterBase *previous)
38 {
39  last = previous;
40  connect(last, TQT_SIGNAL(output(const TQByteArray &)),
41  this, TQT_SLOT(slotInput(const TQByteArray &)));
42 }
43 
44 HTTPFilterChain::HTTPFilterChain()
45  : first(0)
46 {
47 }
48 
49 void
50 HTTPFilterChain::addFilter(HTTPFilterBase *filter)
51 {
52  if (!last)
53  {
54  first = filter;
55  }
56  else
57  {
58  disconnect(last, TQT_SIGNAL(output(const TQByteArray &)), 0, 0);
59  filter->chain(last);
60  }
61  last = filter;
62  connect(filter, TQT_SIGNAL(output(const TQByteArray &)),
63  this, TQT_SIGNAL(output(const TQByteArray &)));
64  connect(filter, TQT_SIGNAL(error(int, const TQString &)),
65  this, TQT_SIGNAL(error(int, const TQString &)));
66 }
67 
68 void
69 HTTPFilterChain::slotInput(const TQByteArray &d)
70 {
71  if (first)
72  first->slotInput(d);
73  else
74  emit output(d);
75 }
76 
77 HTTPFilterMD5::HTTPFilterMD5()
78 {
79 }
80 
81 TQString
82 HTTPFilterMD5::md5()
83 {
84  return TQString::fromLatin1(context.base64Digest());
85 }
86 
87 void
88 HTTPFilterMD5::slotInput(const TQByteArray &d)
89 {
90  context.update(d);
91  emit output(d);
92 }
93 
94 
95 HTTPFilterGZip::HTTPFilterGZip()
96 {
97 #ifdef DO_GZIP
98  bHasHeader = false;
99  bHasFinished = false;
100  bPlainText = false;
101  bEatTrailer = false;
102  bEof = false;
103  zstr.next_in = (Bytef *) Z_NULL;
104  zstr.avail_in = 0;
105  zstr.zalloc = Z_NULL;
106  zstr.zfree = Z_NULL;
107  zstr.opaque = Z_NULL;
108 
109  inflateInit2(&zstr, -MAX_WBITS);
110 
111  iTrailer = 8;
112 #endif
113 }
114 
115 HTTPFilterGZip::~HTTPFilterGZip()
116 {
117 #ifdef DO_GZIP
118  inflateEnd(&zstr);
119 #endif
120 
121 }
122 
123 /* The get_byte() and checkHeader() functions are modified version from */
124 /* the correpsonding functions that can be found in zlib, the following */
125 /* copyright notice applies to these functions: */
126 
127 /* zlib.h -- interface of the 'zlib' general purpose compression library
128  version 1.1.3, July 9th, 1998
129 
130  Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
131 
132  This software is provided 'as-is', without any express or implied
133  warranty. In no event will the authors be held liable for any damages
134  arising from the use of this software.
135 
136  Permission is granted to anyone to use this software for any purpose,
137  including commercial applications, and to alter it and redistribute it
138  freely, subject to the following restrictions:
139 
140  1. The origin of this software must not be misrepresented; you must not
141  claim that you wrote the original software. If you use this software
142  in a product, an acknowledgment in the product documentation would be
143  appreciated but is not required.
144  2. Altered source versions must be plainly marked as such, and must not be
145  misrepresented as being the original software.
146  3. This notice may not be removed or altered from any source distribution.
147 
148  Jean-loup Gailly Mark Adler
149  jloup@gzip.org madler@alumni.caltech.edu
150 
151 
152  The data format used by the zlib library is described by RFCs (Request for
153  Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
154  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
155 */
156 
157 int
158 HTTPFilterGZip::get_byte()
159 {
160 #ifdef DO_GZIP
161  if (bEof) return EOF;
162  if (zstr.avail_in == 0)
163  {
164  bEof = true;
165  return EOF;
166  }
167  zstr.avail_in--;
168  zstr.total_in++;
169  return *(zstr.next_in)++;
170 #else
171  return 0;
172 #endif
173 }
174 
175 #ifdef DO_GZIP
176 
177 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
178 
179 /* gzip flag byte */
180 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
181 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
182 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
183 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
184 #define COMMENT 0x10 /* bit 4 set: file comment present */
185 #define RESERVED 0xE0 /* bits 5..7: reserved */
186 #endif
187 
188 // 0 : ok
189 // 1 : not gzip
190 // 2 : no header
191 int
192 HTTPFilterGZip::checkHeader()
193 {
194 #ifdef DO_GZIP
195  uInt len;
196  int c;
197 
198  /* Check the gzip magic header */
199  for (len = 0; len < 2; len++) {
200  c = get_byte();
201  if (c != gz_magic[len]) {
202  if (len != 0)
203  {
204  zstr.avail_in++;
205  zstr.next_in--;
206  }
207  if (c != EOF) {
208  zstr.avail_in++;
209  zstr.next_in--;
210  return 1;
211  }
212  return 2;
213  }
214  }
215 
216  int method = get_byte(); /* method byte */
217  int flags = get_byte(); /* flags byte */
218 
219  if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
220  return bEof ? 2 : 1;
221  }
222 
223  /* Discard time, xflags and OS code: */
224  for (len = 0; len < 6; len++) (void)get_byte();
225 
226  if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
227  len = (uInt)get_byte();
228  len += ((uInt)get_byte())<<8;
229  /* len is garbage if EOF but the loop below will quit anyway */
230  while (len-- != 0 && get_byte() != EOF) ;
231  }
232  if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
233  while ((c = get_byte()) != 0 && c != EOF) ;
234  }
235  if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
236  while ((c = get_byte()) != 0 && c != EOF) ;
237  }
238  if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
239  for (len = 0; len < 2; len++) (void)get_byte();
240  }
241 
242  return bEof ? 2 : 0;
243 #else
244  return 0;
245 #endif
246 }
247 
248 void
249 HTTPFilterGZip::slotInput(const TQByteArray &d)
250 {
251 #ifdef DO_GZIP
252  if (bPlainText)
253  {
254  emit output(d);
255  return;
256  }
257  if (d.size() == 0)
258  {
259  if (bEatTrailer)
260  bHasFinished = true;
261  if (!bHasFinished)
262  {
263  // Make sure we get the last bytes still in the pipe.
264  // Needed with "deflate".
265  TQByteArray flush(4);
266  flush.fill(0);
267  slotInput(flush);
268  if (!bHasFinished && !bHasHeader)
269  {
270  // Send as-is
271  emit output(headerData);
272  bHasFinished = true;
273  // End of data
274  emit output(TQByteArray());
275  }
276  }
277  if (!bHasFinished)
278  emit error( TDEIO::ERR_SLAVE_DEFINED, i18n("Unexpected end of data, some information may be lost."));
279  return;
280  }
281  if (bHasFinished)
282  return;
283 
284  if (bEatTrailer)
285  {
286  iTrailer -= d.size();
287  if (iTrailer <= 0)
288  {
289  bHasFinished = true;
290  // End of data
291  emit output(TQByteArray());
292  }
293  return;
294  }
295 
296  if (!bHasHeader)
297  {
298  bEof = false;
299 
300  // Add data to header.
301  int orig_size = headerData.size();
302  headerData.resize(orig_size+d.size());
303  memcpy(headerData.data()+orig_size, d.data(), d.size());
304 
305  zstr.avail_in = headerData.size();
306  zstr.next_in = (Bytef *) headerData.data();
307 
308  int result = checkHeader();
309  if (result == 1)
310  {
311  bPlainText = true;
312  output(headerData);
313  return;
314  }
315 
316  if (result != 0)
317  return; // next time better
318 
319  bHasHeader = true;
320  }
321  else
322  {
323  zstr.avail_in = d.size();
324  zstr.next_in = (Bytef *) d.data();
325  }
326 
327  while( zstr.avail_in )
328  {
329  char buf[8192];
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))
334  {
335  emit error( TDEIO::ERR_SLAVE_DEFINED, i18n("Receiving corrupt data."));
336  break;
337  }
338  int bytesOut = 8192 - zstr.avail_out;
339  if (bytesOut)
340  {
341  TQByteArray d;
342  d.setRawData( buf, bytesOut );
343  emit output(d);
344  d.resetRawData( buf, bytesOut );
345  }
346  if (result == Z_STREAM_END)
347  {
348  if (iTrailer)
349  {
350  bEatTrailer = true;
351  }
352  else
353  {
354  bHasFinished = true;
355  // End of data
356  emit output(TQByteArray());
357  }
358  return;
359  }
360  }
361 #endif
362 }
363 
364 HTTPFilterDeflate::HTTPFilterDeflate()
365 {
366 #ifdef DO_GZIP
367  bHasHeader = true;
368  iTrailer = 0;
369 #endif
370 }
371 
372 #include "httpfilter.moc"

tdeio/httpfilter

Skip menu "tdeio/httpfilter"
  • Main Page
  • File List

tdeio/httpfilter

Skip menu "tdeio/httpfilter"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/httpfilter by doxygen 1.8.8
This website is maintained by Timothy Pearson.