• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

KIO

dataprotocol.cpp
Go to the documentation of this file.
00001 //  dataprotocol.cpp
00002 // ==================
00003 //
00004 // Implementation of the data protocol (rfc 2397)
00005 //
00006 // Author: Leo Savernik
00007 // Email: l.savernik@aon.at
00008 // Copyright (C) 2002, 2003 by Leo Savernik <l.savernik@aon.at>
00009 // Created: Sam Dez 28 14:11:18 CET 2002
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU Lesser General Public License as        *
00015  *   published by the Free Software Foundation; version 2.                 *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #include "dataprotocol.h"
00020 
00021 #include <kdebug.h>
00022 #include <kurl.h>
00023 #include "global.h"
00024 #include <kglobal.h>
00025 
00026 #include <QtCore/QByteArray>
00027 #include <QtCore/QCharRef>
00028 #include <QtCore/QMutableStringListIterator>
00029 #include <QtCore/QTextCodec>
00030 
00031 #ifdef DATAKIOSLAVE
00032 #  include <kinstance.h>
00033 #  include <stdlib.h>
00034 #endif
00035 
00036 #if !defined(DATAKIOSLAVE)
00037 #  define DISPATCH(f) dispatch_##f
00038 #else
00039 #  define DISPATCH(f) f
00040 #endif
00041 
00042 using namespace KIO;
00043 #ifdef DATAKIOSLAVE
00044 extern "C" {
00045 
00046   int kdemain( int argc, char **argv ) {
00047     KComponentData componentData( "kio_data" );
00048 
00049     kDebug(7101) << "*** Starting kio_data ";
00050 
00051     if (argc != 4) {
00052       kDebug(7101) << "Usage: kio_data  protocol domain-socket1 domain-socket2";
00053       exit(-1);
00054     }
00055 
00056     DataProtocol slave(argv[2], argv[3]);
00057     slave.dispatchLoop();
00058 
00059     kDebug(7101) << "*** kio_data Done";
00060     return 0;
00061   }
00062 }
00063 #endif
00064 
00066 struct DataHeader {
00067   QString mime_type;        // mime type of content (lowercase)
00068   MetaData attributes;      // attribute/value pairs (attribute lowercase,
00069                 //  value unchanged)
00070   bool is_base64;       // true if data is base64 encoded
00071   QString url;          // reference to decoded url
00072   int data_offset;      // zero-indexed position within url
00073                 // where the real data begins. May point beyond
00074                     // the end to indicate that there is no data
00075   QString charset;      // shortcut to charset (it always exists)
00076 };
00077 
00087 static int find(const QString &buf, int begin, QChar c1,
00088         QChar c2 = QLatin1Char('\0'), QChar c3 = QLatin1Char('\0')) {
00089   int pos = begin;
00090   int size = buf.length();
00091   while (pos < size) {
00092     QChar ch = buf[pos];
00093     if (ch == c1
00094         || (c2 != QLatin1Char('\0') && ch == c2)
00095     || (c3 != QLatin1Char('\0') && ch == c3))
00096       break;
00097     pos++;
00098   }/*wend*/
00099   return pos;
00100 }
00101 
00112 inline QString extract(const QString &buf, int &pos, QChar c1,
00113         QChar c2 = QLatin1Char('\0'), QChar c3 = QLatin1Char('\0')) {
00114   int oldpos = pos;
00115   pos = find(buf,oldpos,c1,c2,c3);
00116   return buf.mid(oldpos, pos-oldpos);
00117 }
00118 
00125 inline void ignoreWS(const QString &buf, int &pos) {
00126   int size = buf.length();
00127   QChar ch = buf[pos];
00128   while (pos < size && ch.isSpace())
00129     ch = buf[++pos];
00130 }
00131 
00140 static QString parseQuotedString(const QString &buf, int &pos) {
00141   int size = buf.length();
00142   QString res;
00143   res.reserve(size);    // can't be larger than buf
00144   pos++;        // jump over leading quote
00145   bool escaped = false; // if true means next character is literal
00146   bool parsing = true;  // true as long as end quote not found
00147   while (parsing && pos < size) {
00148     QChar ch = buf[pos++];
00149     if (escaped) {
00150       res += ch;
00151       escaped = false;
00152     } else {
00153       switch (ch.unicode()) {
00154         case '"': parsing = false; break;
00155         case '\\': escaped = true; break;
00156         default: res += ch; break;
00157       }/*end switch*/
00158     }/*end if*/
00159   }/*wend*/
00160   res.squeeze();
00161   return res;
00162 }
00163 
00169 static void parseDataHeader(const KUrl &url, DataHeader &header_info) {
00170   static const QString& text_plain = KGlobal::staticQString("text/plain");
00171   static const QString& charset = KGlobal::staticQString("charset");
00172   static const QString& us_ascii = KGlobal::staticQString("us-ascii");
00173   static const QString& base64 = KGlobal::staticQString("base64");
00174 
00175   // initialize header info members
00176   header_info.mime_type = text_plain;
00177   header_info.charset = header_info.attributes.insert(charset,us_ascii).value();
00178   header_info.is_base64 = false;
00179 
00180   // decode url and save it
00181   QString &raw_url = header_info.url = url.path().toLatin1();
00182   int raw_url_len = raw_url.length();
00183 
00184   header_info.data_offset = 0;
00185 
00186   // read mime type
00187   if (raw_url_len == 0)
00188     return;
00189   QString mime_type = extract(raw_url, header_info.data_offset,
00190                   QLatin1Char(';'), QLatin1Char(',')).trimmed();
00191   if (!mime_type.isEmpty()) header_info.mime_type = mime_type;
00192 
00193   if (header_info.data_offset >= raw_url_len) return;
00194   // jump over delimiter token and return if data reached
00195   if (raw_url[header_info.data_offset++] == QLatin1Char(',')) return;
00196 
00197   // read all attributes and store them
00198   bool data_begin_reached = false;
00199   while (!data_begin_reached && header_info.data_offset < raw_url_len) {
00200     // read attribute
00201     QString attribute = extract(raw_url, header_info.data_offset,
00202                     QLatin1Char('='), QLatin1Char(';'),
00203                 QLatin1Char(',')).trimmed();
00204     if (header_info.data_offset >= raw_url_len
00205         || raw_url[header_info.data_offset] != QLatin1Char('=')) {
00206       // no assigment, must be base64 option
00207       if (attribute == base64)
00208         header_info.is_base64 = true;
00209     } else {
00210       header_info.data_offset++; // jump over '=' token
00211 
00212       // read value
00213       ignoreWS(raw_url,header_info.data_offset);
00214       if (header_info.data_offset >= raw_url_len) return;
00215 
00216       QString value;
00217       if (raw_url[header_info.data_offset] == QLatin1Char('"')) {
00218         value = parseQuotedString(raw_url,header_info.data_offset);
00219         ignoreWS(raw_url,header_info.data_offset);
00220       } else
00221         value = extract(raw_url, header_info.data_offset, QLatin1Char(';'),
00222             QLatin1Char(',')).trimmed();
00223 
00224       // add attribute to map
00225       header_info.attributes[attribute.toLower()] = value;
00226 
00227     }/*end if*/
00228     if (header_info.data_offset < raw_url_len
00229     && raw_url[header_info.data_offset] == QLatin1Char(','))
00230       data_begin_reached = true;
00231     header_info.data_offset++; // jump over separator token
00232   }/*wend*/
00233 }
00234 
00235 #ifdef DATAKIOSLAVE
00236 DataProtocol::DataProtocol(const QByteArray &pool_socket, const QByteArray &app_socket)
00237     : SlaveBase("kio_data", pool_socket, app_socket) {
00238 #else
00239 DataProtocol::DataProtocol() {
00240 #endif
00241   kDebug();
00242 }
00243 
00244 /* --------------------------------------------------------------------- */
00245 
00246 DataProtocol::~DataProtocol() {
00247   kDebug();
00248 }
00249 
00250 /* --------------------------------------------------------------------- */
00251 
00252 void DataProtocol::get(const KUrl& url) {
00253   ref();
00254   kDebug() << "kio_data@"<<this<<"::get(const KUrl& url)";
00255 
00256   DataHeader hdr;
00257   parseDataHeader(url,hdr);
00258 
00259   int size = hdr.url.length();
00260   int data_ofs = qMin(hdr.data_offset,size);
00261   // FIXME: string is copied, would be nice if we could have a reference only
00262   QString url_data = hdr.url.mid(data_ofs);
00263   QByteArray outData;
00264 
00265   if (hdr.is_base64) {
00266     // base64 stuff is expected to contain the correct charset, so we just
00267     // decode it and pass it to the receiver
00268     outData = QByteArray::fromBase64(url_data.toUtf8());
00269   } else {
00270     // FIXME: This is all flawed, must be reworked thoroughly
00271     // non encoded data must be converted to the given charset
00272     QTextCodec *codec = QTextCodec::codecForName(hdr.charset.toLatin1());
00273     if (codec != 0) {
00274       outData = codec->fromUnicode(url_data);
00275     } else {
00276       // if there is no approprate codec, just use local encoding. This
00277       // should work for >90% of all cases.
00278       outData = url_data.toLocal8Bit();
00279     }/*end if*/
00280   }/*end if*/
00281 
00282   //kDebug() << "emit mimeType@"<<this;
00283   mimeType(hdr.mime_type);
00284   //kDebug() << "emit totalSize@"<<this;
00285   totalSize(outData.size());
00286 
00287   //kDebug() << "emit setMetaData@"<<this;
00288 #if defined(DATAKIOSLAVE)
00289   MetaData::ConstIterator it;
00290   for (it = hdr.attributes.constBegin(); it != hdr.attributes.constEnd(); ++it) {
00291     setMetaData(it.key(),it.value());
00292   }/*next it*/
00293 #else
00294   setAllMetaData(hdr.attributes);
00295 #endif
00296 
00297   //kDebug() << "emit sendMetaData@"<<this;
00298   sendMetaData();
00299 //   kDebug() << "(1) queue size " << dispatchQueue.size();
00300   // empiric studies have shown that this shouldn't be queued & dispatched
00301   data(outData);
00302 //   kDebug() << "(2) queue size " << dispatchQueue.size();
00303   DISPATCH(data(QByteArray()));
00304 //   kDebug() << "(3) queue size " << dispatchQueue.size();
00305   DISPATCH(finished());
00306 //   kDebug() << "(4) queue size " << dispatchQueue.size();
00307   deref();
00308 }
00309 
00310 /* --------------------------------------------------------------------- */
00311 
00312 void DataProtocol::mimetype(const KUrl &url) {
00313   ref();
00314   DataHeader hdr;
00315   parseDataHeader(url,hdr);
00316   mimeType(hdr.mime_type);
00317   finished();
00318   deref();
00319 }
00320 
00321 /* --------------------------------------------------------------------- */
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:20:54 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

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

kdelibs-4.8.3 API Reference

Skip menu "kdelibs-4.8.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal