• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

httpfilter.cc

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE libraries
00003    Copyright (c) 2002 Waldo Bastian <bastian@kde.org>
00004    Copyright 2009 David Faure <faure@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "httpfilter.h"
00022 #include <kgzipfilter.h>
00023 #include <kdebug.h>
00024 
00025 #include <klocale.h>
00026 
00027 #include <stdio.h>
00028 
00029 HTTPFilterBase::HTTPFilterBase()
00030  : last(0)
00031 {
00032 }
00033 
00034 HTTPFilterBase::~HTTPFilterBase()
00035 {
00036    delete last;
00037 }
00038 
00039 void
00040 HTTPFilterBase::chain(HTTPFilterBase *previous)
00041 {
00042    last = previous;
00043    connect(last, SIGNAL(output(QByteArray)),
00044            this, SLOT(slotInput(QByteArray)));
00045 }
00046 
00047 HTTPFilterChain::HTTPFilterChain()
00048  : first(0)
00049 {
00050 }
00051 
00052 void
00053 HTTPFilterChain::addFilter(HTTPFilterBase *filter)
00054 {
00055    if (!last)
00056    {
00057       first = filter;
00058    }
00059    else
00060    {
00061       disconnect(last, SIGNAL(output(QByteArray)), 0, 0);
00062       filter->chain(last);
00063    }
00064    last = filter;
00065    connect(filter, SIGNAL(output(QByteArray)),
00066            this, SIGNAL(output(QByteArray)));
00067    connect(filter, SIGNAL(error(QString)),
00068            this, SIGNAL(error(QString)));
00069 }
00070 
00071 void
00072 HTTPFilterChain::slotInput(const QByteArray &d)
00073 {
00074    if (first)
00075       first->slotInput(d);
00076    else
00077       emit output(d);
00078 }
00079 
00080 HTTPFilterMD5::HTTPFilterMD5()
00081 {
00082 }
00083 
00084 QString
00085 HTTPFilterMD5::md5()
00086 {
00087     return QString::fromLatin1(context.base64Digest());
00088 }
00089 
00090 void
00091 HTTPFilterMD5::slotInput(const QByteArray &d)
00092 {
00093    context.update(d);
00094    emit output(d);
00095 }
00096 
00097 
00098 HTTPFilterGZip::HTTPFilterGZip(bool deflate)
00099     : m_deflateMode(deflate),
00100       m_firstData(true),
00101       m_finished(false)
00102 {
00103     // We can't use KFilterDev because it assumes it can read as much data as necessary
00104     // from the underlying device. It's a pull strategy, while we have to do
00105     // a push strategy.
00106     m_gzipFilter = new KGzipFilter;
00107 }
00108 
00109 HTTPFilterGZip::~HTTPFilterGZip()
00110 {
00111     m_gzipFilter->terminate();
00112     delete m_gzipFilter;
00113 
00114 }
00115 
00116 /*
00117   The data format used by the zlib library is described by RFCs (Request for
00118   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
00119   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
00120 
00121   Use /usr/include/zlib.h as the primary source of documentation though.
00122 */
00123 
00124 void
00125 HTTPFilterGZip::slotInput(const QByteArray &d)
00126 {
00127     //kDebug() << "Got" << d.size() << "bytes as input";
00128     if (!d.isEmpty()) {
00129 
00130         if (m_firstData) {
00131             if (m_deflateMode) {
00132                 bool zlibHeader = true;
00133                 // Autodetect broken webservers (thanks Microsoft) who send raw-deflate
00134                 // instead of zlib-headers-deflate when saying Content-Encoding: deflate.
00135                 const char firstChar = d[0];
00136                 if ((firstChar & 0x0f) != 8) {
00137                     // In a zlib header, CM should be 8 (cf RFC 1950)
00138                     zlibHeader = false;
00139                 } else if (d.size() > 1) {
00140                     const char flg = d[1];
00141                     if ((firstChar * 256 + flg) % 31 != 0) { // Not a multiple of 31? invalid zlib header then
00142                         zlibHeader = false;
00143                     }
00144                 }
00145                 //if (!zlibHeader)
00146                 //    kDebug() << "Bad webserver, uses raw-deflate instead of zlib-deflate...";
00147                 m_gzipFilter->init(QIODevice::ReadOnly, zlibHeader ? KGzipFilter::ZlibHeader : KGzipFilter::RawDeflate);
00148             } else {
00149                 m_gzipFilter->init(QIODevice::ReadOnly, KGzipFilter::GZipHeader);
00150             }
00151             m_firstData = false;
00152         }
00153 
00154         m_gzipFilter->setInBuffer(d.constData(), d.size());
00155     }
00156     while (!m_gzipFilter->inBufferEmpty() && !m_finished) {
00157         char buf[8192];
00158         m_gzipFilter->setOutBuffer(buf, sizeof(buf));
00159         KFilterBase::Result result = m_gzipFilter->uncompress();
00160         //kDebug() << "uncompress returned" << result;
00161         switch (result) {
00162         case KFilterBase::Ok:
00163         case KFilterBase::End:
00164         {
00165             const int bytesOut = sizeof(buf) - m_gzipFilter->outBufferAvailable();
00166             if (bytesOut) {
00167                 emit output(QByteArray(buf, bytesOut));
00168             }
00169             if (result == KFilterBase::End) {
00170                 //kDebug() << "done, bHasFinished=true";
00171                 emit output(QByteArray());
00172                 m_finished = true;
00173             }
00174             break;
00175         }
00176         case KFilterBase::Error:
00177             kWarning() << "Error from KGZipFilter";
00178             emit error(i18n("Receiving corrupt data."));
00179             m_finished = true; // exit this while loop
00180             break;
00181         }
00182     }
00183 }
00184 
00185 HTTPFilterDeflate::HTTPFilterDeflate()
00186     : HTTPFilterGZip(true)
00187 {
00188 }
00189 
00190 #include "httpfilter.moc"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal