1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-xmlrpc.c: XML-RPC parser/generator
*
* Copyright (C) 2007 Red Hat, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <time.h>
#include <libxml/tree.h>
#include "soup-xmlrpc.h"
#include "soup-value-utils.h"
#include "soup-date.h"
#include "soup-message.h"
#include "soup-misc.h"
#include "soup-session.h"
/**
* SECTION:soup-xmlrpc
* @short_description: XML-RPC support
*
**/
static xmlNode *find_real_node (xmlNode *node);
static gboolean insert_value (xmlNode *parent, GValue *value);
static void
insert_member (gpointer name, gpointer value, gpointer data)
{
xmlNode *member, **struct_node = data;
if (!*struct_node)
return;
member = xmlNewChild (*struct_node, NULL,
(const xmlChar *)"member", NULL);
xmlNewTextChild (member, NULL,
(const xmlChar *)"name", (const xmlChar *)name);
if (!insert_value (member, value)) {
xmlFreeNode (*struct_node);
*struct_node = NULL;
}
}
static gboolean
insert_value (xmlNode *parent, GValue *value)
{
GType type = G_VALUE_TYPE (value);
xmlNode *xvalue;
char buf[128];
xvalue = xmlNewChild (parent, NULL, (const xmlChar *)"value", NULL);
if (type == G_TYPE_INT) {
snprintf (buf, sizeof (buf), "%d", g_value_get_int (value));
xmlNewChild (xvalue, NULL,
(const xmlChar *)"int",
(const xmlChar *)buf);
} else if (type == G_TYPE_BOOLEAN) {
snprintf (buf, sizeof (buf), "%d", g_value_get_boolean (value));
xmlNewChild (xvalue, NULL,
(const xmlChar *)"boolean",
(const xmlChar *)buf);
} else if (type == G_TYPE_STRING) {
xmlNewTextChild (xvalue, NULL,
(const xmlChar *)"string",
(const xmlChar *)g_value_get_string (value));
} else if (type == G_TYPE_DOUBLE) {
g_ascii_dtostr (buf, sizeof (buf), g_value_get_double (value));
xmlNewChild (xvalue, NULL,
(const xmlChar *)"double",
(const xmlChar *)buf);
} else if (type == SOUP_TYPE_DATE) {
SoupDate *date = g_value_get_boxed (value);
char *timestamp = soup_date_to_string (date, SOUP_DATE_ISO8601_XMLRPC);
xmlNewChild (xvalue, NULL,
(const xmlChar *)"dateTime.iso8601",
(const xmlChar *)timestamp);
g_free (timestamp);
} else if (type == SOUP_TYPE_BYTE_ARRAY) {
GByteArray *ba = g_value_get_boxed (value);
char *encoded;
encoded = g_base64_encode (ba->data, ba->len);
xmlNewChild (xvalue, NULL,
(const xmlChar *)"base64",
(const xmlChar *)encoded);
g_free (encoded);
} else if (type == G_TYPE_HASH_TABLE) {
GHashTable *hash = g_value_get_boxed (value);
xmlNode *struct_node;
struct_node = xmlNewChild (xvalue, NULL,
(const xmlChar *)"struct", NULL);
g_hash_table_foreach (hash, insert_member, &struct_node);
if (!struct_node)
return FALSE;
} else if (type == G_TYPE_VALUE_ARRAY) {
GValueArray *va = g_value_get_boxed (value);
xmlNode *node;
int i;
node = xmlNewChild (xvalue, NULL,
(const xmlChar *)"array", NULL);
node = xmlNewChild (node, NULL,
(const xmlChar *)"data", NULL);
for (i = 0; i < va->n_values; i++) {
if (!insert_value (node, &va->values[i]))
return FALSE;
}
} else
return FALSE;
return TRUE;
}
/**
* soup_xmlrpc_build_method_call:
* @method_name: the name of the XML-RPC method
* @params: (array length=n_params): arguments to @method
* @n_params: length of @params
*
* This creates an XML-RPC methodCall and returns it as a string.
* This is the low-level method that soup_xmlrpc_request_new() is
* built on.
*
* @params is an array of #GValue representing the parameters to
* @method. (It is *not* a #GValueArray, although if you have a
* #GValueArray, you can just pass its <literal>values</literal>f and
* <literal>n_values</literal> fields.)
*
* The correspondence between glib types and XML-RPC types is:
*
* int: #int (%G_TYPE_INT)
* boolean: #gboolean (%G_TYPE_BOOLEAN)
* string: #char* (%G_TYPE_STRING)
* double: #double (%G_TYPE_DOUBLE)
* datetime.iso8601: #SoupDate (%SOUP_TYPE_DATE)
* base64: #GByteArray (%SOUP_TYPE_BYTE_ARRAY)
* struct: #GHashTable (%G_TYPE_HASH_TABLE)
* array: #GValueArray (%G_TYPE_VALUE_ARRAY)
*
* For structs, use a #GHashTable that maps strings to #GValue;
* soup_value_hash_new() and related methods can help with this.
*
* Return value: the text of the methodCall, or %NULL on error
**/
char *
soup_xmlrpc_build_method_call (const char *method_name,
GValue *params, int n_params)
{
xmlDoc *doc;
xmlNode *node, *param;
xmlChar *xmlbody;
int i, len;
char *body;
doc = xmlNewDoc ((const xmlChar *)"1.0");
doc->standalone = FALSE;
doc->encoding = xmlCharStrdup ("UTF-8");
node = xmlNewDocNode (doc, NULL, (const xmlChar *)"methodCall", NULL);
xmlDocSetRootElement (doc, node);
xmlNewChild (node, NULL, (const xmlChar *)"methodName",
(const xmlChar *)method_name);
node = xmlNewChild (node, NULL, (const xmlChar *)"params", NULL);
for (i = 0; i < n_params; i++) {
param = xmlNewChild (node, NULL,
(const xmlChar *)"param", NULL);
if (!insert_value (param, ¶ms[i])) {
xmlFreeDoc (doc);
return NULL;
}
}
xmlDocDumpMemory (doc, &xmlbody, &len);
body = g_strndup ((char *)xmlbody, len);
xmlFree (xmlbody);
xmlFreeDoc (doc);
return body;
}
static SoupMessage *
soup_xmlrpc_request_newv (const char *uri, const char *method_name, va_list args)
{
SoupMessage *msg;
GValueArray *params;
char *body;
params = soup_value_array_from_args (args);
if (!params)
return NULL;
body = soup_xmlrpc_build_method_call (method_name, params->values,
params->n_values);
g_value_array_free (params);
if (!body)
return NULL;
msg = soup_message_new ("POST", uri);
soup_message_set_request (msg, "text/xml", SOUP_MEMORY_TAKE,
body, strlen (body));
return msg;
}
/**
* soup_xmlrpc_request_new:
* @uri: URI of the XML-RPC service
* @method_name: the name of the XML-RPC method to invoke at @uri
* @...: parameters for @method
*
* Creates an XML-RPC methodCall and returns a #SoupMessage, ready
* to send, for that method call.
*
* The parameters are passed as type/value pairs; ie, first a #GType,
* and then a value of the appropriate type, finally terminated by
* %G_TYPE_INVALID.
*
* Return value: (transfer full): a #SoupMessage encoding the
* indicated XML-RPC request.
**/
SoupMessage *
soup_xmlrpc_request_new (const char *uri, const char *method_name, ...)
{
SoupMessage *msg;
va_list args;
va_start (args, method_name);
msg = soup_xmlrpc_request_newv (uri, method_name, args);
va_end (args);
return msg;
}
/**
* soup_xmlrpc_build_method_response:
* @value: the return value
*
* This creates a (successful) XML-RPC methodResponse and returns it
* as a string. To create a fault response, use
* soup_xmlrpc_build_fault().
*
* The glib type to XML-RPC type mapping is as with
* soup_xmlrpc_build_method_call(), qv.
*
* Return value: the text of the methodResponse, or %NULL on error
**/
char *
soup_xmlrpc_build_method_response (GValue *value)
{
xmlDoc *doc;
xmlNode *node;
xmlChar *xmlbody;
char *body;
int len;
doc = xmlNewDoc ((const xmlChar *)"1.0");
doc->standalone = FALSE;
doc->encoding = xmlCharStrdup ("UTF-8");
node = xmlNewDocNode (doc, NULL,
(const xmlChar *)"methodResponse", NULL);
xmlDocSetRootElement (doc, node);
node = xmlNewChild (node, NULL, (const xmlChar *)"params", NULL);
node = xmlNewChild (node, NULL, (const xmlChar *)"param", NULL);
if (!insert_value (node, value)) {
xmlFreeDoc (doc);
return NULL;
}
xmlDocDumpMemory (doc, &xmlbody, &len);
body = g_strndup ((char *)xmlbody, len);
xmlFree (xmlbody);
xmlFreeDoc (doc);
return body;
}
static char *
soup_xmlrpc_build_faultv (int fault_code, const char *fault_format, va_list args)
{
xmlDoc *doc;
xmlNode *node, *member;
GValue value;
xmlChar *xmlbody;
char *fault_string, *body;
int len;
fault_string = g_strdup_vprintf (fault_format, args);
doc = xmlNewDoc ((const xmlChar *)"1.0");
doc->standalone = FALSE;
doc->encoding = xmlCharStrdup ("UTF-8");
node = xmlNewDocNode (doc, NULL,
(const xmlChar *)"methodResponse", NULL);
xmlDocSetRootElement (doc, node);
node = xmlNewChild (node, NULL, (const xmlChar *)"fault", NULL);
node = xmlNewChild (node, NULL, (const xmlChar *)"value", NULL);
node = xmlNewChild (node, NULL, (const xmlChar *)"struct", NULL);
memset (&value, 0, sizeof (value));
member = xmlNewChild (node, NULL, (const xmlChar *)"member", NULL);
xmlNewChild (member, NULL,
(const xmlChar *)"name", (const xmlChar *)"faultCode");
g_value_init (&value, G_TYPE_INT);
g_value_set_int (&value, fault_code);
insert_value (member, &value);
g_value_unset (&value);
member = xmlNewChild (node, NULL, (const xmlChar *)"member", NULL);
xmlNewChild (member, NULL,
(const xmlChar *)"name", (const xmlChar *)"faultString");
g_value_init (&value, G_TYPE_STRING);
g_value_take_string (&value, fault_string);
insert_value (member, &value);
g_value_unset (&value);
xmlDocDumpMemory (doc, &xmlbody, &len);
body = g_strndup ((char *)xmlbody, len);
xmlFree (xmlbody);
xmlFreeDoc (doc);
return body;
}
/**
* soup_xmlrpc_build_fault:
* @fault_code: the fault code
* @fault_format: a printf()-style format string
* @...: the parameters to @fault_format
*
* This creates an XML-RPC fault response and returns it as a string.
* (To create a successful response, use
* soup_xmlrpc_build_method_response().)
*
* Return value: the text of the fault
**/
char *
soup_xmlrpc_build_fault (int fault_code, const char *fault_format, ...)
{
va_list args;
char *body;
va_start (args, fault_format);
body = soup_xmlrpc_build_faultv (fault_code, fault_format, args);
va_end (args);
return body;
}
/**
* soup_xmlrpc_set_response:
* @msg: an XML-RPC request
* @type: the type of the response value
* @...: the response value
*
* Sets the status code and response body of @msg to indicate a
* successful XML-RPC call, with a return value given by @type and the
* following varargs argument, of the type indicated by @type.
**/
void
soup_xmlrpc_set_response (SoupMessage *msg, GType type, ...)
{
va_list args;
GValue value;
char *body;
va_start (args, type);
SOUP_VALUE_SETV (&value, type, args);
va_end (args);
body = soup_xmlrpc_build_method_response (&value);
g_value_unset (&value);
soup_message_set_status (msg, SOUP_STATUS_OK);
soup_message_set_response (msg, "text/xml", SOUP_MEMORY_TAKE,
body, strlen (body));
}
/**
* soup_xmlrpc_set_fault:
* @msg: an XML-RPC request
* @fault_code: the fault code
* @fault_format: a printf()-style format string
* @...: the parameters to @fault_format
*
* Sets the status code and response body of @msg to indicate an
* unsuccessful XML-RPC call, with the error described by @fault_code
* and @fault_format.
**/
void
soup_xmlrpc_set_fault (SoupMessage *msg, int fault_code,
const char *fault_format, ...)
{
va_list args;
char *body;
va_start (args, fault_format);
body = soup_xmlrpc_build_faultv (fault_code, fault_format, args);
va_end (args);
soup_message_set_status (msg, SOUP_STATUS_OK);
soup_message_set_response (msg, "text/xml", SOUP_MEMORY_TAKE,
body, strlen (body));
}
static gboolean
parse_value (xmlNode *xmlvalue, GValue *value)
{
xmlNode *typenode;
const char *typename;
xmlChar *content;
memset (value, 0, sizeof (GValue));
typenode = find_real_node (xmlvalue->children);
if (!typenode) {
/* If no type node, it's a string */
content = xmlNodeGetContent (typenode);
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, (char *)content);
xmlFree (content);
return TRUE;
}
typename = (const char *)typenode->name;
if (!strcmp (typename, "i4") || !strcmp (typename, "int")) {
content = xmlNodeGetContent (typenode);
g_value_init (value, G_TYPE_INT);
g_value_set_int (value, atoi ((char *)content));
xmlFree (content);
} else if (!strcmp (typename, "boolean")) {
content = xmlNodeGetContent (typenode);
g_value_init (value, G_TYPE_BOOLEAN);
g_value_set_boolean (value, atoi ((char *)content));
xmlFree (content);
} else if (!strcmp (typename, "string")) {
content = xmlNodeGetContent (typenode);
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, (char *)content);
xmlFree (content);
} else if (!strcmp (typename, "double")) {
content = xmlNodeGetContent (typenode);
g_value_init (value, G_TYPE_DOUBLE);
g_value_set_double (value, g_ascii_strtod ((char *)content, NULL));
xmlFree (content);
} else if (!strcmp (typename, "dateTime.iso8601")) {
content = xmlNodeGetContent (typenode);
g_value_init (value, SOUP_TYPE_DATE);
g_value_take_boxed (value, soup_date_new_from_string ((char *)content));
xmlFree (content);
} else if (!strcmp (typename, "base64")) {
GByteArray *ba;
guchar *decoded;
gsize len;
content = xmlNodeGetContent (typenode);
decoded = g_base64_decode ((char *)content, &len);
ba = g_byte_array_sized_new (len);
g_byte_array_append (ba, decoded, len);
g_free (decoded);
xmlFree (content);
g_value_init (value, SOUP_TYPE_BYTE_ARRAY);
g_value_take_boxed (value, ba);
} else if (!strcmp (typename, "struct")) {
xmlNode *member, *child, *mname, *mxval;
GHashTable *hash;
GValue mgval;
hash = soup_value_hash_new ();
for (member = find_real_node (typenode->children);
member;
member = find_real_node (member->next)) {
if (strcmp ((const char *)member->name, "member") != 0) {
g_hash_table_destroy (hash);
return FALSE;
}
mname = mxval = NULL;
memset (&mgval, 0, sizeof (mgval));
for (child = find_real_node (member->children);
child;
child = find_real_node (child->next)) {
if (!strcmp ((const char *)child->name, "name"))
mname = child;
else if (!strcmp ((const char *)child->name, "value"))
mxval = child;
else
break;
}
if (!mname || !mxval || !parse_value (mxval, &mgval)) {
g_hash_table_destroy (hash);
return FALSE;
}
content = xmlNodeGetContent (mname);
soup_value_hash_insert_value (hash, (char *)content, &mgval);
xmlFree (content);
g_value_unset (&mgval);
}
g_value_init (value, G_TYPE_HASH_TABLE);
g_value_take_boxed (value, hash);
} else if (!strcmp (typename, "array")) {
xmlNode *data, *xval;
GValueArray *array;
GValue gval;
data = find_real_node (typenode->children);
if (!data || strcmp ((const char *)data->name, "data") != 0)
return FALSE;
array = g_value_array_new (1);
for (xval = find_real_node (data->children);
xval;
xval = find_real_node (xval->next)) {
memset (&gval, 0, sizeof (gval));
if (strcmp ((const char *)xval->name, "value") != 0 ||
!parse_value (xval, &gval)) {
g_value_array_free (array);
return FALSE;
}
g_value_array_append (array, &gval);
g_value_unset (&gval);
}
g_value_init (value, G_TYPE_VALUE_ARRAY);
g_value_take_boxed (value, array);
} else
return FALSE;
return TRUE;
}
/**
* soup_xmlrpc_parse_method_call:
* @method_call: the XML-RPC methodCall string
* @length: the length of @method_call, or -1 if it is NUL-terminated
* @method_name: (out): on return, the methodName from @method_call
* @params: (out): on return, the parameters from @method_call
*
* Parses @method_call to get the name and parameters, and returns the
* parameter values in a #GValueArray; see also
* soup_xmlrpc_extract_method_call(), which is more convenient if you
* know in advance what the types of the parameters will be.
*
* Return value: success or failure.
**/
gboolean
soup_xmlrpc_parse_method_call (const char *method_call, int length,
char **method_name, GValueArray **params)
{
xmlDoc *doc;
xmlNode *node, *param, *xval;
xmlChar *xmlMethodName = NULL;
gboolean success = FALSE;
GValue value;
doc = xmlParseMemory (method_call,
length == -1 ? strlen (method_call) : length);
if (!doc)
return FALSE;
node = xmlDocGetRootElement (doc);
if (!node || strcmp ((const char *)node->name, "methodCall") != 0)
goto fail;
node = find_real_node (node->children);
if (!node || strcmp ((const char *)node->name, "methodName") != 0)
goto fail;
xmlMethodName = xmlNodeGetContent (node);
node = find_real_node (node->next);
if (!node || strcmp ((const char *)node->name, "params") != 0)
goto fail;
*params = g_value_array_new (1);
param = find_real_node (node->children);
while (param && !strcmp ((const char *)param->name, "param")) {
xval = find_real_node (param->children);
if (!xval || strcmp ((const char *)xval->name, "value") != 0 ||
!parse_value (xval, &value)) {
g_value_array_free (*params);
goto fail;
}
g_value_array_append (*params, &value);
g_value_unset (&value);
param = find_real_node (param->next);
}
success = TRUE;
*method_name = g_strdup ((char *)xmlMethodName);
fail:
xmlFreeDoc (doc);
if (xmlMethodName)
xmlFree (xmlMethodName);
return success;
}
/**
* soup_xmlrpc_extract_method_call:
* @method_call: the XML-RPC methodCall string
* @length: the length of @method_call, or -1 if it is NUL-terminated
* @method_name: (out): on return, the methodName from @method_call
* @...: return types and locations for parameters
*
* Parses @method_call to get the name and parameters, and puts
* the parameters into variables of the appropriate types.
*
* The parameters are handled similarly to
* @soup_xmlrpc_build_method_call, with pairs of types and values,
* terminated by %G_TYPE_INVALID, except that values are pointers to
* variables of the indicated type, rather than values of the type.
*
* See also soup_xmlrpc_parse_method_call(), which can be used if
* you don't know the types of the parameters.
*
* Return value: success or failure.
**/
gboolean
soup_xmlrpc_extract_method_call (const char *method_call, int length,
char **method_name, ...)
{
GValueArray *params;
gboolean success;
va_list args;
if (!soup_xmlrpc_parse_method_call (method_call, length,
method_name, ¶ms))
return FALSE;
va_start (args, method_name);
success = soup_value_array_to_args (params, args);
va_end (args);
g_value_array_free (params);
return success;
}
/**
* soup_xmlrpc_parse_method_response:
* @method_response: the XML-RPC methodResponse string
* @length: the length of @method_response, or -1 if it is NUL-terminated
* @value: (out): on return, the return value from @method_call
* @error: error return value
*
* Parses @method_response and returns the return value in @value. If
* @method_response is a fault, @value will be unchanged, and @error
* will be set to an error of type %SOUP_XMLRPC_FAULT, with the error
* #code containing the fault code, and the error #message containing
* the fault string. (If @method_response cannot be parsed at all,
* soup_xmlrpc_parse_method_response() will return %FALSE, but @error
* will be unset.)
*
* Return value: %TRUE if a return value was parsed, %FALSE if the
* response could not be parsed, or contained a fault.
**/
gboolean
soup_xmlrpc_parse_method_response (const char *method_response, int length,
GValue *value, GError **error)
{
xmlDoc *doc;
xmlNode *node;
gboolean success = FALSE;
doc = xmlParseMemory (method_response,
length == -1 ? strlen (method_response) : length);
if (!doc)
return FALSE;
node = xmlDocGetRootElement (doc);
if (!node || strcmp ((const char *)node->name, "methodResponse") != 0)
goto fail;
node = find_real_node (node->children);
if (!node)
goto fail;
if (!strcmp ((const char *)node->name, "fault") && error) {
int fault_code;
char *fault_string;
GValue fault_val;
GHashTable *fault_hash;
node = find_real_node (node->children);
if (!node || strcmp ((const char *)node->name, "value") != 0)
goto fail;
if (!parse_value (node, &fault_val))
goto fail;
if (!G_VALUE_HOLDS (&fault_val, G_TYPE_HASH_TABLE)) {
g_value_unset (&fault_val);
goto fail;
}
fault_hash = g_value_get_boxed (&fault_val);
if (!soup_value_hash_lookup (fault_hash, "faultCode",
G_TYPE_INT, &fault_code) ||
!soup_value_hash_lookup (fault_hash, "faultString",
G_TYPE_STRING, &fault_string)) {
g_value_unset (&fault_val);
goto fail;
}
g_set_error (error, SOUP_XMLRPC_FAULT,
fault_code, "%s", fault_string);
g_value_unset (&fault_val);
} else if (!strcmp ((const char *)node->name, "params")) {
node = find_real_node (node->children);
if (!node || strcmp ((const char *)node->name, "param") != 0)
goto fail;
node = find_real_node (node->children);
if (!node || strcmp ((const char *)node->name, "value") != 0)
goto fail;
if (!parse_value (node, value))
goto fail;
success = TRUE;
}
fail:
xmlFreeDoc (doc);
return success;
}
/**
* soup_xmlrpc_extract_method_response:
* @method_response: the XML-RPC methodResponse string
* @length: the length of @method_response, or -1 if it is NUL-terminated
* @error: error return value
* @type: the expected type of the return value
* @...: location for return value
*
* Parses @method_response and extracts the return value into
* a variable of the correct type.
*
* If @method_response is a fault, the return value will be unset,
* and @error will be set to an error of type %SOUP_XMLRPC_FAULT, with
* the error #code containing the fault code, and the error #message
* containing the fault string. (If @method_response cannot be parsed
* at all, soup_xmlrpc_extract_method_response() will return %FALSE,
* but @error will be unset.)
*
* Return value: %TRUE if a return value was parsed, %FALSE if the
* response was of the wrong type, or contained a fault.
**/
gboolean
soup_xmlrpc_extract_method_response (const char *method_response, int length,
GError **error, GType type, ...)
{
GValue value;
va_list args;
if (!soup_xmlrpc_parse_method_response (method_response, length,
&value, error))
return FALSE;
if (!G_VALUE_HOLDS (&value, type))
return FALSE;
va_start (args, type);
SOUP_VALUE_GETV (&value, type, args);
va_end (args);
return TRUE;
}
GQuark
soup_xmlrpc_error_quark (void)
{
static GQuark error;
if (!error)
error = g_quark_from_static_string ("soup_xmlrpc_error_quark");
return error;
}
/**
* SOUP_XMLRPC_FAULT:
*
* A #GError domain representing an XML-RPC fault code. Used with
* #SoupXMLRPCFault (although servers may also return fault codes not
* in that enumeration).
*/
/**
* SoupXMLRPCFault:
* @SOUP_XMLRPC_FAULT_PARSE_ERROR_NOT_WELL_FORMED: request was not
* well-formed
* @SOUP_XMLRPC_FAULT_PARSE_ERROR_UNSUPPORTED_ENCODING: request was in
* an unsupported encoding
* @SOUP_XMLRPC_FAULT_PARSE_ERROR_INVALID_CHARACTER_FOR_ENCODING:
* request contained an invalid character
* @SOUP_XMLRPC_FAULT_SERVER_ERROR_INVALID_XML_RPC: request was not
* valid XML-RPC
* @SOUP_XMLRPC_FAULT_SERVER_ERROR_REQUESTED_METHOD_NOT_FOUND: method
* not found
* @SOUP_XMLRPC_FAULT_SERVER_ERROR_INVALID_METHOD_PARAMETERS: invalid
* parameters
* @SOUP_XMLRPC_FAULT_SERVER_ERROR_INTERNAL_XML_RPC_ERROR: internal
* error
* @SOUP_XMLRPC_FAULT_APPLICATION_ERROR: start of reserved range for
* application error codes
* @SOUP_XMLRPC_FAULT_SYSTEM_ERROR: start of reserved range for
* system error codes
* @SOUP_XMLRPC_FAULT_TRANSPORT_ERROR: start of reserved range for
* transport error codes
*
* Pre-defined XML-RPC fault codes from <ulink
* url="http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php">http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php</ulink>.
* These are an extension, not part of the XML-RPC spec; you can't
* assume servers will use them.
*/
GQuark
soup_xmlrpc_fault_quark (void)
{
static GQuark error;
if (!error)
error = g_quark_from_static_string ("soup_xmlrpc_fault_quark");
return error;
}
static xmlNode *
find_real_node (xmlNode *node)
{
while (node && (node->type == XML_COMMENT_NODE ||
xmlIsBlankNode (node)))
node = node->next;
return node;
}
|