forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlrpc.c
3009 lines (2715 loc) · 78.7 KB
/
xmlrpc.c
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of libXMLRPC - a C library for xml-encoded function calls.
Author: Dan Libby ([email protected])
Epinions.com may be contacted at [email protected]
*/
/*
Copyright 2000 Epinions, Inc.
Subject to the following 3 conditions, Epinions, Inc. permits you, free
of charge, to (a) use, copy, distribute, modify, perform and display this
software and associated documentation files (the "Software"), and (b)
permit others to whom the Software is furnished to do so as well.
1) The above copyright notice and this permission notice shall be included
without modification in all copies or substantial portions of the
Software.
2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE OR NONINFRINGEMENT.
3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH
DAMAGES.
*/
static const char rcsid[] = "#(@) $Id$";
/****h* ABOUT/xmlrpc
* NAME
* XMLRPC_VALUE
* AUTHOR
* Dan Libby, aka danda ([email protected])
* CREATION DATE
* 9/1999 - 10/2000
* HISTORY
* $Log$
* Revision 1.8.4.3.2.1 2008/09/10 00:07:44 felipe
* MFH:
* - Merged fix from SF project (Import Jeff Lawsons patches for XML datetime bug fixes)
* Fixed bugs:
* #45226 (xmlrpc_set_type() segfaults with valid ISO8601 date string)
* #18916 (xmlrpc_set_type() "not working")
*
* Revision 1.8.4.3 2007/09/18 19:49:53 iliaa
*
* Fixed bug #42189 (xmlrpc_set_type() crashes php on invalid datetime
* values).
*
* Revision 1.8.4.2 2007/06/07 09:07:36 tony2001
* MFH: php_localtime_r() checks
*
* Revision 1.8.4.1 2006/11/30 16:38:37 iliaa
* last set of zts fixes
*
* Revision 1.8 2005/03/28 00:07:24 edink
* Reshufle includes to make it compile on windows
*
* Revision 1.7 2005/03/26 03:13:58 sniper
* - Made it possible to build ext/xmlrpc with libxml2
*
* Revision 1.6 2004/04/27 17:33:59 iliaa
* Removed C++ style comments.
*
* Revision 1.5 2003/12/16 21:00:21 sniper
* Fix some compile warnings (patch by Joe Orton)
*
* Revision 1.4 2002/07/05 04:43:53 danda
* merged in updates from SF project. bring php repository up to date with xmlrpc-epi version 0.51
*
* Revision 1.22 2002/03/09 23:15:44 danda
* add fault interrogation funcs
*
* Revision 1.21 2002/03/09 22:27:41 danda
* win32 build patches contributed by Jeff Lawson
*
* Revision 1.20 2002/02/13 20:58:50 danda
* patch to make source more windows friendly, contributed by Jeff Lawson
*
* Revision 1.19 2001/10/12 23:25:54 danda
* default to writing xmlrpc
*
* Revision 1.18 2001/09/29 21:58:05 danda
* adding cvs log to history section
*
* 10/15/2000 -- danda -- adding robodoc documentation
* 08/2000 -- danda -- PHP C extension that uses XMLRPC
* 08/2000 -- danda -- support for two vocabularies: danda-rpc and xml-rpc
* 09/1999 -- danda -- Initial API, before I even knew of standard XMLRPC vocab. Response only.
* 07/2000 -- danda -- wrote new implementation to be compatible with xmlrpc standard and
* incorporated some ideas from ensor, most notably the separation of
* xml dom from xmlrpc api.
* 06/2000 -- danda -- played with expat-ensor from www.ensor.org. Cool, but some flaws.
* TODO
* PORTABILITY
* Coded on RedHat Linux 6.2. Builds on Solaris x86. Should build on just
* about anything with minor mods.
* NOTES
* Welcome to XMLRPC. For more info on the specification and history, see
* http://www.xmlrpc.org.
*
* This code aims to be a full-featured C implementation of XMLRPC. It does not
* have any networking code. Rather, it is intended to be plugged into apps
* or libraries with existing networking facilities, eg PHP, apache, perl, mozilla,
* home-brew application servers, etc.
*
* Usage Paradigm:
* The user of this library will typically be implementing either an XMLRPC server,
* an XMLRPC client, or both. The client will use the library to build an in-memory
* representation of a request, and then serialize (encode) that request into XML. The
* client will then send the XML to the server via external mechanism. The server will
* de-serialize the XML back into an binary representation, call the appropriate registered
* method -- thereby generating a response. The response will be serialized into XML and
* sent back to the client. The client will de-serialize it into memory, and can
* iterate through the results via API.
*
* Both the request and the response may consist of arbitrarily long, arbitrarily nested
* values. The values may be one of several types, as defined by XMLRPC_VALUE_TYPE.
*
* Features and Architecture:
* - The XML parsing (xml_element.c) is completely independent of the XMLRPC api. In fact,
* it can be used as a standalone dom implementation.
* - Because of this, the same XMLRPC data can be serialized into multiple xml vocabularies.
* It is simply a matter of writing a transport. So far, two transports have been defined.
* The default xmlrpc vocab (xml_to_xmlrpc.c), and simple-rpc (xml_to_dandarpc.c) which is
* proprietary, but imho more readable, and nice for proprietary legacy reasons.
* - Various output options, including: xml escaping via CDATA or entity, case folding,
* vocab version, and character encoding.
* - One to One mapping between C structures and actual values, unlike ensor which forces
* one to understand the arcana of the xmlrpc vocab.
* - support for mixed indexed/keyed vector types, making it more compatible with
* languages such as PHP.
* - quite speedy compared to implementations written in interpreted languages. Also, uses
* intelligent string handling, so not many strlen() calls, etc.
* - comprehensive API for manipulation of values
*******/
#include "ext/xml/expat_compat.h"
#include "main/php_reentrancy.h"
#ifdef _WIN32
#include "xmlrpc_win32.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <ctype.h>
#include "queue.h"
#include "xmlrpc.h"
#include "base64.h"
#include "xml_to_xmlrpc.h"
#include "xml_to_dandarpc.h"
#include "xml_to_soap.h"
#include "xml_element.h"
#include "xmlrpc_private.h"
#include "xmlrpc_introspection_private.h"
#include "system_methods_private.h"
/*-*********************
* Begin Time Functions *
***********************/
static time_t mkgmtime(struct tm *tm)
{
static const int mdays[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
return ((((((tm->tm_year - 70) * 365) + mdays[tm->tm_mon] + tm->tm_mday-1 +
(tm->tm_year-68-1+(tm->tm_mon>=2))/4) * 24) + tm->tm_hour) * 60 +
tm->tm_min) * 60 + tm->tm_sec;
}
static int date_from_ISO8601 (const char *text, time_t * value) {
struct tm tm;
int n;
int i;
char buf[30];
if (strchr (text, '-')) {
char *p = (char *) text, *p2 = buf;
while (p && *p) {
if (*p != '-') {
*p2 = *p;
p2++;
if (p2-buf >= sizeof(buf)) {
return -1;
}
}
p++;
}
text = buf;
}
tm.tm_isdst = -1;
#define XMLRPC_IS_NUMBER(x) if (x < '0' || x > '9') return -1;
n = 1000;
tm.tm_year = 0;
for(i = 0; i < 4; i++) {
XMLRPC_IS_NUMBER(text[i])
tm.tm_year += (text[i]-'0')*n;
n /= 10;
}
n = 10;
tm.tm_mon = 0;
for(i = 0; i < 2; i++) {
XMLRPC_IS_NUMBER(text[i+4])
tm.tm_mon += (text[i+4]-'0')*n;
n /= 10;
}
tm.tm_mon --;
if(tm.tm_mon < 0 || tm.tm_mon > 11) {
return -1;
}
n = 10;
tm.tm_mday = 0;
for(i = 0; i < 2; i++) {
XMLRPC_IS_NUMBER(text[i+6])
tm.tm_mday += (text[i+6]-'0')*n;
n /= 10;
}
n = 10;
tm.tm_hour = 0;
for(i = 0; i < 2; i++) {
XMLRPC_IS_NUMBER(text[i+9])
tm.tm_hour += (text[i+9]-'0')*n;
n /= 10;
}
n = 10;
tm.tm_min = 0;
for(i = 0; i < 2; i++) {
XMLRPC_IS_NUMBER(text[i+12])
tm.tm_min += (text[i+12]-'0')*n;
n /= 10;
}
n = 10;
tm.tm_sec = 0;
for(i = 0; i < 2; i++) {
XMLRPC_IS_NUMBER(text[i+15])
tm.tm_sec += (text[i+15]-'0')*n;
n /= 10;
}
tm.tm_year -= 1900;
*value = mkgmtime(&tm);
return 0;
}
static int date_to_ISO8601 (time_t value, char *buf, int length) {
struct tm *tm, tmbuf;
tm = php_gmtime_r(&value, &tmbuf);
if (!tm) {
return 0;
}
#if 0 /* TODO: soap seems to favor this method. xmlrpc the latter. */
return strftime (buf, length, "%Y-%m-%dT%H:%M:%SZ", tm);
#else
return strftime(buf, length, "%Y%m%dT%H:%M:%SZ", tm);
#endif
}
/*-*******************
* End Time Functions *
*********************/
/*-***************************
* Begin XMLRPC_REQUEST funcs *
*****************************/
/****f* REQUEST/XMLRPC_RequestNew
* NAME
* XMLRPC_RequestNew
* SYNOPSIS
* XMLRPC_REQUEST XMLRPC_RequestNew()
* FUNCTION
* Creates a new XMLRPC_Request data struct
* INPUTS
* none
* SEE ALSO
* XMLRPC_RequestFree ()
* SOURCE
*/
XMLRPC_REQUEST XMLRPC_RequestNew() {
XMLRPC_REQUEST xRequest = calloc(1, sizeof(STRUCT_XMLRPC_REQUEST));
if(xRequest) {
simplestring_init(&xRequest->methodName);
}
return xRequest;
}
/*******/
/****f* REQUEST/XMLRPC_RequestFree
* NAME
* XMLRPC_RequestFree
* SYNOPSIS
* void XMLRPC_RequestFree(XMLRPC_REQUEST request, int bFreeIO)
* FUNCTION
* Free XMLRPC Request and all sub-values
* INPUTS
* request -- previously allocated request struct
* bFreeIO -- 1 = also free request value data, if any, 0 = ignore.
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_CleanupValue ()
* SOURCE
*/
void XMLRPC_RequestFree(XMLRPC_REQUEST request, int bFreeIO) {
if(request) {
simplestring_free(&request->methodName);
if(request->io && bFreeIO) {
XMLRPC_CleanupValue(request->io);
}
if(request->error) {
XMLRPC_CleanupValue(request->error);
}
my_free(request);
}
}
/*******/
/* Set Method Name to call */
/****f* REQUEST/XMLRPC_RequestSetMethodName
* NAME
* XMLRPC_RequestSetMethodName
* SYNOPSIS
* const char* XMLRPC_RequestSetMethodName(XMLRPC_REQUEST request, const char* methodName)
* FUNCTION
* Set name of method to call with this request.
* INPUTS
* request -- previously allocated request struct
* methodName -- name of method
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_RequestGetMethodName ()
* XMLRPC_RequestFree ()
* SOURCE
*/
const char* XMLRPC_RequestSetMethodName(XMLRPC_REQUEST request, const char* methodName) {
if(request) {
simplestring_clear(&request->methodName);
simplestring_add(&request->methodName, methodName);
return request->methodName.str;
}
return NULL;
}
/*******/
/****f* REQUEST/XMLRPC_RequestGetMethodName
* NAME
* XMLRPC_RequestGetMethodName
* SYNOPSIS
* const char* XMLRPC_RequestGetMethodName(XMLRPC_REQUEST request)
* FUNCTION
* Get name of method called by this request
* INPUTS
* request -- previously allocated request struct
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_RequestSetMethodName ()
* XMLRPC_RequestFree ()
* SOURCE
*/
const char* XMLRPC_RequestGetMethodName(XMLRPC_REQUEST request) {
return request ? request->methodName.str : NULL;
}
/*******/
/****f* REQUEST/XMLRPC_RequestSetRequestType
* NAME
* XMLRPC_RequestSetRequestType
* SYNOPSIS
* XMLRPC_REQUEST_TYPE XMLRPC_RequestSetRequestType(XMLRPC_REQUEST request, XMLRPC_REQUEST_TYPE type)
* FUNCTION
* A request struct may be allocated by a caller or by xmlrpc
* in response to a request. This allows setting the
* request type.
* INPUTS
* request -- previously allocated request struct
* type -- request type [xmlrpc_method_call | xmlrpc_method_response]
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_RequestGetRequestType ()
* XMLRPC_RequestFree ()
* XMLRPC_REQUEST_TYPE
* SOURCE
*/
XMLRPC_REQUEST_TYPE XMLRPC_RequestSetRequestType (XMLRPC_REQUEST request,
XMLRPC_REQUEST_TYPE type) {
if(request) {
request->request_type = type;
return request->request_type;
}
return xmlrpc_request_none;
}
/*******/
/****f* REQUEST/XMLRPC_RequestGetRequestType
* NAME
* XMLRPC_RequestGetRequestType
* SYNOPSIS
* XMLRPC_REQUEST_TYPE XMLRPC_RequestGetRequestType(XMLRPC_REQUEST request)
* FUNCTION
* A request struct may be allocated by a caller or by xmlrpc
* in response to a request. This allows setting the
* request type.
* INPUTS
* request -- previously allocated request struct
* RESULT
* type -- request type [xmlrpc_method_call | xmlrpc_method_response]
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_RequestSetRequestType ()
* XMLRPC_RequestFree ()
* XMLRPC_REQUEST_TYPE
* SOURCE
*/
XMLRPC_REQUEST_TYPE XMLRPC_RequestGetRequestType(XMLRPC_REQUEST request) {
return request ? request->request_type : xmlrpc_request_none;
}
/*******/
/****f* REQUEST/XMLRPC_RequestSetData
* NAME
* XMLRPC_RequestSetData
* SYNOPSIS
* XMLRPC_VALUE XMLRPC_RequestSetData(XMLRPC_REQUEST request, XMLRPC_VALUE data)
* FUNCTION
* Associates a block of xmlrpc data with the request. The
* data is *not* copied. A pointer is kept. The caller
* should be careful not to doubly free the data value,
* which may optionally be free'd by XMLRPC_RequestFree().
* INPUTS
* request -- previously allocated request struct
* data -- previously allocated data struct
* RESULT
* XMLRPC_VALUE -- pointer to value stored, or NULL
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_RequestGetData ()
* XMLRPC_RequestFree ()
* XMLRPC_REQUEST
* XMLRPC_VALUE
* SOURCE
*/
XMLRPC_VALUE XMLRPC_RequestSetData(XMLRPC_REQUEST request, XMLRPC_VALUE data) {
if(request && data) {
if (request->io) {
XMLRPC_CleanupValue (request->io);
}
request->io = XMLRPC_CopyValue(data);
return request->io;
}
return NULL;
}
/*******/
/****f* REQUEST/XMLRPC_RequestGetData
* NAME
* XMLRPC_RequestGetData
* SYNOPSIS
* XMLRPC_VALUE XMLRPC_RequestGetData(XMLRPC_REQUEST request)
* FUNCTION
* Returns data associated with request, if any.
* INPUTS
* request -- previously allocated request struct
* RESULT
* XMLRPC_VALUE -- pointer to value stored, or NULL
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_RequestSetData ()
* XMLRPC_RequestFree ()
* XMLRPC_REQUEST
* XMLRPC_VALUE
* SOURCE
*/
XMLRPC_VALUE XMLRPC_RequestGetData(XMLRPC_REQUEST request) {
return request ? request->io : NULL;
}
/*******/
/****f* REQUEST/XMLRPC_RequestSetError
* NAME
* XMLRPC_RequestSetError
* SYNOPSIS
* XMLRPC_VALUE XMLRPC_RequestSetError(XMLRPC_REQUEST request, XMLRPC_VALUE error)
* FUNCTION
* Associates a block of xmlrpc data, representing an error
* condition, with the request.
* INPUTS
* request -- previously allocated request struct
* error -- previously allocated error code or struct
* RESULT
* XMLRPC_VALUE -- pointer to value stored, or NULL
* NOTES
* This is a private function for usage by internals only.
* SEE ALSO
* XMLRPC_RequestGetError ()
* SOURCE
*/
XMLRPC_VALUE XMLRPC_RequestSetError (XMLRPC_REQUEST request, XMLRPC_VALUE error) {
if (request && error) {
if (request->error) {
XMLRPC_CleanupValue (request->error);
}
request->error = XMLRPC_CopyValue (error);
return request->error;
}
return NULL;
}
/*******/
/****f* REQUEST/XMLRPC_RequestGetError
* NAME
* XMLRPC_RequestGetError
* SYNOPSIS
* XMLRPC_VALUE XMLRPC_RequestGetError(XMLRPC_REQUEST request)
* FUNCTION
* Returns error data associated with request, if any.
* INPUTS
* request -- previously allocated request struct
* RESULT
* XMLRPC_VALUE -- pointer to error value stored, or NULL
* NOTES
* This is a private function for usage by internals only.
* SEE ALSO
* XMLRPC_RequestSetError ()
* XMLRPC_RequestFree ()
* SOURCE
*/
XMLRPC_VALUE XMLRPC_RequestGetError (XMLRPC_REQUEST request) {
return request ? request->error : NULL;
}
/*******/
/****f* REQUEST/XMLRPC_RequestSetOutputOptions
* NAME
* XMLRPC_RequestSetOutputOptions
* SYNOPSIS
* XMLRPC_REQUEST_OUTPUT_OPTIONS XMLRPC_RequestSetOutputOptions(XMLRPC_REQUEST request, XMLRPC_REQUEST_OUTPUT_OPTIONS output)
* FUNCTION
* Sets output options used for generating XML. The output struct
* is copied, and may be freed by the caller.
* INPUTS
* request -- previously allocated request struct
* output -- output options struct initialized by caller
* RESULT
* XMLRPC_REQUEST_OUTPUT_OPTIONS -- pointer to value stored, or NULL
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_RequestGetOutputOptions ()
* XMLRPC_RequestFree ()
* XMLRPC_REQUEST
* XMLRPC_REQUEST_OUTPUT_OPTIONS
* SOURCE
*/
XMLRPC_REQUEST_OUTPUT_OPTIONS XMLRPC_RequestSetOutputOptions(XMLRPC_REQUEST request, XMLRPC_REQUEST_OUTPUT_OPTIONS output) {
if(request && output) {
memcpy (&request->output, output,
sizeof (STRUCT_XMLRPC_REQUEST_OUTPUT_OPTIONS));
return &request->output;
}
return NULL;
}
/*******/
/****f* REQUEST/XMLRPC_RequestGetOutputOptions
* NAME
* XMLRPC_RequestGetOutputOptions
* SYNOPSIS
* XMLRPC_REQUEST_OUTPUT_OPTIONS XMLRPC_RequestGetOutputOptions(XMLRPC_REQUEST request)
* FUNCTION
* Gets a pointer to output options used for generating XML.
* INPUTS
* request -- previously allocated request struct
* RESULT
* XMLRPC_REQUEST_OUTPUT_OPTIONS -- pointer to options stored, or NULL
* SEE ALSO
* XMLRPC_RequestNew ()
* XMLRPC_RequestSetOutputOptions ()
* XMLRPC_RequestFree ()
* XMLRPC_REQUEST
* XMLRPC_REQUEST_OUTPUT_OPTIONS
* SOURCE
*/
XMLRPC_REQUEST_OUTPUT_OPTIONS XMLRPC_RequestGetOutputOptions(XMLRPC_REQUEST request) {
return request ? &request->output : NULL;
}
/*******/
/*-*************************
* End XMLRPC_REQUEST funcs *
***************************/
/*-***************************
* Begin Serializiation funcs *
*****************************/
/****f* SERIALIZE/XMLRPC_VALUE_ToXML
* NAME
* XMLRPC_VALUE_ToXML
* SYNOPSIS
* char* XMLRPC_VALUE_ToXML(XMLRPC_VALUE val)
* FUNCTION
* encode XMLRPC_VALUE into XML buffer. Note that the generated
* buffer will not contain a methodCall.
* INPUTS
* val -- previously allocated XMLRPC_VALUE
* buf_len -- length of returned buffer, if not null
* RESULT
* char* -- newly allocated buffer containing XML.
* It is the caller's responsibility to free it.
* SEE ALSO
* XMLRPC_REQUEST_ToXML ()
* XMLRPC_VALUE_FromXML ()
* XMLRPC_Free ()
* XMLRPC_VALUE
* SOURCE
*/
char* XMLRPC_VALUE_ToXML(XMLRPC_VALUE val, int* buf_len) {
xml_element *root_elem = XMLRPC_VALUE_to_xml_element(val);
char* pRet = NULL;
if(root_elem) {
pRet = xml_elem_serialize_to_string(root_elem, NULL, buf_len);
xml_elem_free(root_elem);
}
return pRet;
}
/*******/
/****f* SERIALIZE/XMLRPC_REQUEST_ToXML
* NAME
* XMLRPC_REQUEST_ToXML
* SYNOPSIS
* char* XMLRPC_REQUEST_ToXML(XMLRPC_REQUEST request)
* FUNCTION
* encode XMLRPC_REQUEST into XML buffer
* INPUTS
* request -- previously allocated XMLRPC_REQUEST
* buf_len -- size of returned buf, if not null
* RESULT
* char* -- newly allocated buffer containing XML.
* It is the caller's responsibility to free it.
* SEE ALSO
* XMLRPC_REQUEST_ToXML ()
* XMLRPC_REQUEST_FromXML ()
* XMLRPC_Free ()
* XMLRPC_VALUE_ToXML ()
* XMLRPC_REQUEST
* SOURCE
*/
char* XMLRPC_REQUEST_ToXML(XMLRPC_REQUEST request, int* buf_len) {
char* pRet = NULL;
if (request) {
xml_element *root_elem = NULL;
if (request->output.version == xmlrpc_version_simple) {
root_elem = DANDARPC_REQUEST_to_xml_element (request);
}
else if (request->output.version == xmlrpc_version_1_0 ||
request->output.version == xmlrpc_version_none) {
root_elem = XMLRPC_REQUEST_to_xml_element (request);
}
else if (request->output.version == xmlrpc_version_soap_1_1) {
root_elem = SOAP_REQUEST_to_xml_element (request);
}
if(root_elem) {
pRet =
xml_elem_serialize_to_string (root_elem,
&request->output.xml_elem_opts,
buf_len);
xml_elem_free(root_elem);
}
}
return pRet;
}
/*******/
/****f* SERIALIZE/XMLRPC_VALUE_FromXML
* NAME
* XMLRPC_VALUE_FromXML
* SYNOPSIS
* XMLRPC_VALUE XMLRPC_VALUE_FromXML(const char* in_buf, int le
* FUNCTION
* Retrieve XMLRPC_VALUE from XML buffer. Note that this will
* ignore any methodCall. See XMLRPC_REQUEST_FromXML
* INPUTS
* in_buf -- character buffer containing XML
* len -- length of buffer
* RESULT
* XMLRPC_VALUE -- newly allocated data, or NULL if error. Should
* be free'd by caller.
* SEE ALSO
* XMLRPC_VALUE_ToXML ()
* XMLRPC_REQUEST_FromXML ()
* XMLRPC_VALUE
* SOURCE
*/
XMLRPC_VALUE XMLRPC_VALUE_FromXML (const char *in_buf, int len, XMLRPC_REQUEST_INPUT_OPTIONS in_options) {
XMLRPC_VALUE xResponse = NULL;
XMLRPC_REQUEST req = XMLRPC_REQUEST_FromXML(in_buf, len, in_options);
if(req) {
xResponse = req->io;
XMLRPC_RequestFree(req, 0);
}
return xResponse;
}
/*******/
/* map parser errors to standard xml-rpc errors */
static XMLRPC_VALUE map_expat_errors(XML_ELEM_ERROR error) {
XMLRPC_VALUE xReturn = NULL;
if(error) {
XMLRPC_ERROR_CODE code;
char buf[1024];
snprintf(buf, sizeof(buf),
"error occurred at line %ld, column %ld, byte index %ld",
error->line, error->column, error->byte_index);
/* expat specific errors */
switch(error->parser_code) {
case XML_ERROR_UNKNOWN_ENCODING:
code = xmlrpc_error_parse_unknown_encoding;
break;
case XML_ERROR_INCORRECT_ENCODING:
code = xmlrpc_error_parse_bad_encoding;
break;
default:
code = xmlrpc_error_parse_xml_syntax;
break;
}
xReturn = XMLRPC_UtilityCreateFault(code, buf);
}
return xReturn;
}
/****f* SERIALIZE/XMLRPC_REQUEST_FromXML
* NAME
* XMLRPC_REQUEST_FromXML
* SYNOPSIS
* XMLRPC_REQUEST XMLRPC_REQUEST_FromXML(const char* in_buf, int le
* FUNCTION
* Retrieve XMLRPC_REQUEST from XML buffer
* INPUTS
* in_buf -- character buffer containing XML
* len -- length of buffer
* RESULT
* XMLRPC_REQUEST -- newly allocated data, or NULL if error. Should
* be free'd by caller.
* SEE ALSO
* XMLRPC_REQUEST_ToXML ()
* XMLRPC_VALUE_FromXML ()
* XMLRPC_REQUEST
* SOURCE
*/
XMLRPC_REQUEST XMLRPC_REQUEST_FromXML (const char *in_buf, int len,
XMLRPC_REQUEST_INPUT_OPTIONS in_options) {
XMLRPC_REQUEST request = XMLRPC_RequestNew();
STRUCT_XML_ELEM_ERROR error = {0};
if(request) {
xml_element *root_elem =
xml_elem_parse_buf (in_buf, len,
(in_options ? &in_options->xml_elem_opts : NULL),
&error);
if(root_elem) {
if(!strcmp(root_elem->name, "simpleRPC")) {
request->output.version = xmlrpc_version_simple;
xml_element_to_DANDARPC_REQUEST(request, root_elem);
}
else if (!strcmp (root_elem->name, "SOAP-ENV:Envelope")) {
request->output.version = xmlrpc_version_soap_1_1;
xml_element_to_SOAP_REQUEST (request, root_elem);
}
else {
request->output.version = xmlrpc_version_1_0;
xml_element_to_XMLRPC_REQUEST(request, root_elem);
}
xml_elem_free(root_elem);
}
else {
if(error.parser_error) {
XMLRPC_RequestSetError (request, map_expat_errors (&error));
}
}
}
return request;
}
/*******/
/*-************************
* End Serialization Funcs *
**************************/
/****f* VALUE/XMLRPC_CreateValueEmpty
* NAME
* XMLRPC_CreateValueEmpty
* SYNOPSIS
* XMLRPC_VALUE XMLRPC_CreateValueEmpty ()
* FUNCTION
* Create an XML value to be used/modified elsewhere.
* INPUTS
* RESULT
* XMLRPC_VALUE. The new value, or NULL on failure.
* SEE ALSO
* XMLRPC_CleanupValue ()
* XMLRPC_VALUE
* SOURCE
*/
XMLRPC_VALUE XMLRPC_CreateValueEmpty() {
XMLRPC_VALUE v = calloc(1, sizeof(STRUCT_XMLRPC_VALUE));
if(v) {
#ifdef XMLRPC_DEBUG_REFCOUNT
printf ("calloc'd 0x%x\n", v);
#endif
v->type = xmlrpc_empty;
simplestring_init(&v->id);
simplestring_init(&v->str);
}
return v;
}
/*******/
/****f* VALUE/XMLRPC_SetValueID_Case
* NAME
* XMLRPC_SetValueID_Case
* SYNOPSIS
* const char *XMLRPC_SetValueID_Case(XMLRPC_VALUE value, const char* id, int len, XMLRPC_CASE id_case)
* FUNCTION
* Assign an ID (key) to an XMLRPC value.
* INPUTS
* value The xml value who's ID we will set.
* id The desired new id.
* len length of id string if known, or 0 if unknown.
* id_case one of XMLRPC_CASE
* RESULT
* const char* pointer to the newly allocated id string, or NULL
* SEE ALSO
* XMLRPC_SetValueID ()
* XMLRPC_GetValueID ()
* XMLRPC_VALUE
* XMLRPC_CASE
* SOURCE
*/
const char *XMLRPC_SetValueID_Case(XMLRPC_VALUE value, const char* id, int len, XMLRPC_CASE id_case) {
const char* pRetval = NULL;
if(value) {
if(id) {
simplestring_clear(&value->id);
(len > 0) ? simplestring_addn(&value->id, id, len) :
simplestring_add(&value->id, id);
/* upper or lower case string in place if required. could be a separate func. */
if(id_case == xmlrpc_case_lower || id_case == xmlrpc_case_upper) {
int i;
for(i = 0; i < value->id.len; i++) {
value->id.str[i] =
(id_case ==
xmlrpc_case_lower) ? tolower (value->id.
str[i]) : toupper (value->
id.
str[i]);
}
}
pRetval = value->id.str;
#ifdef XMLRPC_DEBUG_REFCOUNT
printf("set value id: %s\n", pRetval);
#endif
}
}
return pRetval;
}
/*******/
/****f* VALUE/XMLRPC_SetValueString
* NAME
* XMLRPC_SetValueString
* SYNOPSIS
* const char *XMLRPC_SetValueString(XMLRPC_VALUE value, const char* val, int len)
* FUNCTION
* Assign a string value to an XMLRPC_VALUE, and set it to type xmlrpc_string
* INPUTS
* value The xml value who's ID we will set.
* val The desired new string val.
* len length of val string if known, or 0 if unknown.
* RESULT
* const char* pointer to the newly allocated value string, or NULL
* SEE ALSO
* XMLRPC_GetValueString ()
* XMLRPC_VALUE
* XMLRPC_VALUE_TYPE
* SOURCE
*/
const char *XMLRPC_SetValueString(XMLRPC_VALUE value, const char* val, int len) {
char *pRetval = NULL;
if(value && val) {
simplestring_clear(&value->str);
(len > 0) ? simplestring_addn(&value->str, val, len) :
simplestring_add(&value->str, val);
value->type = xmlrpc_string;
pRetval = (char *)value->str.str;
}
return pRetval;
}
/*******/
/****f* VALUE/XMLRPC_SetValueInt
* NAME
* XMLRPC_SetValueInt
* SYNOPSIS
* void XMLRPC_SetValueInt(XMLRPC_VALUE value, int val)
* FUNCTION
* Assign an int value to an XMLRPC_VALUE, and set it to type xmlrpc_int
* INPUTS
* value The xml value who's ID we will set.
* val The desired new integer value
* RESULT
* SEE ALSO
* XMLRPC_GetValueInt ()
* XMLRPC_VALUE
* XMLRPC_VALUE_TYPE
* SOURCE
*/
void XMLRPC_SetValueInt(XMLRPC_VALUE value, int val) {
if(value) {
value->type = xmlrpc_int;
value->i = val;
}
}
/*******/
/****f* VALUE/XMLRPC_SetValueBoolean
* NAME
* XMLRPC_SetValueBoolean
* SYNOPSIS
* void XMLRPC_SetValueBoolean(XMLRPC_VALUE value, int val)
* FUNCTION
* Assign a boolean value to an XMLRPC_VALUE, and set it to type xmlrpc_boolean
* INPUTS
* value The xml value who's value we will set.
* val The desired new boolean value. [0 | 1]
* RESULT