26 #include <QtCore/QByteArray>
27 #include <QtCore/QCharRef>
28 #include <QtCore/QMutableStringListIterator>
29 #include <QtCore/QTextCodec>
32 # include <kinstance.h>
36 #if !defined(DATAKIOSLAVE)
37 # define DISPATCH(f) dispatch_##f
39 # define DISPATCH(f) f
46 int kdemain(
int argc,
char **argv ) {
49 kDebug(7101) <<
"*** Starting kio_data ";
52 kDebug(7101) <<
"Usage: kio_data protocol domain-socket1 domain-socket2";
59 kDebug(7101) <<
"*** kio_data Done";
85 static int find(
const QByteArray &buf,
int begin,
const char c1)
87 static const char comma =
',';
88 static const char semicolon =
';';
90 int size = buf.length();
92 const char ch = buf[pos];
93 if (ch == comma || ch == semicolon || (c1 !=
'\0' && ch == c1))
109 const char c1 =
'\0')
112 pos =
find(buf, oldpos, c1);
113 return buf.mid(oldpos, pos-oldpos);
124 int size = buf.length();
125 while (pos < size && buf[pos].isSpace())
138 int size = buf.length();
142 bool escaped =
false;
144 while (parsing && pos < size) {
145 const QChar ch = buf[pos++];
150 switch (ch.unicode()) {
151 case '"': parsing =
false;
break;
152 case '\\': escaped =
true;
break;
153 default: res += ch;
break;
173 DataHeader header_info;
176 header_info.mime_type = text_plain;
177 header_info.attributes.insert(charset, us_ascii);
178 header_info.is_base64 =
false;
181 const QByteArray &raw_url = header_info.url = QByteArray::fromPercentEncoding( url.encodedPath() );
182 const int raw_url_len = raw_url.length();
184 header_info.data_offset = 0;
187 if (raw_url_len == 0)
189 const QString mime_type =
extract(raw_url, header_info.data_offset).trimmed();
190 if (!mime_type.isEmpty()) header_info.mime_type = mime_type;
194 if (header_info.data_offset >= raw_url_len)
197 if (raw_url[header_info.data_offset++] == QLatin1Char(
','))
201 bool data_begin_reached =
false;
202 while (!data_begin_reached && header_info.data_offset < raw_url_len) {
204 const QString attribute =
extract(raw_url, header_info.data_offset,
'=').trimmed();
205 if (header_info.data_offset >= raw_url_len
206 || raw_url[header_info.data_offset] != QLatin1Char(
'=')) {
208 if (attribute == base64)
209 header_info.is_base64 =
true;
211 header_info.data_offset++;
214 ignoreWS(raw_url,header_info.data_offset);
215 if (header_info.data_offset >= raw_url_len)
219 if (raw_url[header_info.data_offset] == QLatin1Char(
'"')) {
221 ignoreWS(raw_url,header_info.data_offset);
223 value =
extract(raw_url, header_info.data_offset).trimmed();
226 header_info.attributes[attribute.toLower()] = value;
229 if (header_info.data_offset < raw_url_len
230 && raw_url[header_info.data_offset] == QLatin1Char(
','))
231 data_begin_reached =
true;
232 header_info.data_offset++;
240 :
SlaveBase(
"kio_data", pool_socket, app_socket) {
242 DataProtocol::DataProtocol() {
257 kDebug() <<
"kio_data@"<<
this<<
"::get(const KUrl& url)";
261 const int size = hdr.url.length();
262 const int data_ofs = qMin(hdr.data_offset, size);
264 const QByteArray url_data = hdr.url.mid(data_ofs);
270 outData = QByteArray::fromBase64(url_data);
272 QTextCodec *codec = QTextCodec::codecForName(hdr.attributes[
"charset"].toLatin1());
274 outData = codec->toUnicode(url_data).toUtf8();
286 #if defined(DATAKIOSLAVE)
287 MetaData::ConstIterator it;
288 for (it = hdr.attributes.constBegin(); it != hdr.attributes.constEnd(); ++it) {
289 setMetaData(it.key(),it.value());
void sendMetaData()
Sends metadata set with setAllMetaData.
void data(const QByteArray &)
virtual void get(const KUrl &url)
static QString extract(const QByteArray &buf, int &pos, const char c1= '\0')
extracts the string between the current position pos and the first occurrence of either c1 or comma (...
static void ignoreWS(const QString &buf, int &pos)
ignores all whitespaces
static QString parseQuotedString(const QString &buf, int &pos)
parses a quoted string as per rfc 822.
void totalSize(KIO::filesize_t)
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
void setAllMetaData(const MetaData &)
Sets metadata.
There are two classes that specifies the protocol between application (job) and kioslave.
static int find(const QByteArray &buf, int begin, const char c1)
returns the position of the first occurrence of any of the given characters c1 or comma ('...
void mimeType(const QString &)
static DataHeader parseDataHeader(const KUrl &url, const bool mimeOnly)
parses the header of a data url
virtual void mimetype(const KUrl &url)
const QString & staticQString(const char *str)
This kioslave provides support of data urls as specified by rfc 2397.