19 #include "dataprotocol.h"
24 #include <tdeio/global.h>
26 #include <tqcstring.h>
28 #include <tqstringlist.h>
29 #include <tqtextcodec.h>
32 # include <kinstance.h>
39 #if !defined(DATAKIOSLAVE) && !defined(TESTKIO)
40 # define DISPATCH(f) dispatch_##f
42 # define DISPATCH(f) f
45 using namespace TDEIO;
49 int kdemain(
int argc,
char **argv ) {
50 TDEInstance instance(
"tdeio_data" );
52 kdDebug(7101) <<
"*** Starting tdeio_data " << endl;
55 kdDebug(7101) <<
"Usage: tdeio_data protocol domain-socket1 domain-socket2" << endl;
62 kdDebug(7101) <<
"*** tdeio_data Done" << endl;
82 const TQChar text_plain_str[] = {
't',
'e',
'x',
't',
'/',
'p',
'l',
'a',
'i',
'n' };
83 const TQChar charset_str[] = {
'c',
'h',
'a',
'r',
's',
'e',
't' };
84 const TQChar us_ascii_str[] = {
'u',
's',
'-',
'a',
's',
'c',
'i',
'i' };
85 const TQChar base64_str[] = {
'b',
'a',
's',
'e',
'6',
'4' };
95 static int find(
const TQString &buf,
int begin, TQChar c1, TQChar c2 =
'\0',
98 int size = (int)buf.length();
100 TQChar ch = buf[pos];
102 || (c2 !=
'\0' && ch == c2)
103 || (c3 !=
'\0' && ch == c3))
120 inline TQString extract(
const TQString &buf,
int &pos, TQChar c1,
121 TQChar c2 =
'\0', TQChar c3 =
'\0') {
123 pos = find(buf,oldpos,c1,c2,c3);
124 return TQString(buf.unicode() + oldpos, pos - oldpos);
133 inline void ignoreWS(
const TQString &buf,
int &pos) {
134 int size = (int)buf.length();
135 TQChar ch = buf[pos];
136 while (pos < size && (ch ==
' ' || ch ==
'\t' || ch ==
'\n'
149 static TQString parseQuotedString(
const TQString &buf,
int &pos) {
150 int size = (int)buf.length();
153 bool escaped =
false;
155 while (parsing && pos < size) {
156 TQChar ch = buf[pos++];
162 case '"': parsing =
false;
break;
163 case '\\': escaped =
true;
break;
164 default: res += ch;
break;
176 static void parseDataHeader(
const KURL &url, DataHeader &header_info) {
177 TQConstString text_plain(text_plain_str,
sizeof text_plain_str/
sizeof text_plain_str[0]);
178 TQConstString charset(charset_str,
sizeof charset_str/
sizeof charset_str[0]);
179 TQConstString us_ascii(us_ascii_str,
sizeof us_ascii_str/
sizeof us_ascii_str[0]);
180 TQConstString base64(base64_str,
sizeof base64_str/
sizeof base64_str[0]);
182 header_info.mime_type = text_plain.string();
183 header_info.charset = &header_info.attributes.insert(
184 charset.string(),us_ascii.string())
186 header_info.is_base64 =
false;
189 TQString &raw_url = header_info.url = TQString::fromLatin1(
"data:") + url.path();
190 int raw_url_len = (int)raw_url.length();
193 header_info.data_offset = raw_url.find(
':');
194 header_info.data_offset++;
197 if (header_info.data_offset >= raw_url_len)
return;
198 TQString mime_type = extract(raw_url,header_info.data_offset,
';',
',')
200 if (!mime_type.isEmpty()) header_info.mime_type = mime_type;
202 if (header_info.data_offset >= raw_url_len)
return;
204 if (raw_url[header_info.data_offset++] ==
',')
return;
207 bool data_begin_reached =
false;
208 while (!data_begin_reached && header_info.data_offset < raw_url_len) {
210 TQString attribute = extract(raw_url,header_info.data_offset,
'=',
';',
',')
212 if (header_info.data_offset >= raw_url_len
213 || raw_url[header_info.data_offset] !=
'=') {
215 if (attribute == base64.string())
216 header_info.is_base64 =
true;
218 header_info.data_offset++;
221 ignoreWS(raw_url,header_info.data_offset);
222 if (header_info.data_offset >= raw_url_len)
return;
225 if (raw_url[header_info.data_offset] ==
'"') {
226 value = parseQuotedString(raw_url,header_info.data_offset);
227 ignoreWS(raw_url,header_info.data_offset);
229 value = extract(raw_url,header_info.data_offset,
';',
',')
233 header_info.attributes[attribute.lower()] = value;
236 if (header_info.data_offset < raw_url_len
237 && raw_url[header_info.data_offset] ==
',')
238 data_begin_reached =
true;
239 header_info.data_offset++;
244 DataProtocol::DataProtocol(
const TQCString &pool_socket,
const TQCString &app_socket)
245 :
SlaveBase(
"tdeio_data", pool_socket, app_socket) {
247 DataProtocol::DataProtocol() {
249 kdDebug() <<
"DataProtocol::DataProtocol()" << endl;
254 DataProtocol::~DataProtocol() {
255 kdDebug() <<
"DataProtocol::~DataProtocol()" << endl;
260 void DataProtocol::get(
const KURL& url) {
263 kdDebug() <<
"tdeio_data@"<<
this<<
"::get(const KURL& url)" << endl ;
266 parseDataHeader(url,hdr);
268 int size = (int)hdr.url.length();
269 int data_ofs = TQMIN(hdr.data_offset,size);
271 TQString url_data = hdr.url.mid(data_ofs);
280 KCodecs::base64Decode(url_data.local8Bit(),outData);
284 TQTextCodec *codec = TQTextCodec::codecForName(hdr.charset->latin1());
286 outData = codec->fromUnicode(url_data);
290 outData = url_data.local8Bit();
296 mimeType(hdr.mime_type);
299 totalSize(outData.size());
303 #if defined(TESTKIO) || defined(DATAKIOSLAVE)
304 MetaData::ConstIterator it;
305 for (it = hdr.attributes.begin(); it != hdr.attributes.end(); ++it) {
306 setMetaData(it.key(),it.data());
320 DISPATCH(data(TQByteArray()));
322 DISPATCH(finished());
329 void DataProtocol::mimetype(
const KURL &url) {
332 parseDataHeader(url,hdr);
333 mimeType(hdr.mime_type);
This tdeioslave provides support of data urls as specified by rfc 2397.
void setAllMetaData(const MetaData &)
Sets metadata.
void sendMetaData()
Sends metadata set with setAllMetaData.
There are two classes that specifies the protocol between application (job) and tdeioslave.
A namespace for TDEIO globals.