Menu

[r1953]: / advanced / src / xmlrpc_client.c  Maximize  Restore  History

Download this file

1118 lines (862 with data), 37.2 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
/* Copyright information is at end of file */
#define _XOPEN_SOURCE 600 /* Make sure strdup() is in <string.h> */
#include "xmlrpc_config.h"
#undef PACKAGE
#undef VERSION
#include <stdarg.h>
#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-c/base.h"
#include "xmlrpc-c/base_int.h"
#include "xmlrpc-c/string_int.h"
#include "xmlrpc-c/client.h"
#include "xmlrpc-c/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"
#include "version.h"
struct xmlrpc_client {
/*----------------------------------------------------------------------------
This represents a client object.
-----------------------------------------------------------------------------*/
bool myTransport;
/* The transport described below was created by this object;
No one else knows it exists and this object is responsible
for destroying it.
*/
struct xmlrpc_client_transport * transportP;
struct xmlrpc_client_transport_ops transportOps;
xmlrpc_dialect dialect;
};
struct xmlrpc_call_info {
/* This is all the information needed to finish executing a started
RPC.
You don't need this for an RPC the user executes synchronously,
because then you can just use the storage in which the user passed
his arguments. But for asynchronous, the user will take back his
storage, and we need to keep this info in our own.
*/
struct {
/* This are arguments to pass to the completion function. It
doesn't make sense to use them for anything else. In fact, it
really doesn't make sense for them to be arguments to the
completion function, but they are historically. */
const char * serverUrl;
const char * methodName;
xmlrpc_value * paramArrayP;
void * userData;
} completionArgs;
xmlrpc_response_handler completionFn;
/* The serialized XML data passed to this call. We keep this around
** for use by our source_anchor field. */
xmlrpc_mem_block *serialized_xml;
};
/*=========================================================================
Global Constant Setup/Teardown
=========================================================================*/
static void
callTransportSetup(xmlrpc_env * const envP,
xmlrpc_transport_setup setupFn) {
if (setupFn)
setupFn(envP);
}
static void
setupTransportGlobalConst(xmlrpc_env * const envP) {
#if MUST_BUILD_WININET_CLIENT
if (!envP->fault_occurred)
callTransportSetup(envP,
xmlrpc_wininet_transport_ops.setup_global_const);
#endif
#if MUST_BUILD_CURL_CLIENT
if (!envP->fault_occurred)
callTransportSetup(envP,
xmlrpc_curl_transport_ops.setup_global_const);
#endif
#if MUST_BUILD_LIBWWW_CLIENT
if (!envP->fault_occurred)
callTransportSetup(envP,
xmlrpc_libwww_transport_ops.setup_global_const);
#endif
}
static void
callTransportTeardown(xmlrpc_transport_teardown teardownFn) {
if (teardownFn)
teardownFn();
}
static void
teardownTransportGlobalConst(void) {
#if MUST_BUILD_WININET_CLIENT
callTransportTeardown(
xmlrpc_wininet_transport_ops.teardown_global_const);
#endif
#if MUST_BUILD_CURL_CLIENT
callTransportTeardown(
xmlrpc_curl_transport_ops.teardown_global_const);
#endif
#if MUST_BUILD_LIBWWW_CLIENT
callTransportTeardown(
xmlrpc_libwww_transport_ops.teardown_global_const);
#endif
}
/*=========================================================================
Global stuff (except the global client)
=========================================================================*/
static unsigned int constSetupCount = 0;
void
xmlrpc_client_setup_global_const(xmlrpc_env * const envP) {
/*----------------------------------------------------------------------------
Set up pseudo-constant global variables (they'd be constant, except that
the library loader doesn't set them. An explicit call from the loaded
program does).
This function is not thread-safe. The user is supposed to call it
(perhaps cascaded down from a multitude of higher level libraries)
as part of early program setup, when the program is only one thread.
-----------------------------------------------------------------------------*/
if (constSetupCount == 0)
setupTransportGlobalConst(envP);
++constSetupCount;
}
void
xmlrpc_client_teardown_global_const(void) {
/*----------------------------------------------------------------------------
Complement to xmlrpc_client_setup_global_const().
This function is not thread-safe. The user is supposed to call it
(perhaps cascaded down from a multitude of higher level libraries)
as part of final program cleanup, when the program is only one thread.
-----------------------------------------------------------------------------*/
assert(constSetupCount > 0);
--constSetupCount;
if (constSetupCount == 0)
teardownTransportGlobalConst();
}
unsigned int const xmlrpc_client_version_major = XMLRPC_VERSION_MAJOR;
unsigned int const xmlrpc_client_version_minor = XMLRPC_VERSION_MINOR;
unsigned int const xmlrpc_client_version_point = XMLRPC_VERSION_POINT;
/*=========================================================================
Client Create/Destroy
=========================================================================*/
static void
getTransportOps(
xmlrpc_env * const envP,
const char * const transportName,
const struct xmlrpc_client_transport_ops ** const opsPP) {
if (false) {
}
#if MUST_BUILD_WININET_CLIENT
else if (strcmp(transportName, "wininet") == 0)
*opsPP = &xmlrpc_wininet_transport_ops;
#endif
#if MUST_BUILD_CURL_CLIENT
else if (strcmp(transportName, "curl") == 0)
*opsPP = &xmlrpc_curl_transport_ops;
#endif
#if MUST_BUILD_LIBWWW_CLIENT
else if (strcmp(transportName, "libwww") == 0)
*opsPP = &xmlrpc_libwww_transport_ops;
#endif
else
xmlrpc_faultf(envP, "Unrecognized XML transport name '%s'",
transportName);
}
struct xportParms {
const void * parmsP;
size_t size;
};
static void
getTransportParmsFromClientParms(
xmlrpc_env * const envP,
const struct xmlrpc_clientparms * const clientparmsP,
unsigned int const parmSize,
struct xportParms * const xportParmsP) {
if (parmSize < XMLRPC_CPSIZE(transportparmsP) ||
clientparmsP->transportparmsP == NULL) {
xportParmsP->parmsP = NULL;
xportParmsP->size = 0;
} else {
xportParmsP->parmsP = clientparmsP->transportparmsP;
if (parmSize < XMLRPC_CPSIZE(transportparm_size))
xmlrpc_faultf(envP, "Your 'clientparms' argument contains the "
"transportparmsP member, "
"but no transportparms_size member");
else
xportParmsP->size = clientparmsP->transportparm_size;
}
}
static void
getTransportInfo(
xmlrpc_env * const envP,
const struct xmlrpc_clientparms * const clientparmsP,
unsigned int const parmSize,
const char ** const transportNameP,
struct xportParms * const transportParmsP,
const struct xmlrpc_client_transport_ops ** const transportOpsPP,
xmlrpc_client_transport ** const transportPP) {
const char * transportNameParm;
xmlrpc_client_transport * transportP;
const struct xmlrpc_client_transport_ops * transportOpsP;
if (parmSize < XMLRPC_CPSIZE(transport))
transportNameParm = NULL;
else
transportNameParm = clientparmsP->transport;
if (parmSize < XMLRPC_CPSIZE(transportP))
transportP = NULL;
else
transportP = clientparmsP->transportP;
if (parmSize < XMLRPC_CPSIZE(transportOpsP))
transportOpsP = NULL;
else
transportOpsP = clientparmsP->transportOpsP;
if ((transportOpsP && !transportP) || (transportP && ! transportOpsP))
xmlrpc_faultf(envP, "'transportOpsP' and 'transportP' go together. "
"You must specify both or neither");
else if (transportNameParm && transportP)
xmlrpc_faultf(envP, "You cannot specify both 'transport' and "
"'transportP' transport parameters.");
else if (transportP)
*transportNameP = NULL;
else if (transportNameParm)
*transportNameP = transportNameParm;
else
*transportNameP = xmlrpc_client_get_default_transport(envP);
*transportOpsPP = transportOpsP;
*transportPP = transportP;
if (!envP->fault_occurred) {
getTransportParmsFromClientParms(
envP, clientparmsP, parmSize, transportParmsP);
if (!envP->fault_occurred) {
if (transportParmsP->parmsP && !transportNameParm)
xmlrpc_faultf(
envP,
"You specified transport parameters, but did not "
"specify a transport type. Parameters are specific "
"to a particular type.");
}
}
}
static void
getDialectFromClientParms(
const struct xmlrpc_clientparms * const clientparmsP,
unsigned int const parmSize,
xmlrpc_dialect * const dialectP) {
if (parmSize < XMLRPC_CPSIZE(dialect))
*dialectP = xmlrpc_dialect_i8;
else
*dialectP = clientparmsP->dialect;
}
static void
clientCreate(
xmlrpc_env * const envP,
bool const myTransport,
const struct xmlrpc_client_transport_ops * const transportOpsP,
struct xmlrpc_client_transport * const transportP,
xmlrpc_dialect const dialect,
xmlrpc_client ** const clientPP) {
XMLRPC_ASSERT_PTR_OK(transportOpsP);
XMLRPC_ASSERT_PTR_OK(transportP);
XMLRPC_ASSERT_PTR_OK(clientPP);
if (constSetupCount == 0) {
xmlrpc_faultf(envP,
"You have not called "
"xmlrpc_client_setup_global_const().");
/* Impl note: We can't just call it now because it isn't
thread-safe.
*/
} else {
xmlrpc_client * clientP;
MALLOCVAR(clientP);
if (clientP == NULL)
xmlrpc_faultf(envP, "Unable to allocate memory for "
"client descriptor.");
else {
clientP->myTransport = myTransport;
clientP->transportOps = *transportOpsP;
clientP->transportP = transportP;
clientP->dialect = dialect;
*clientPP = clientP;
}
}
}
static void
createTransportAndClient(
xmlrpc_env * const envP,
const char * const transportName,
const void * const transportparmsP,
size_t const transportparmSize,
int const flags,
const char * const appname,
const char * const appversion,
xmlrpc_dialect const dialect,
xmlrpc_client ** const clientPP) {
const struct xmlrpc_client_transport_ops * transportOpsP;
getTransportOps(envP, transportName, &transportOpsP);
if (!envP->fault_occurred) {
xmlrpc_client_transport * transportP;
/* The following call is not thread-safe */
transportOpsP->create(
envP, flags, appname, appversion,
transportparmsP, transportparmSize,
&transportP);
if (!envP->fault_occurred) {
bool const myTransportTrue = true;
clientCreate(envP, myTransportTrue, transportOpsP, transportP,
dialect, clientPP);
if (envP->fault_occurred)
transportOpsP->destroy(transportP);
}
}
}
void
xmlrpc_client_create(xmlrpc_env * const envP,
int const flags,
const char * const appname,
const char * const appversion,
const struct xmlrpc_clientparms * const clientparmsP,
unsigned int const parmSize,
xmlrpc_client ** const clientPP) {
XMLRPC_ASSERT_PTR_OK(clientPP);
if (constSetupCount == 0) {
xmlrpc_faultf(envP,
"You have not called "
"xmlrpc_client_setup_global_const().");
/* Impl note: We can't just call it now because it isn't
thread-safe.
*/
} else {
const char * transportName = transportName;
struct xportParms transportparms = transportparms;
const struct xmlrpc_client_transport_ops * transportOpsP;
xmlrpc_client_transport * transportP;
xmlrpc_dialect dialect;
getTransportInfo(envP, clientparmsP, parmSize, &transportName,
&transportparms, &transportOpsP, &transportP);
getDialectFromClientParms(clientparmsP, parmSize, &dialect);
if (!envP->fault_occurred) {
if (transportName)
createTransportAndClient(envP, transportName,
transportparms.parmsP,
transportparms.size,
flags, appname, appversion, dialect,
clientPP);
else {
bool myTransportFalse = false;
clientCreate(envP, myTransportFalse,
transportOpsP, transportP, dialect, clientPP);
}
}
}
}
void
xmlrpc_client_destroy(xmlrpc_client * const clientP) {
XMLRPC_ASSERT_PTR_OK(clientP);
if (clientP->myTransport)
clientP->transportOps.destroy(clientP->transportP);
free(clientP);
}
/*=========================================================================
Call/Response Utilities
=========================================================================*/
static void
makeCallXml(xmlrpc_env * const envP,
const char * const methodName,
xmlrpc_value * const paramArrayP,
xmlrpc_dialect const dialect,
xmlrpc_mem_block ** const callXmlPP) {
XMLRPC_ASSERT_VALUE_OK(paramArrayP);
XMLRPC_ASSERT_PTR_OK(callXmlPP);
if (methodName == NULL)
xmlrpc_faultf(envP, "method name argument is NULL pointer");
else {
xmlrpc_mem_block * callXmlP;
callXmlP = XMLRPC_MEMBLOCK_NEW(char, envP, 0);
if (!envP->fault_occurred) {
xmlrpc_serialize_call2(envP, callXmlP, methodName, paramArrayP,
dialect);
*callXmlPP = callXmlP;
if (envP->fault_occurred)
XMLRPC_MEMBLOCK_FREE(char, callXmlP);
}
}
}
/*=========================================================================
Synchronous Call
=========================================================================*/
void
xmlrpc_client_transport_call2(
xmlrpc_env * const envP,
xmlrpc_client * const clientP,
const xmlrpc_server_info * const serverP,
xmlrpc_mem_block * const callXmlP,
xmlrpc_mem_block ** const respXmlPP) {
XMLRPC_ASSERT_PTR_OK(clientP);
XMLRPC_ASSERT_PTR_OK(serverP);
XMLRPC_ASSERT_PTR_OK(callXmlP);
XMLRPC_ASSERT_PTR_OK(respXmlPP);
clientP->transportOps.call(
envP, clientP->transportP, serverP, callXmlP,
respXmlPP);
}
static void
parseResponse(xmlrpc_env * const envP,
xmlrpc_mem_block * const respXmlP,
xmlrpc_value ** const resultPP,
int * const faultCodeP,
const char ** const faultStringP) {
xmlrpc_env respEnv;
xmlrpc_env_init(&respEnv);
xmlrpc_parse_response2(
&respEnv,
XMLRPC_MEMBLOCK_CONTENTS(char, respXmlP),
XMLRPC_MEMBLOCK_SIZE(char, respXmlP),
resultPP, faultCodeP, faultStringP);
if (respEnv.fault_occurred)
xmlrpc_env_set_fault_formatted(
envP, respEnv.fault_code,
"Unable to make sense of XML-RPC response from server. "
"%s. Use XMLRPC_TRACE_XML to see for yourself",
respEnv.fault_string);
xmlrpc_env_clean(&respEnv);
}
void
xmlrpc_client_call2(xmlrpc_env * const envP,
struct xmlrpc_client * const clientP,
const xmlrpc_server_info * const serverInfoP,
const char * const methodName,
xmlrpc_value * const paramArrayP,
xmlrpc_value ** const resultPP) {
xmlrpc_mem_block * callXmlP = callXmlP;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(clientP);
XMLRPC_ASSERT_PTR_OK(serverInfoP);
XMLRPC_ASSERT_PTR_OK(paramArrayP);
makeCallXml(envP, methodName, paramArrayP, clientP->dialect, &callXmlP);
if (!envP->fault_occurred) {
xmlrpc_mem_block * respXmlP;
xmlrpc_traceXml("XML-RPC CALL",
XMLRPC_MEMBLOCK_CONTENTS(char, callXmlP),
XMLRPC_MEMBLOCK_SIZE(char, callXmlP));
clientP->transportOps.call(
envP, clientP->transportP, serverInfoP, callXmlP, &respXmlP);
if (!envP->fault_occurred) {
int faultCode;
const char * faultString;
xmlrpc_traceXml("XML-RPC RESPONSE",
XMLRPC_MEMBLOCK_CONTENTS(char, respXmlP),
XMLRPC_MEMBLOCK_SIZE(char, respXmlP));
parseResponse(envP, respXmlP, resultPP, &faultCode, &faultString);
if (!envP->fault_occurred) {
if (faultString) {
xmlrpc_env_set_fault_formatted(
envP, faultCode,
"RPC failed at server. %s", faultString);
xmlrpc_strfree(faultString);
} else
XMLRPC_ASSERT_VALUE_OK(*resultPP);
}
XMLRPC_MEMBLOCK_FREE(char, respXmlP);
}
XMLRPC_MEMBLOCK_FREE(char, callXmlP);
}
}
static void
computeParamArray(xmlrpc_env * const envP,
const char * const format,
va_list args,
xmlrpc_value ** const paramArrayPP) {
/*----------------------------------------------------------------------------
'format' and 'args' specify the parameter list of an RPC, in the form
of an XML-RPC array value, with one element per RPC parameter.
'format' is an XML-RPC value format string, e.g. "(ii{s:i,s:i})".
'args' is the list of substitution values for that string
(6 values in this example, 4 integers and 2 strings).
We return the XML-RPC value 'format' and 'args' represent, but throw an
error if they don't validly specify a single array.
Note that it is a common user error to specify the format string as a
single or string of argument types, instead of as an array of argument
types. E.g. "i" or "ii" instead of "(i)" and "(ii)". So we try
especially hard to give an informative message for that case.
-----------------------------------------------------------------------------*/
xmlrpc_env env;
xmlrpc_value * paramArrayP;
const char * suffix;
/* Stuff left over in format string after parameter array
specification.
*/
xmlrpc_env_init(&env);
xmlrpc_build_value_va(&env, format, args, &paramArrayP, &suffix);
if (env.fault_occurred)
xmlrpc_env_set_fault_formatted(
envP, env.fault_code, "Invalid RPC arguments. "
"The format argument must indicate a single array (each element "
"of which is one argument to the XML-RPC call), and the "
"following arguments must correspond to that format argument. "
"The failure is: %s",
env.fault_string);
else {
XMLRPC_ASSERT_VALUE_OK(paramArrayP);
if (*suffix != '\0')
xmlrpc_faultf(envP,
"Junk after the parameter array specifier: '%s'. "
"The format string must specify exactly one value: "
"an array of RPC parameters",
suffix);
else {
if (xmlrpc_value_type(paramArrayP) != XMLRPC_TYPE_ARRAY)
xmlrpc_faultf(
envP,
"You must specify the parameter list as an "
"XML-RPC array value, "
"each element of which is a parameter of the RPC. "
"But your format string specifies an XML-RPC %s, not "
"an array",
xmlrpc_type_name(xmlrpc_value_type(paramArrayP)));
}
if (env.fault_occurred)
xmlrpc_DECREF(paramArrayP);
else
*paramArrayPP = paramArrayP;
}
xmlrpc_env_clean(&env);
}
void
xmlrpc_client_call_server2_va(xmlrpc_env * const envP,
struct xmlrpc_client * const clientP,
const xmlrpc_server_info * const serverInfoP,
const char * const methodName,
const char * const format,
va_list args,
xmlrpc_value ** const resultPP) {
/* This function exists only for use by the global client function
xmlrpc_client_call_server().
*/
xmlrpc_value * paramArrayP;
/* The XML-RPC parameter list array */
computeParamArray(envP, format, args, &paramArrayP);
if (!envP->fault_occurred) {
xmlrpc_client_call2(envP, clientP,
serverInfoP, methodName, paramArrayP,
resultPP);
xmlrpc_DECREF(paramArrayP);
}
}
void
xmlrpc_client_call2f_va(xmlrpc_env * const envP,
xmlrpc_client * const clientP,
const char * const serverUrl,
const char * const methodName,
const char * const format,
xmlrpc_value ** const resultPP,
va_list args) {
xmlrpc_value * paramArrayP;
/* The XML-RPC parameter list array */
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(clientP);
XMLRPC_ASSERT_PTR_OK(serverUrl);
XMLRPC_ASSERT_PTR_OK(methodName);
XMLRPC_ASSERT_PTR_OK(format);
XMLRPC_ASSERT_PTR_OK(resultPP);
computeParamArray(envP, format, args, &paramArrayP);
if (!envP->fault_occurred) {
xmlrpc_server_info * serverInfoP;
serverInfoP = xmlrpc_server_info_new(envP, serverUrl);
if (!envP->fault_occurred) {
/* Perform the actual XML-RPC call. */
xmlrpc_client_call2(envP, clientP,
serverInfoP, methodName, paramArrayP,
resultPP);
if (!envP->fault_occurred)
XMLRPC_ASSERT_VALUE_OK(*resultPP);
xmlrpc_server_info_free(serverInfoP);
}
xmlrpc_DECREF(paramArrayP);
}
}
void
xmlrpc_client_call2f(xmlrpc_env * const envP,
xmlrpc_client * const clientP,
const char * const serverUrl,
const char * const methodName,
xmlrpc_value ** const resultPP,
const char * const format,
...) {
va_list args;
XMLRPC_ASSERT_PTR_OK(format);
va_start(args, format);
xmlrpc_client_call2f_va(envP, clientP, serverUrl,
methodName, format, resultPP, args);
va_end(args);
}
/*=========================================================================
Asynchronous Call
=========================================================================*/
static void
callInfoSetCompletion(xmlrpc_env * const envP,
struct xmlrpc_call_info * const callInfoP,
const char * const serverUrl,
const char * const methodName,
xmlrpc_value * const paramArrayP,
xmlrpc_response_handler completionFn,
void * const userData) {
callInfoP->completionFn = completionFn;
callInfoP->completionArgs.userData = userData;
callInfoP->completionArgs.serverUrl = strdup(serverUrl);
if (callInfoP->completionArgs.serverUrl == NULL)
xmlrpc_faultf(envP, "Couldn't get memory to store server URL");
else {
callInfoP->completionArgs.methodName = strdup(methodName);
if (callInfoP->completionArgs.methodName == NULL)
xmlrpc_faultf(envP, "Couldn't get memory to store method name");
else {
callInfoP->completionArgs.paramArrayP = paramArrayP;
xmlrpc_INCREF(paramArrayP);
}
if (envP->fault_occurred)
xmlrpc_strfree(callInfoP->completionArgs.serverUrl);
}
}
static void
callInfoCreate(xmlrpc_env * const envP,
const char * const methodName,
xmlrpc_value * const paramArrayP,
xmlrpc_dialect const dialect,
const char * const serverUrl,
xmlrpc_response_handler completionFn,
void * const userData,
struct xmlrpc_call_info ** const callInfoPP) {
/*----------------------------------------------------------------------------
Create a call_info object. A call_info object represents an XML-RPC
call.
-----------------------------------------------------------------------------*/
struct xmlrpc_call_info * callInfoP;
XMLRPC_ASSERT_PTR_OK(serverUrl);
XMLRPC_ASSERT_PTR_OK(methodName);
XMLRPC_ASSERT_VALUE_OK(paramArrayP);
XMLRPC_ASSERT_PTR_OK(callInfoPP);
MALLOCVAR(callInfoP);
if (callInfoP == NULL)
xmlrpc_faultf(envP, "Couldn't allocate memory for xmlrpc_call_info");
else {
xmlrpc_mem_block * callXmlP = callXmlP;
makeCallXml(envP, methodName, paramArrayP, dialect, &callXmlP);
if (!envP->fault_occurred) {
xmlrpc_traceXml("XML-RPC CALL",
XMLRPC_MEMBLOCK_CONTENTS(char, callXmlP),
XMLRPC_MEMBLOCK_SIZE(char, callXmlP));
callInfoP->serialized_xml = callXmlP;
callInfoSetCompletion(envP, callInfoP, serverUrl, methodName,
paramArrayP, completionFn, userData);
if (envP->fault_occurred)
free(callInfoP);
}
}
*callInfoPP = callInfoP;
}
static void
callInfoDestroy(struct xmlrpc_call_info * const callInfoP) {
XMLRPC_ASSERT_PTR_OK(callInfoP);
if (callInfoP->completionFn) {
xmlrpc_DECREF(callInfoP->completionArgs.paramArrayP);
xmlrpc_strfree(callInfoP->completionArgs.methodName);
xmlrpc_strfree(callInfoP->completionArgs.serverUrl);
}
if (callInfoP->serialized_xml)
xmlrpc_mem_block_free(callInfoP->serialized_xml);
free(callInfoP);
}
void
xmlrpc_client_event_loop_finish(xmlrpc_client * const clientP) {
XMLRPC_ASSERT_PTR_OK(clientP);
clientP->transportOps.finish_asynch(
clientP->transportP, timeout_no, 0);
}
void
xmlrpc_client_event_loop_finish_timeout(xmlrpc_client * const clientP,
xmlrpc_timeout const timeout) {
XMLRPC_ASSERT_PTR_OK(clientP);
clientP->transportOps.finish_asynch(
clientP->transportP, timeout_yes, timeout);
}
static void
asynchComplete(struct xmlrpc_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 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 *responseXmlP might very well indicate that the server
failed the request.
-----------------------------------------------------------------------------*/
xmlrpc_env env;
xmlrpc_value * resultP = resultP;
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) {
int faultCode;
const char * faultString;
xmlrpc_parse_response2(&env,
XMLRPC_MEMBLOCK_CONTENTS(char, responseXmlP),
XMLRPC_MEMBLOCK_SIZE(char, responseXmlP),
&resultP, &faultCode, &faultString);
if (!env.fault_occurred) {
if (faultString) {
xmlrpc_env_set_fault_formatted(
&env, faultCode,
"RPC failed at server. %s", faultString);
xmlrpc_strfree(faultString);
}
}
}
/* Call the user's completion function with the RPC result */
(*callInfoP->completionFn)(callInfoP->completionArgs.serverUrl,
callInfoP->completionArgs.methodName,
callInfoP->completionArgs.paramArrayP,
callInfoP->completionArgs.userData,
&env, resultP);
if (!env.fault_occurred)
xmlrpc_DECREF(resultP);
callInfoDestroy(callInfoP);
xmlrpc_env_clean(&env);
}
void
xmlrpc_client_start_rpc(xmlrpc_env * const envP,
struct xmlrpc_client * const clientP,
const xmlrpc_server_info * const serverInfoP,
const char * const methodName,
xmlrpc_value * const argP,
xmlrpc_response_handler completionFn,
void * const userData) {
struct xmlrpc_call_info * callInfoP;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(clientP);
XMLRPC_ASSERT_PTR_OK(serverInfoP);
XMLRPC_ASSERT_PTR_OK(methodName);
XMLRPC_ASSERT_VALUE_OK(argP);
callInfoCreate(envP, methodName, argP, clientP->dialect,
serverInfoP->serverUrl, completionFn, userData,
&callInfoP);
if (!envP->fault_occurred)
clientP->transportOps.send_request(
envP, clientP->transportP, serverInfoP,
callInfoP->serialized_xml,
&asynchComplete, callInfoP);
if (envP->fault_occurred)
callInfoDestroy(callInfoP);
else {
/* asynchComplete() will destroy *callInfoP */
}
}
void
xmlrpc_client_start_rpcf_server_va(
xmlrpc_env * const envP,
xmlrpc_client * const clientP,
const xmlrpc_server_info * const serverInfoP,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
const char * const format,
va_list args) {
xmlrpc_value * paramArrayP;
/* The XML-RPC parameter list array */
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(clientP);
XMLRPC_ASSERT_PTR_OK(serverInfoP);
XMLRPC_ASSERT_PTR_OK(methodName);
XMLRPC_ASSERT_PTR_OK(format);
computeParamArray(envP, format, args, &paramArrayP);
if (!envP->fault_occurred) {
xmlrpc_client_start_rpc(envP, clientP,
serverInfoP, methodName, paramArrayP,
responseHandler, userData);
xmlrpc_DECREF(paramArrayP);
}
}
void
xmlrpc_client_start_rpcf_va(xmlrpc_env * const envP,
xmlrpc_client * const clientP,
const char * const serverUrl,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
const char * const format,
va_list args) {
xmlrpc_server_info * serverInfoP;
serverInfoP = xmlrpc_server_info_new(envP, serverUrl);
if (!envP->fault_occurred) {
xmlrpc_client_start_rpcf_server_va(
envP, clientP,
serverInfoP, methodName,
responseHandler, userData,
format, args);
xmlrpc_server_info_free(serverInfoP);
}
}
void
xmlrpc_client_start_rpcf(xmlrpc_env * const envP,
xmlrpc_client * const clientP,
const char * const serverUrl,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
const char * const format,
...) {
va_list args;
XMLRPC_ASSERT_PTR_OK(format);
va_start(args, format);
xmlrpc_client_start_rpcf_va(envP, clientP, serverUrl, methodName,
responseHandler,
userData, format, args);
va_end(args);
}
/*=========================================================================
Miscellaneous
=========================================================================*/
const char *
xmlrpc_client_get_default_transport(xmlrpc_env * const envP ATTR_UNUSED) {
return XMLRPC_DEFAULT_TRANSPORT;
}
void
xmlrpc_client_set_interrupt(xmlrpc_client * const clientP,
int * const interruptP) {
if (clientP->transportOps.set_interrupt)
clientP->transportOps.set_interrupt(clientP->transportP, interruptP);
}
/* 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.
*/
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.