fix return value
[enigma2.git] / lib / network / xmlrpc.cpp
1 #ifndef DISABLE_NETWORK
2
3 #include <lib/network/xmlrpc.h>
4
5
6 static std::map<eString, int (*)(std::vector<eXMLRPCVariant>&, ePtrList<eXMLRPCVariant>&)> rpcproc;
7
8 void eXMLRPCVariant::zero()
9 {
10         _struct=0;
11         _array=0;
12         _i4=0;
13         _boolean=0;
14         _string=0;
15         _double=0;
16 //      _datetime=0;
17 //      _base64=0;
18 }
19
20 eXMLRPCVariant::eXMLRPCVariant(std::map<eString,eXMLRPCVariant*> *__struct)
21 {
22         zero();
23         _struct=__struct;
24 }
25
26 eXMLRPCVariant::eXMLRPCVariant(std::vector<eXMLRPCVariant> *__array)
27 {
28         zero();
29         _array=__array;
30 }
31
32 eXMLRPCVariant::eXMLRPCVariant(__s32 *__i4)
33 {
34         zero();
35         _i4=__i4;
36 }
37
38 eXMLRPCVariant::eXMLRPCVariant(bool *__boolean)
39 {
40         zero();
41         _boolean=__boolean;
42 }
43
44 eXMLRPCVariant::eXMLRPCVariant(eString *__string)
45 {
46         zero();
47         _string=__string;
48 }
49
50 eXMLRPCVariant::eXMLRPCVariant(double *__double)
51 {
52         zero();
53         _double=__double;
54 }
55
56 /*eXMLRPCVariant::eXMLRPCVariant(QDateTime *__datetime)
57 {
58         zero();
59         _datetime=__datetime;
60 } */
61
62 /*eXMLRPCVariant::eXMLRPCVariant(QByteArray *__base64)
63 {
64         zero();
65         _base64=__base64;
66 } */
67
68 eXMLRPCVariant::eXMLRPCVariant(const eXMLRPCVariant &c)
69 {
70         zero();
71         if (c._i4)
72                 _i4=new int(*c._i4);
73         if (c._boolean)
74                 _boolean=new bool(*c._boolean);
75         if (c._string)
76                 _string=new eString(*c._string);
77         if (c._double)
78                 _double=new double(*c._double);
79         // datetime, base64
80         if (c._struct)
81         {
82                 _struct=new std::map<eString,eXMLRPCVariant*>;
83                 for (std::map<eString,eXMLRPCVariant*>::iterator b(c._struct->begin()); b != c._struct->end(); ++b)
84                         _struct->insert(std::pair<eString,eXMLRPCVariant*>(b->first, new eXMLRPCVariant(*b->second)));
85         }
86         if (c._array)
87                 _array = new std::vector<eXMLRPCVariant>(*c._array);
88 }
89
90 eXMLRPCVariant::~eXMLRPCVariant()
91 {
92         if (_struct)
93         {
94                 for (std::map<eString,eXMLRPCVariant*>::iterator i(_struct->begin()); i != _struct->end(); ++i)
95                         delete i->second;
96
97                 delete _struct;
98         }
99         if (_array)
100                 delete _array;
101         if (_i4)
102                 delete _i4;
103         if (_boolean)
104                 delete _boolean;
105         if (_string)
106                 delete _string;
107         if (_double)
108                 delete _string;
109 /*      if (_datetime)
110                 delete _datetime;*/
111 /*      if (_base64)
112                 delete _base64;*/
113 }
114
115 std::map<eString,eXMLRPCVariant*> *eXMLRPCVariant::getStruct()
116 {
117         return _struct;
118 }
119
120 std::vector<eXMLRPCVariant> *eXMLRPCVariant::getArray()
121 {
122         return _array;
123 }
124
125 __s32 *eXMLRPCVariant::getI4()
126 {
127         return _i4;
128 }
129
130 bool *eXMLRPCVariant::getBoolean()
131 {
132         return _boolean;
133 }
134
135 eString *eXMLRPCVariant::getString()
136 {
137         return _string;
138 }
139
140 double *eXMLRPCVariant::getDouble()
141 {
142         return _double;
143 }
144
145 /*QDateTime *eXMLRPCVariant::getDatetime()
146 {
147         return _datetime;
148 } */
149
150 /*QByteArray *eXMLRPCVariant::getBase64()
151 {
152         return _base64;
153 } */
154
155 void eXMLRPCVariant::toXML(eString &result)
156 {
157         if (getArray())
158         {
159                 static eString s1("<value><array><data>");
160                 result+=s1;
161                 for (unsigned int i=0; i<getArray()->size(); i++)
162                 {
163                         static eString s("  ");
164                         result+=s;
165                         (*getArray())[i].toXML(result);
166                         static eString s1("\n");
167                         result+=s1;
168                 }
169                 static eString s2("</data></array></value>\n");
170                 result+=s2;
171         } else if (getStruct())
172         {
173                 static eString s1("<value><struct>");
174                 result+=s1;
175                 for (std::map<eString,eXMLRPCVariant*>::iterator i(_struct->begin()); i != _struct->end(); ++i)
176                 {
177                         static eString s1("  <member><name>");
178                         result+=s1;
179                         result+=i->first;
180                         static eString s2("</name>");
181                         result+=s2;
182                         i->second->toXML(result);
183                         static eString s3("</member>\n");
184                         result+=s3;
185                 }
186                 static eString s2("</struct></value>\n");
187                 result+=s2;
188         } else if (getI4())
189         {
190                 static eString s1("<value><i4>");
191                 result+=s1;
192                 result+=eString().setNum(*getI4());
193                 static eString s2("</i4></value>");
194                 result+=s2;
195         } else if (getBoolean())
196         {
197                 static eString s0("<value><boolean>0</boolean></value>");
198                 static eString s1("<value><boolean>1</boolean></value>");
199                 result+=(*getBoolean())?s1:s0;
200         } else if (getString())
201         {
202                 static eString s1("<value><string>");
203                 static eString s2("</string></value>");
204                 result+=s1;
205                 result+=*getString();
206                 result+=s2;
207         } else if (getDouble())
208         {
209                 result+=eString().sprintf("<value><double>%lf</double></value>", *getDouble());
210         }       else
211                 eFatal("couldn't append");
212 }
213
214 static eXMLRPCVariant *fromXML(XMLTreeNode *n)
215 {
216         if (strcmp(n->GetType(), "value"))
217                 return 0;
218         n=n->GetChild();
219         const char *data=n->GetData();
220         if (!data)
221                 data="";
222         if ((!strcmp(n->GetType(), "i4")) || (!strcmp(n->GetType(), "int")))
223                 return new eXMLRPCVariant(new int(atoi(data)));
224         else if (!strcmp(n->GetType(), "boolean"))
225                 return new eXMLRPCVariant(new bool(atoi(data)));
226         else if (!strcmp(n->GetType(), "string"))
227                 return new eXMLRPCVariant(new eString(data));
228         else if (!strcmp(n->GetType(), "double"))
229                 return new eXMLRPCVariant(new double(atof(data)));
230         else if (!strcmp(n->GetType(), "struct")) {
231                 std::map<eString,eXMLRPCVariant*> *s=new std::map<eString,eXMLRPCVariant*>;
232                 for (n=n->GetChild(); n; n=n->GetNext())
233                 {
234                         if (strcmp(data, "member"))
235                         {
236                                 delete s;
237                                 return 0;
238                         }
239                         eString name=0;
240                         eXMLRPCVariant *value;
241                         for (XMLTreeNode *v=n->GetChild(); v; v=v->GetNext())
242                         {
243                                 if (!strcmp(v->GetType(), "name"))
244                                         name=eString(v->GetData());
245                                 else if (!strcmp(v->GetType(), "value"))
246                                         value=fromXML(v);
247                         }
248                         if ((!value) || (!name))
249                         {
250                                 delete s;
251                                 return 0;
252                         }
253                         s->INSERT(name,value);
254                 }
255                 return new eXMLRPCVariant(s);
256         } else if (!strcmp(n->GetType(), "array"))
257         {
258                 ePtrList<eXMLRPCVariant> l;
259                 l.setAutoDelete(true);
260                 n=n->GetChild();
261                 if (strcmp(data, "data"))
262                         return 0;
263                 for (n=n->GetChild(); n; n=n->GetNext())
264                         if (!strcmp(n->GetType(), "value"))
265                         {
266                                 eXMLRPCVariant *value=fromXML(n);
267                                 if (!value)
268                                         return 0;
269                                 l.push_back(value);
270                         }
271
272                 return new eXMLRPCVariant( l.getVector() );
273         }
274         eDebug("couldn't convert %s", n->GetType());
275         return 0;
276 }
277
278 eXMLRPCResponse::eXMLRPCResponse(eHTTPConnection *c):
279         eHTTPDataSource(c), parser("ISO-8859-1")
280 {
281         // size etc. setzen aber erst NACH data-phase
282         connection->localstate=eHTTPConnection::stateWait;
283 }
284
285 eXMLRPCResponse::~eXMLRPCResponse()
286 {
287 }
288
289 int eXMLRPCResponse::doCall()
290 {
291         eDebug("doing call");
292         result="";
293                 // get method name
294         eString methodName=0;
295         
296         if (connection->remote_header["Content-Type"]!="text/xml")
297         {
298                 eDebug("remote header failure (%s != text/xml)", (connection->remote_header["Content-Type"]).c_str());
299                 return -3;
300         }
301         
302         XMLTreeNode *methodCall=parser.RootNode();
303         if (!methodCall)
304         {
305                 eDebug("empty xml");
306                 return -1;
307         }
308         if (strcmp(methodCall->GetType(), "methodCall"))
309         {
310                 eDebug("no methodCall found");
311                 return -2;
312         }
313
314         ePtrList<eXMLRPCVariant> params;
315         params.setAutoDelete(true);
316         
317         for (XMLTreeNode *c=methodCall->GetChild(); c; c=c->GetNext())
318         {
319                 if (!strcmp(c->GetType(), "methodName"))
320                         methodName=eString(c->GetData());
321                 else if (!strcmp(c->GetType(), "params"))
322                 {
323                         for (XMLTreeNode *p=c->GetChild(); p; p=p->GetNext())
324                                 if (!strcmp(p->GetType(), "param"))
325                                         params.push_back(fromXML(p->GetChild()));
326                 } else
327                 {
328                         eDebug("unknown stuff found");
329                         return 0;
330                 }
331         }
332         
333         if (!methodName)
334         {
335                 eDebug("no methodName found!");
336                 return -3;
337         }
338         
339         eDebug("methodName: %s", methodName.c_str() );
340         
341         result="<?xml version=\"1.0\"?>\n"
342                 "<methodResponse>";
343         
344         ePtrList<eXMLRPCVariant> ret;
345         ret.setAutoDelete(true);
346
347         int (*proc)(std::vector<eXMLRPCVariant>&, ePtrList<eXMLRPCVariant> &)=rpcproc[methodName];
348         int fault;
349
350         std::vector<eXMLRPCVariant>* v = params.getVector();
351         
352         if (!proc)
353         {
354                 fault=1;
355                 xmlrpc_fault(ret, -1, "called method not present");             
356         } else
357                 fault=proc( *v , ret);
358
359         delete v;
360
361         eDebug("converting to text...");
362
363         if (fault)
364         {
365                 result+="<fault>\n";
366                 ret.current()->toXML(result);
367                 result+="</fault>\n";
368         } else
369         {
370                 result+="<params>\n";
371                 for (ePtrList<eXMLRPCVariant>::iterator i(ret); i != ret.end(); ++i)
372                 {
373                         result+="<param>";
374                         i->toXML(result);
375                         result+="</param>";
376                 }
377                 result+="</params>";
378         }
379         result+="</methodResponse>";
380         char buffer[10];
381         snprintf(buffer, 10, "%d", size=result.length());
382         wptr=0;
383         connection->local_header["Content-Type"]="text/xml";
384         connection->local_header["Content-Length"]=buffer;
385         connection->code=200;
386         connection->code_descr="OK";
387         connection->localstate=eHTTPConnection::stateResponse;
388         return 0;
389 }
390
391 int eXMLRPCResponse::doWrite(int hm)
392 {
393         int tw=size-wptr;
394         if (tw>hm)
395                 tw=hm;
396         if (tw<=0)
397                 return -1;
398         connection->writeBlock(result.c_str()+wptr, tw);
399         wptr+=tw;
400         return size > wptr ? 1 : -1;
401 }
402
403 void eXMLRPCResponse::haveData(void *data, int len)
404 {
405         if (result)
406                 return;
407         int err=0;
408
409         if (!parser.Parse((char*)data, len, !len))
410         {
411                 char temp[len+1];
412                 temp[len]=0;
413                 memcpy(temp, data, len);
414                 eDebug("%s: %s", temp, parser.ErrorString(parser.GetErrorCode()));
415                 err=1;
416         }
417         
418         if ((!err) && (!len))
419                 err=doCall();
420
421         if (err)
422         {
423                 eDebug("schade: %d", err);
424                 connection->code=400;
425                 connection->code_descr="Bad request";
426                 char buffer[10];
427                 snprintf(buffer, 10, "%d", size=result.length());
428                 wptr=0;
429                 connection->local_header["Content-Type"]="text/html";
430                 connection->local_header["Content-Length"]=buffer;
431                 result.sprintf("XMLRPC error %d\n", err);
432                 connection->localstate=eHTTPConnection::stateResponse;
433         }
434 }
435
436 void xmlrpc_initialize(eHTTPD *httpd)
437 {
438         httpd->addResolver(new eHTTPXMLRPCResolver);
439 }
440
441 void xmlrpc_addMethod(eString methodName, int (*proc)(std::vector<eXMLRPCVariant>&, ePtrList<eXMLRPCVariant>&))
442 {
443         rpcproc[methodName]=proc;
444 }
445
446 void xmlrpc_fault(ePtrList<eXMLRPCVariant> &res, int faultCode, eString faultString)
447 {
448         std::map<eString,eXMLRPCVariant*> *s=new std::map<eString,eXMLRPCVariant*>;
449         s->INSERT("faultCode", new eXMLRPCVariant(new __s32(faultCode)));
450         s->INSERT("faultString", new eXMLRPCVariant(new eString(faultString)));
451         res.push_back(new eXMLRPCVariant(s));
452 }
453
454 int xmlrpc_checkArgs(eString args, std::vector<eXMLRPCVariant> &parm, ePtrList<eXMLRPCVariant> &res)
455 {
456         if (parm.size() != args.length())
457         {
458                 xmlrpc_fault(res, -500, eString().sprintf("parameter count mismatch (found %d, expected %d)", parm.size(), args.length()));
459                 return 1;
460         }
461         
462         for (unsigned int i=0; i<args.length(); i++)
463         {
464                 switch (args[i])
465                 {
466                 case 'i':
467                         if (parm[i].getI4())
468                                 continue;
469                         break;
470                 case 'b':
471                         if (parm[i].getBoolean())
472                                 continue;
473                         break;
474                 case 's':
475                         if (parm[i].getString())
476                                 continue;
477                         break;
478                 case 'd':
479                         if (parm[i].getDouble())
480                                 continue;
481                         break;
482 /*              case 't':
483                         if (parm[i].getDatetime())
484                                 continue;
485                         break;
486                 case '6':
487                         if (parm[i].getBase64())
488                                 continue;
489                         break;*/
490                 case '$':
491                         if (parm[i].getStruct())
492                                 continue;
493                         break;
494                 case 'a':
495                         if (parm[i].getArray())
496                                 continue;
497                         break;
498                 }
499                 xmlrpc_fault(res, -501, eString().sprintf("parameter type mismatch, expected %c as #%d", args[i], i));
500                 return 1;
501         }
502         return 0;
503 }
504
505 eHTTPXMLRPCResolver::eHTTPXMLRPCResolver()
506 {
507 }
508
509 eHTTPDataSource *eHTTPXMLRPCResolver::getDataSource(eString request, eString path, eHTTPConnection *conn)
510 {
511         if ((path=="/RPC2") && (request=="POST"))
512                 return new eXMLRPCResponse(conn);
513         if ((path=="/SID2") && (request=="POST"))
514                 return new eXMLRPCResponse(conn);
515         return 0;
516 }
517
518 #endif //DISABLE_NETWORK