KHtml

httpfiltergzip.cpp
1 /*
2  This file is part of the KDE libraries
3  Copyright (c) 2002 Waldo Bastian <[email protected]>
4  Copyright 2009 David Faure <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "httpfiltergzip_p.h"
22 #include <kcompressiondevice.h>
23 #include <kfilterbase.h>
24 #include <KLocalizedString>
25 #include "kmultipart_debug.h"
26 
27 HTTPFilterGZip::HTTPFilterGZip()
28  : m_firstData(true),
29  m_finished(false)
30 {
31  // We can't use KFilterDev because it assumes it can read as much data as necessary
32  // from the underlying device. It's a pull strategy, while we have to do
33  // a push strategy.
34  m_gzipFilter = KCompressionDevice::filterForCompressionType(KCompressionDevice::GZip);
35 }
36 
37 HTTPFilterGZip::~HTTPFilterGZip()
38 {
39  m_gzipFilter->terminate();
40  delete m_gzipFilter;
41 
42 }
43 
44 /*
45  The data format used by the zlib library is described by RFCs (Request for
46  Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
47  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
48 
49  Use /usr/include/zlib.h as the primary source of documentation though.
50 */
51 
52 void
53 HTTPFilterGZip::slotInput(const QByteArray &d)
54 {
55  if (d.isEmpty()) {
56  return;
57  }
58 
59  //qCDebug(KHTML_LOG) << "Got" << d.size() << "bytes as input";
60  if (m_firstData) {
61  m_gzipFilter->setFilterFlags(KFilterBase::WithHeaders);
62  m_gzipFilter->init(QIODevice::ReadOnly);
63  m_firstData = false;
64  }
65 
66  m_gzipFilter->setInBuffer(d.constData(), d.size());
67 
68  while (!m_gzipFilter->inBufferEmpty() && !m_finished) {
69  char buf[8192];
70  m_gzipFilter->setOutBuffer(buf, sizeof(buf));
71  KFilterBase::Result result = m_gzipFilter->uncompress();
72  //qCDebug(KMULTIPART_LOG) << "uncompress returned" << result;
73  switch (result) {
74  case KFilterBase::Ok:
75  case KFilterBase::End: {
76  const int bytesOut = sizeof(buf) - m_gzipFilter->outBufferAvailable();
77  if (bytesOut) {
78  emit output(QByteArray(buf, bytesOut));
79  }
80  if (result == KFilterBase::End) {
81  //qCDebug(KMULTIPART_LOG) << "done, bHasFinished=true";
82  emit output(QByteArray());
83  m_finished = true;
84  }
85  break;
86  }
87  case KFilterBase::Error:
88  qCDebug(KMULTIPART_LOG) << "Error from KGZipFilter";
89  emit error(i18n("Receiving corrupt data."));
90  m_finished = true; // exit this while loop
91  break;
92  }
93  }
94 }
bool isEmpty() const const
static KFilterBase * filterForCompressionType(CompressionType type)
const char * constData() const const
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString i18n(const char *text, const TYPE &arg...)
int size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.