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 if (d.isEmpty()) 00128 return; 00129 00130 //kDebug() << "Got" << d.size() << "bytes as input"; 00131 if (m_firstData) { 00132 if (m_deflateMode) { 00133 bool zlibHeader = true; 00134 // Autodetect broken webservers (thanks Microsoft) who send raw-deflate 00135 // instead of zlib-headers-deflate when saying Content-Encoding: deflate. 00136 const char firstChar = d[0]; 00137 if ((firstChar & 0x0f) != 8) { 00138 // In a zlib header, CM should be 8 (cf RFC 1950) 00139 zlibHeader = false; 00140 } else if (d.size() > 1) { 00141 const char flg = d[1]; 00142 if ((firstChar * 256 + flg) % 31 != 0) { // Not a multiple of 31? invalid zlib header then 00143 zlibHeader = false; 00144 } 00145 } 00146 //if (!zlibHeader) 00147 // kDebug() << "Bad webserver, uses raw-deflate instead of zlib-deflate..."; 00148 m_gzipFilter->init(QIODevice::ReadOnly, zlibHeader ? KGzipFilter::ZlibHeader : KGzipFilter::RawDeflate); 00149 } else { 00150 m_gzipFilter->init(QIODevice::ReadOnly, KGzipFilter::GZipHeader); 00151 } 00152 m_firstData = false; 00153 } 00154 00155 m_gzipFilter->setInBuffer(d.constData(), d.size()); 00156 00157 while (!m_gzipFilter->inBufferEmpty() && !m_finished) { 00158 char buf[8192]; 00159 m_gzipFilter->setOutBuffer(buf, sizeof(buf)); 00160 KFilterBase::Result result = m_gzipFilter->uncompress(); 00161 //kDebug() << "uncompress returned" << result; 00162 switch (result) { 00163 case KFilterBase::Ok: 00164 case KFilterBase::End: 00165 { 00166 const int bytesOut = sizeof(buf) - m_gzipFilter->outBufferAvailable(); 00167 if (bytesOut) { 00168 emit output(QByteArray(buf, bytesOut)); 00169 } 00170 if (result == KFilterBase::End) { 00171 //kDebug() << "done, bHasFinished=true"; 00172 emit output(QByteArray()); 00173 m_finished = true; 00174 } 00175 break; 00176 } 00177 case KFilterBase::Error: 00178 kWarning() << "Error from KGZipFilter"; 00179 emit error(i18n("Receiving corrupt data.")); 00180 m_finished = true; // exit this while loop 00181 break; 00182 } 00183 } 00184 } 00185 00186 HTTPFilterDeflate::HTTPFilterDeflate() 00187 : HTTPFilterGZip(true) 00188 { 00189 } 00190 00191 #include "httpfilter.moc"
KDE 4.6 API Reference