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
|
/* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE. */
#include "xmlrpc_config.h"
#undef PACKAGE
#undef VERSION
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include "bool.h"
#include "mallocvar.h"
#include "xmlrpc.h"
#include "xmlrpc_int.h"
#include "xmlrpc_client.h"
#include "xmlrpc_client_int.h"
/* transport_config.h defines XMLRPC_DEFAULT_TRANSPORT,
MUST_BUILD_WININET_CLIENT, MUST_BUILD_CURL_CLIENT,
MUST_BUILD_LIBWWW_CLIENT
*/
#include "transport_config.h"
#if MUST_BUILD_WININET_CLIENT
#include "xmlrpc_wininet_transport.h"
#endif
#if MUST_BUILD_CURL_CLIENT
#include "xmlrpc_curl_transport.h"
#endif
#if MUST_BUILD_LIBWWW_CLIENT
#include "xmlrpc_libwww_transport.h"
#endif
struct xmlrpc_client {
/*----------------------------------------------------------------------------
This represents a client object.
-----------------------------------------------------------------------------*/
struct clientTransport * transportP;
};
typedef struct call_info
{
/* These fields are used when performing asynchronous calls.
** The _asynch_data_holder contains server_url, method_name and
** param_array, so it's the only thing we need to free. */
xmlrpc_value *_asynch_data_holder;
char *server_url;
char *method_name;
xmlrpc_value *param_array;
xmlrpc_response_handler callback;
void *user_data;
/* The serialized XML data passed to this call. We keep this around
** for use by our source_anchor field. */
xmlrpc_mem_block *serialized_xml;
} call_info;
static bool clientInitialized = FALSE;
/*=========================================================================
** Initialization and Shutdown
**=========================================================================
*/
static struct clientTransportOps clientTransportOps;
static struct xmlrpc_client client;
/* Some day, we need to make this dynamically allocated, so there can
be more than one client per program and just generally to provide
a cleaner interface.
*/
extern void
xmlrpc_client_init(int const flags,
const char * const appname,
const char * const appversion) {
struct xmlrpc_clientparms clientparms;
/* As our interface does not allow for failure, we just fail silently ! */
xmlrpc_env env;
xmlrpc_env_init(&env);
clientparms.transport = XMLRPC_DEFAULT_TRANSPORT;
xmlrpc_client_init2(&env, flags,
appname, appversion,
&clientparms, XMLRPC_CPSIZE(transport));
xmlrpc_env_clean(&env);
}
const char *
xmlrpc_client_get_default_transport(xmlrpc_env * const env ATTR_UNUSED) {
return XMLRPC_DEFAULT_TRANSPORT;
}
static void
setupTransport(xmlrpc_env * const envP,
const char * const transportName) {
if (FALSE) {
}
#if MUST_BUILD_WININET_CLIENT
else if (strcmp(transportName, "wininet") == 0)
clientTransportOps = xmlrpc_wininet_transport_ops;
#endif
#if MUST_BUILD_CURL_CLIENT
else if (strcmp(transportName, "curl") == 0)
clientTransportOps = xmlrpc_curl_transport_ops;
else if (strcmp(transportName, "libcurl") == 0)
clientTransportOps = xmlrpc_curl_transport_ops;
#endif
#if MUST_BUILD_LIBWWW_CLIENT
else if (strcmp(transportName, "libwww") == 0)
clientTransportOps = xmlrpc_libwww_transport_ops;
#endif
else
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR,
"Unrecognized XML transport name '%s'", transportName);
}
void
xmlrpc_client_init2(xmlrpc_env * const envP,
int const flags,
const char * const appname,
const char * const appversion,
struct xmlrpc_clientparms * const clientparmsP,
unsigned int const parm_size) {
if (clientInitialized)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR,
"Xmlrpc-c client instance has already been initialized "
"(need to call xmlrpc_client_cleanup() before you can "
"reinitialize).");
else {
const char * transportName;
if (parm_size < XMLRPC_CPSIZE(transport) ||
clientparmsP->transport == NULL) {
/* He didn't specify a transport. Use the default */
transportName = xmlrpc_client_get_default_transport(envP);
} else
transportName = clientparmsP->transport;
if (!envP->fault_occurred) {
setupTransport(envP, transportName);
if (!envP->fault_occurred) {
clientTransportOps.create(envP, flags, appname, appversion,
&client.transportP);
if (!envP->fault_occurred)
clientInitialized = TRUE;
}
}
}
}
void
xmlrpc_client_cleanup() {
XMLRPC_ASSERT(clientInitialized);
clientTransportOps.destroy(client.transportP);
clientInitialized = FALSE;
}
static void
call_info_free(call_info * const callInfoP) {
/* Assume the worst.. That only parts of the call_info are valid. */
XMLRPC_ASSERT_PTR_OK(callInfoP);
/* If this has been allocated, we're responsible for destroying it. */
if (callInfoP->_asynch_data_holder)
xmlrpc_DECREF(callInfoP->_asynch_data_holder);
/* Now we can blow away the XML data. */
if (callInfoP->serialized_xml)
xmlrpc_mem_block_free(callInfoP->serialized_xml);
free(callInfoP);
}
static void
call_info_new(xmlrpc_env * const envP,
xmlrpc_server_info * const server,
const char * const method_name,
xmlrpc_value * const argP,
call_info ** const callInfoPP) {
/*----------------------------------------------------------------------------
Create a call_info object. A call_info object represents an XML-RPC
call.
-----------------------------------------------------------------------------*/
call_info * callInfoP;
XMLRPC_ASSERT_PTR_OK(argP);
XMLRPC_ASSERT_PTR_OK(callInfoPP);
if (method_name == NULL)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR,
"method name argument is NULL pointer");
else if (server == NULL)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR,
"server info argument is NULL pointer");
else {
MALLOCVAR(callInfoP);
if (callInfoP == NULL)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR,
"Couldn't allocate memory for xmlrpc_call_info");
else {
xmlrpc_mem_block * callXmlP;
/* Clear contents. */
memset(callInfoP, 0, sizeof(*callInfoP));
/* Make the XML for our call */
callXmlP = XMLRPC_MEMBLOCK_NEW(char, envP, 0);
if (!envP->fault_occurred) {
xmlrpc_serialize_call(envP, callXmlP, method_name, argP);
if (!envP->fault_occurred) {
xmlrpc_traceXml("XML-RPC CALL",
XMLRPC_MEMBLOCK_CONTENTS(char, callXmlP),
(unsigned int)XMLRPC_MEMBLOCK_SIZE(char, callXmlP));
callInfoP->serialized_xml = callXmlP;
*callInfoPP = callInfoP;
}
if (envP->fault_occurred)
XMLRPC_MEMBLOCK_FREE(char, callXmlP);
}
if (envP->fault_occurred)
free(callInfoP);
}
}
}
static void
clientCallServerParams(xmlrpc_env * const envP,
struct clientTransport * const transportP,
xmlrpc_server_info * const serverP,
const char * const methodName,
xmlrpc_value * const paramArrayP,
xmlrpc_value ** const resultPP) {
call_info * callInfoP;
if (!clientInitialized)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR,
"Xmlrpc-c client instance has not been initialized "
"(need to call xmlrpc_client_init2()).");
else {
call_info_new(envP, serverP, methodName, paramArrayP, &callInfoP);
if (!envP->fault_occurred) {
xmlrpc_mem_block * respXmlP;
clientTransportOps.call(envP, transportP, serverP,
callInfoP->serialized_xml, callInfoP,
&respXmlP);
if (!envP->fault_occurred) {
xmlrpc_traceXml("XML-RPC RESPONSE",
XMLRPC_MEMBLOCK_CONTENTS(char, respXmlP),
(unsigned int)XMLRPC_MEMBLOCK_SIZE(char, respXmlP));
*resultPP = xmlrpc_parse_response(
envP,
XMLRPC_MEMBLOCK_CONTENTS(char, respXmlP),
XMLRPC_MEMBLOCK_SIZE(char, respXmlP));
XMLRPC_MEMBLOCK_FREE(char, respXmlP);
}
call_info_free(callInfoP);
}
}
}
xmlrpc_value *
xmlrpc_client_call_params(xmlrpc_env * const envP,
const char * const serverUrl,
const char * const methodName,
xmlrpc_value * const paramArrayP) {
xmlrpc_value *retval;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(serverUrl);
if (!clientInitialized)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR,
"Xmlrpc-c client instance has not been initialized "
"(need to call xmlrpc_client_init2()).");
else {
xmlrpc_server_info * serverP;
/* Build a server info object and make our call. */
serverP = xmlrpc_server_info_new(envP, serverUrl);
if (!envP->fault_occurred) {
clientCallServerParams(envP, client.transportP, serverP,
methodName, paramArrayP,
&retval);
xmlrpc_server_info_free(serverP);
}
}
if (!envP->fault_occurred)
XMLRPC_ASSERT_VALUE_OK(retval);
return retval;
}
static xmlrpc_value *
xmlrpc_client_call_va(xmlrpc_env * const envP,
const char * const server_url,
const char * const method_name,
const char * const format,
va_list args) {
xmlrpc_value * argP;
xmlrpc_value * retval = 0;
xmlrpc_env argenv;
const char * suffix;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(format);
/* Build our argument value. */
xmlrpc_env_init(&argenv);
xmlrpc_build_value_va(&argenv, format, args, &argP, &suffix);
if (argenv.fault_occurred) {
xmlrpc_env_set_fault_formatted(
envP, argenv.fault_code, "Invalid RPC arguments. "
"The format argument must indicate a single array, and the "
"following arguments must correspond to that format argument. "
"The failure is: %s",
argenv.fault_string);
xmlrpc_env_clean(&argenv);
} else {
XMLRPC_ASSERT_VALUE_OK(argP);
if (*suffix != '\0')
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR, "Junk after the argument "
"specifier: '%s'. There must be exactly one arument.",
suffix);
else {
/* Perform the actual XML-RPC call. */
retval = xmlrpc_client_call_params(
envP, server_url, method_name, argP);
if (!envP->fault_occurred)
XMLRPC_ASSERT_VALUE_OK(retval);
}
xmlrpc_DECREF(argP);
}
return retval;
}
xmlrpc_value *
xmlrpc_client_call(xmlrpc_env * const envP,
const char * const server_url,
const char * const method_name,
const char * const format,
...) {
xmlrpc_value * result;
va_list args;
va_start(args, format);
result = xmlrpc_client_call_va(envP, server_url,
method_name, format, args);
va_end(args);
return result;
}
xmlrpc_value *
xmlrpc_client_call_server(xmlrpc_env * const envP,
xmlrpc_server_info * const serverP,
const char * const methodName,
const char * const format,
...) {
va_list args;
xmlrpc_value * paramArrayP;
xmlrpc_value * retval;
const char * suffix;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(format);
/* Build our argument */
va_start(args, format);
xmlrpc_build_value_va(envP, format, args, ¶mArrayP, &suffix);
va_end(args);
if (!envP->fault_occurred) {
if (*suffix != '\0')
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR, "Junk after the argument "
"specifier: '%s'. There must be exactly one arument.",
suffix);
else
clientCallServerParams(envP, client.transportP, serverP,
methodName, paramArrayP,
&retval);
xmlrpc_DECREF(paramArrayP);
}
return retval;
}
void
xmlrpc_client_event_loop_finish_asynch(void) {
XMLRPC_ASSERT(clientInitialized);
clientTransportOps.finish_asynch(client.transportP, timeout_no, 0);
}
void
xmlrpc_client_event_loop_finish_asynch_timeout(timeout_t const timeout) {
XMLRPC_ASSERT(clientInitialized);
clientTransportOps.finish_asynch(client.transportP, timeout_yes, timeout);
}
static void
call_info_set_asynch_data(xmlrpc_env * const env,
call_info * const info,
const char * const server_url,
const char * const method_name,
xmlrpc_value * const argP,
xmlrpc_response_handler callback,
void * const user_data) {
xmlrpc_value *holder;
XMLRPC_ASSERT_ENV_OK(env);
XMLRPC_ASSERT_PTR_OK(info);
XMLRPC_ASSERT(info->_asynch_data_holder == NULL);
XMLRPC_ASSERT_PTR_OK(server_url);
XMLRPC_ASSERT_PTR_OK(method_name);
XMLRPC_ASSERT_VALUE_OK(argP);
/* Install our callback and user_data.
** (We're not responsible for destroying the user_data.) */
info->callback = callback;
info->user_data = user_data;
/* Build an XML-RPC data structure to hold our other data. This makes
** copies of server_url and method_name, and increments the reference
** to the argument *argP. */
holder = xmlrpc_build_value(env, "(ssV)",
server_url, method_name, argP);
XMLRPC_FAIL_IF_FAULT(env);
/* Parse the newly-allocated structure into our public member variables.
** This doesn't make any new references, so we can dispose of the whole
** thing by DECREF'ing the one master reference. Nifty, huh? */
xmlrpc_parse_value(env, holder, "(ssV)",
&info->server_url,
&info->method_name,
&info->param_array);
XMLRPC_FAIL_IF_FAULT(env);
/* Hand over ownership of the holder to the call_info struct. */
info->_asynch_data_holder = holder;
holder = NULL;
cleanup:
if (env->fault_occurred) {
if (holder)
xmlrpc_DECREF(holder);
}
}
/*=========================================================================
** xmlrpc_server_info
**=========================================================================
*/
xmlrpc_server_info *
xmlrpc_server_info_new (xmlrpc_env * const env,
const char * const server_url) {
xmlrpc_server_info *server;
char *url_copy;
/* Error-handling preconditions. */
url_copy = NULL;
XMLRPC_ASSERT_ENV_OK(env);
XMLRPC_ASSERT_PTR_OK(server_url);
/* Allocate our memory blocks. */
server = (xmlrpc_server_info*) malloc(sizeof(xmlrpc_server_info));
XMLRPC_FAIL_IF_NULL(server, env, XMLRPC_INTERNAL_ERROR,
"Couldn't allocate memory for xmlrpc_server_info");
memset(server, 0, sizeof(xmlrpc_server_info));
url_copy = (char*) malloc(strlen(server_url) + 1);
XMLRPC_FAIL_IF_NULL(url_copy, env, XMLRPC_INTERNAL_ERROR,
"Couldn't allocate memory for server URL");
/* Build our object. */
strcpy(url_copy, server_url);
server->_server_url = url_copy;
server->_http_basic_auth = NULL;
cleanup:
if (env->fault_occurred) {
if (url_copy)
free(url_copy);
if (server)
free(server);
return NULL;
}
return server;
}
xmlrpc_server_info * xmlrpc_server_info_copy(xmlrpc_env *env,
xmlrpc_server_info *aserver)
{
xmlrpc_server_info *server;
char *url_copy, *auth_copy;
XMLRPC_ASSERT_ENV_OK(env);
XMLRPC_ASSERT_PTR_OK(aserver);
/* Error-handling preconditions. */
url_copy = NULL;
auth_copy = NULL;
/* Allocate our memory blocks. */
server = (xmlrpc_server_info*) malloc(sizeof(xmlrpc_server_info));
XMLRPC_FAIL_IF_NULL(server, env, XMLRPC_INTERNAL_ERROR,
"Couldn't allocate memory for xmlrpc_server_info");
url_copy = (char*) malloc(strlen(aserver->_server_url) + 1);
XMLRPC_FAIL_IF_NULL(url_copy, env, XMLRPC_INTERNAL_ERROR,
"Couldn't allocate memory for server URL");
auth_copy = (char*) malloc(strlen(aserver->_http_basic_auth) + 1);
XMLRPC_FAIL_IF_NULL(auth_copy, env, XMLRPC_INTERNAL_ERROR,
"Couldn't allocate memory for authentication info");
/* Build our object. */
strcpy(url_copy, aserver->_server_url);
server->_server_url = url_copy;
strcpy(auth_copy, aserver->_http_basic_auth);
server->_http_basic_auth = auth_copy;
cleanup:
if (env->fault_occurred) {
if (url_copy)
free(url_copy);
if (auth_copy)
free(auth_copy);
if (server)
free(server);
return NULL;
}
return server;
}
void xmlrpc_server_info_free (xmlrpc_server_info *server)
{
XMLRPC_ASSERT_PTR_OK(server);
XMLRPC_ASSERT(server->_server_url != XMLRPC_BAD_POINTER);
if (server->_http_basic_auth)
free(server->_http_basic_auth);
free(server->_server_url);
server->_server_url = XMLRPC_BAD_POINTER;
free(server);
}
/*=========================================================================
** xmlrpc_client_call_asynch
**=========================================================================
*/
void
xmlrpc_client_call_asynch(const char * const serverUrl,
const char * const methodName,
xmlrpc_response_handler callback,
void * const userData,
const char * const format,
...) {
xmlrpc_env env;
va_list args;
xmlrpc_value * paramArrayP;
const char * suffix;
xmlrpc_env_init(&env);
XMLRPC_ASSERT_PTR_OK(serverUrl);
XMLRPC_ASSERT_PTR_OK(format);
/* Build our argument array. */
va_start(args, format);
xmlrpc_build_value_va(&env, format, args, ¶mArrayP, &suffix);
va_end(args);
if (env.fault_occurred) {
/* Unfortunately, we have no way to return an error and the
regular callback for a failed RPC is designed to have the
parameter array passed to it. This was probably an oversight
of the original asynch design, but now we have to be as
backward compatible as possible, so we do this:
*/
(*callback)(serverUrl, methodName, NULL, userData, &env, NULL);
} else {
if (*suffix != '\0')
xmlrpc_env_set_fault_formatted(
&env, XMLRPC_INTERNAL_ERROR, "Junk after the argument "
"specifier: '%s'. There must be exactly one arument.",
suffix);
else {
xmlrpc_server_info * serverP;
serverP = xmlrpc_server_info_new(&env, serverUrl);
if (!env.fault_occurred) {
xmlrpc_client_call_server_asynch_params(
serverP, methodName, callback, userData,
paramArrayP);
}
xmlrpc_server_info_free(serverP);
}
if (env.fault_occurred)
(*callback)(serverUrl, methodName, paramArrayP, userData,
&env, NULL);
xmlrpc_DECREF(paramArrayP);
}
xmlrpc_env_clean(&env);
}
void
xmlrpc_client_call_asynch_params(const char * const serverUrl,
const char * const methodName,
xmlrpc_response_handler callback,
void * const userData,
xmlrpc_value * const paramArrayP) {
xmlrpc_env env;
xmlrpc_server_info *serverP;
xmlrpc_env_init(&env);
XMLRPC_ASSERT_PTR_OK(serverUrl);
serverP = xmlrpc_server_info_new(&env, serverUrl);
if (!env.fault_occurred) {
xmlrpc_client_call_server_asynch_params(
serverP, methodName, callback, userData, paramArrayP);
xmlrpc_server_info_free(serverP);
}
if (env.fault_occurred)
/* We have no way to return failure; we report the failure
as it happened after we successfully started the RPC.
*/
(*callback)(serverUrl, methodName, paramArrayP, userData,
&env, NULL);
xmlrpc_env_clean(&env);
}
void
xmlrpc_client_call_server_asynch(xmlrpc_server_info * const serverP,
const char * const methodName,
xmlrpc_response_handler callback,
void * const userData,
const char * const format,
...) {
xmlrpc_env env;
va_list args;
xmlrpc_value * paramArrayP;
const char * suffix;
xmlrpc_env_init(&env);
XMLRPC_ASSERT_PTR_OK(format);
/* Build our parameter array. */
va_start(args, format);
xmlrpc_build_value_va(&env, format, args, ¶mArrayP, &suffix);
va_end(args);
if (env.fault_occurred) {
/* Unfortunately, we have no way to return an error and the
regular callback for a failed RPC is designed to have the
parameter array passed to it. This was probably an oversight
of the original asynch design, but now we have to be as
backward compatible as possible, so we do this:
*/
(*callback)(serverP->_server_url, methodName, NULL, userData,
&env, NULL);
} else {
if (*suffix != '\0')
xmlrpc_env_set_fault_formatted(
&env, XMLRPC_INTERNAL_ERROR, "Junk after the argument "
"specifier: '%s'. There must be exactly one arument.",
suffix);
else {
xmlrpc_client_call_server_asynch_params(
serverP, methodName, callback, userData, paramArrayP);
}
xmlrpc_DECREF(paramArrayP);
}
if (env.fault_occurred)
(*callback)(serverP->_server_url, methodName, paramArrayP, userData,
&env, NULL);
xmlrpc_env_clean(&env);
}
static void
asynchComplete(call_info * const callInfoP,
xmlrpc_mem_block * const responseXmlP,
xmlrpc_env const transportEnv) {
/*----------------------------------------------------------------------------
Complete an asynchronous XML-RPC call request.
This includes calling the user's RPC completion routine.
'transportEnv' describes the an error that the transport
encountered in processing the call. If the transport successfully
sent the call to the server and processed the response but the
server failed the call, 'transportEnv' indicates no error, and the
response in *callInfoP might very well indicate that the server
failed the request.
-----------------------------------------------------------------------------*/
xmlrpc_env env;
xmlrpc_value * responseP = 0;
xmlrpc_env_init(&env);
if (transportEnv.fault_occurred)
xmlrpc_env_set_fault_formatted(
&env, transportEnv.fault_code,
"Client transport failed to execute the RPC. %s",
transportEnv.fault_string);
if (!env.fault_occurred)
responseP = xmlrpc_parse_response(
&env,
XMLRPC_MEMBLOCK_CONTENTS(char, responseXmlP),
XMLRPC_MEMBLOCK_SIZE(char, responseXmlP));
/* Call the user's callback function with the result */
(*callInfoP->callback)(callInfoP->server_url,
callInfoP->method_name,
callInfoP->param_array,
callInfoP->user_data, &env, responseP);
if (!env.fault_occurred)
xmlrpc_DECREF(responseP);
call_info_free(callInfoP);
xmlrpc_env_clean(&env);
}
static void
sendRequest(xmlrpc_env * const envP,
struct clientTransport * const transportP,
xmlrpc_server_info * const serverP,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
xmlrpc_value * const argP) {
call_info * callInfoP;
call_info_new(envP, serverP, methodName, argP, &callInfoP);
if (!envP->fault_occurred) {
call_info_set_asynch_data(envP, callInfoP,
serverP->_server_url, methodName,
argP, responseHandler, userData);
if (!envP->fault_occurred)
clientTransportOps.send_request(
envP, transportP, serverP, callInfoP->serialized_xml,
&asynchComplete, callInfoP);
if (envP->fault_occurred)
call_info_free(callInfoP);
else {
/* asynchComplete() will free *callInfoP */
}
}
if (envP->fault_occurred) {
/* Transport did not start the call. Report the call complete
(with error) now.
*/
(*responseHandler)(serverP->_server_url, methodName, argP, userData,
envP, NULL);
} else {
/* The transport will call *responseHandler() when it has completed
the call
*/
}
}
void
xmlrpc_client_call_server_asynch_params(
xmlrpc_server_info * const serverP,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
xmlrpc_value * const argP) {
xmlrpc_env env;
xmlrpc_env_init(&env);
XMLRPC_ASSERT_PTR_OK(serverP);
XMLRPC_ASSERT_PTR_OK(methodName);
XMLRPC_ASSERT_PTR_OK(responseHandler);
XMLRPC_ASSERT_VALUE_OK(argP);
if (!clientInitialized)
xmlrpc_env_set_fault_formatted(
&env, XMLRPC_INTERNAL_ERROR,
"Xmlrpc-c client instance has not been initialized "
"(need to call xmlrpc_client_init2()).");
else
sendRequest(&env, client.transportP, serverP,
methodName, responseHandler, userData,
argP);
xmlrpc_env_clean(&env);
}
void
xmlrpc_server_info_set_basic_auth(xmlrpc_env * const envP,
xmlrpc_server_info * const serverP,
const char * const username,
const char * const password) {
size_t username_len, password_len, raw_token_len;
char *raw_token;
xmlrpc_mem_block *token;
char *token_data, *auth_type, *auth_header;
size_t token_len, auth_type_len, auth_header_len;
/* Error-handling preconditions. */
token = NULL;
auth_header = NULL;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(serverP);
XMLRPC_ASSERT_PTR_OK(username);
XMLRPC_ASSERT_PTR_OK(password);
/* Calculate some lengths. */
username_len = strlen(username);
password_len = strlen(password);
raw_token_len = username_len + password_len + 1;
/* Build a raw token of the form 'username:password'. */
raw_token = (char*) malloc(raw_token_len + 1);
XMLRPC_FAIL_IF_NULL(raw_token, envP, XMLRPC_INTERNAL_ERROR,
"Couldn't allocate memory for auth token");
strcpy(raw_token, username);
raw_token[username_len] = ':';
strcpy(&raw_token[username_len + 1], password);
/* Encode our raw token using Base64. */
token = xmlrpc_base64_encode_without_newlines(envP,
(unsigned char*) raw_token,
raw_token_len);
XMLRPC_FAIL_IF_FAULT(envP);
token_data = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, token);
token_len = XMLRPC_TYPED_MEM_BLOCK_SIZE(char, token);
/* Build our actual header value. (I hate string processing in C.) */
auth_type = "Basic ";
auth_type_len = strlen(auth_type);
auth_header_len = auth_type_len + token_len;
auth_header = (char*) malloc(auth_header_len + 1);
XMLRPC_FAIL_IF_NULL(auth_header, envP, XMLRPC_INTERNAL_ERROR,
"Couldn't allocate memory for auth header");
memcpy(auth_header, auth_type, auth_type_len);
memcpy(&auth_header[auth_type_len], token_data, token_len);
auth_header[auth_header_len] = '\0';
/* Clean up any pre-existing authentication information, and install
** the new value. */
if (serverP->_http_basic_auth)
free(serverP->_http_basic_auth);
serverP->_http_basic_auth = auth_header;
cleanup:
if (raw_token)
free(raw_token);
if (token)
xmlrpc_mem_block_free(token);
if (envP->fault_occurred) {
if (auth_header)
free(auth_header);
}
}
|