Menu

[r3316]: / advanced / examples / xmlrpc_server_validatee.c  Maximize  Restore  History

Download this file

413 lines (331 with data), 13.8 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
/* 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. */
/*============================================================================
xmlrpc_server_validatee
==============================================================================
This program runs an XMLRPC server, using the Xmlrpc-c libraries.
The server implements the methods that the Userland Validator1 test suite
invokes, which are supposed to exercise a broad range of XMLRPC server
function.
Coments here used to say you could get information about Validator1
from <http://validator.xmlrpc.com/>, but as of 2004.09.25, there's nothing
there (there's a web server, but it is not configured to serve that
particular URL).
============================================================================*/
#define WIN32_LEAN_AND_MEAN /* required by xmlrpc-c/server_abyss.h */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/server.h>
#include <xmlrpc-c/server_abyss.h>
#include "config.h" /* information about this build environment */
#define RETURN_IF_FAULT(envP) \
do { \
if ((envP)->fault_occurred) \
return NULL; \
} while (0)
/*=========================================================================
** validator1.arrayOfStructsTest
**=========================================================================
*/
static xmlrpc_value *
array_of_structs(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const user_data) {
xmlrpc_value * arrayP;
xmlrpc_value * retval;
xmlrpc_decompose_value(envP, paramArrayP, "(A)", &arrayP);
if (envP->fault_occurred)
retval = NULL;
else {
/* Add up all the struct elements named "curly". */
size_t size;
size = xmlrpc_array_size(envP, arrayP);
if (envP->fault_occurred)
retval = NULL;
else {
unsigned int sum;
unsigned int i;
sum = 0;
for (i = 0; i < size && !envP->fault_occurred; ++i) {
xmlrpc_value * strctP;
strctP = xmlrpc_array_get_item(envP, arrayP, i);
if (!envP->fault_occurred) {
xmlrpc_int32 curly;
xmlrpc_decompose_value(envP, strctP, "{s:i,*}",
"curly", &curly);
if (!envP->fault_occurred)
sum += curly;
}
}
xmlrpc_DECREF(arrayP);
if (envP->fault_occurred)
retval = NULL;
else
retval = xmlrpc_build_value(envP, "i", sum);
}
}
return retval;
}
/*=========================================================================
** validator1.countTheEntities
**=========================================================================
*/
static xmlrpc_value *
count_entities(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const user_data) {
const char * str;
size_t len, i;
xmlrpc_int32 left, right, amp, apos, quote;
xmlrpc_decompose_value(envP, paramArrayP, "(s#)", &str, &len);
RETURN_IF_FAULT(envP);
left = right = amp = apos = quote = 0;
for (i = 0; i < len; ++i) {
switch (str[i]) {
case '<': ++left; break;
case '>': ++right; break;
case '&': ++amp; break;
case '\'': ++apos; break;
case '\"': ++quote; break;
default: break;
}
}
free((void*)str);
return xmlrpc_build_value(envP, "{s:i,s:i,s:i,s:i,s:i}",
"ctLeftAngleBrackets", left,
"ctRightAngleBrackets", right,
"ctAmpersands", amp,
"ctApostrophes", apos,
"ctQuotes", quote);
}
/*=========================================================================
** validator1.easyStructTest
**=========================================================================
*/
static xmlrpc_value *
easy_struct(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const user_data) {
xmlrpc_int32 larry, moe, curly;
/* Parse our argument array and get the stooges. */
xmlrpc_decompose_value(envP, paramArrayP, "({s:i,s:i,s:i,*})",
"larry", &larry,
"moe", &moe,
"curly", &curly);
RETURN_IF_FAULT(envP);
/* Return our result. */
return xmlrpc_build_value(envP, "i", larry + moe + curly);
}
/*=========================================================================
** validator1.echoStructTest
**=========================================================================
*/
static xmlrpc_value *
echo_struct(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const user_data) {
xmlrpc_value * sP;
/* Parse our argument array. */
xmlrpc_decompose_value(envP, paramArrayP, "(S)", &sP);
RETURN_IF_FAULT(envP);
return sP; /* We transfer our reference on '*sP' to Caller */
}
/*=========================================================================
** validator1.manyTypesTest
**=========================================================================
*/
static xmlrpc_value *
many_types(xmlrpc_env * const env,
xmlrpc_value * const param_array,
void * const user_data) {
/* Create another reference to our argument array and return it as is. */
xmlrpc_INCREF(param_array);
return param_array;
}
/*=========================================================================
** validator1.moderateSizeArrayCheck
**=========================================================================
*/
static void
concatenate(xmlrpc_env * const envP,
const char * const str1,
size_t const str1_len,
const char * const str2,
size_t const str2_len,
xmlrpc_value ** const resultPP) {
/* Concatenate the two strings. */
char * buffer;
buffer = (char*) malloc(str1_len + str2_len);
if (!buffer) {
xmlrpc_env_set_fault(envP, 1,
"Couldn't allocate concatenated string");
} else {
memcpy(buffer, str1, str1_len);
memcpy(&buffer[str1_len], str2, str2_len);
*resultPP = xmlrpc_build_value(envP, "s#",
buffer, str1_len + str2_len);
free(buffer);
}
}
static xmlrpc_value *
moderate_array(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const user_data) {
xmlrpc_value * retval;
xmlrpc_value * arrayP;
/* Parse our argument array. */
xmlrpc_decompose_value(envP, paramArrayP, "(A)", &arrayP);
if (!envP->fault_occurred) {
int const size = xmlrpc_array_size(envP, arrayP);
if (!envP->fault_occurred) {
/* Get our first string. */
xmlrpc_value * const firstItemP =
xmlrpc_array_get_item(envP, arrayP, 0);
if (!envP->fault_occurred) {
const char * str1;
size_t str1_len;
xmlrpc_read_string_lp(envP, firstItemP, &str1_len, &str1);
if (!envP->fault_occurred) {
/* Get our last string. */
xmlrpc_value * const lastItemP =
xmlrpc_array_get_item(envP, arrayP, size - 1);
if (!envP->fault_occurred) {
const char * str2;
size_t str2_len;
xmlrpc_read_string_lp(envP, lastItemP,
&str2_len, &str2);
if (!envP->fault_occurred) {
concatenate(envP, str1, str1_len, str2, str2_len,
&retval);
free((char*)str2);
}
}
free((char*)str1);
}
}
}
xmlrpc_DECREF(arrayP);
}
return retval;
}
/*=========================================================================
** validator1.nestedStructTest
**=========================================================================
*/
static xmlrpc_value *
nested_struct(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const user_data) {
xmlrpc_value * yearsP;
xmlrpc_value * retval;
/* Parse our argument array. */
xmlrpc_decompose_value(envP, paramArrayP, "(S)", &yearsP);
if (envP->fault_occurred)
retval = NULL;
else {
/* Get values of larry, moe and curly for 2000-04-01. */
xmlrpc_int32 larry, moe, curly;
xmlrpc_decompose_value(envP, yearsP,
"{s:{s:{s:{s:i,s:i,s:i,*},*},*},*}",
"2000", "04", "01",
"larry", &larry,
"moe", &moe,
"curly", &curly);
if (envP->fault_occurred)
retval = NULL;
else
retval = xmlrpc_build_value(envP, "i", larry + moe + curly);
xmlrpc_DECREF(yearsP);
}
return retval;
}
/*=========================================================================
** validator1.simpleStructReturnTest
**=========================================================================
*/
static xmlrpc_value *
struct_return(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const user_data) {
xmlrpc_int32 i;
xmlrpc_decompose_value(envP, paramArrayP, "(i)", &i);
RETURN_IF_FAULT(envP);
return xmlrpc_build_value(envP, "{s:i,s:i,s:i}",
"times10", (xmlrpc_int32) i * 10,
"times100", (xmlrpc_int32) i * 100,
"times1000", (xmlrpc_int32) i * 1000);
}
/*=========================================================================
** main
**=========================================================================
*/
int main(int const argc,
const char ** const argv) {
xmlrpc_server_abyss_parms serverparm;
xmlrpc_registry * registryP;
xmlrpc_env env;
if (argc-1 != 1) {
fprintf(stderr, "You must specify 1 argument: The TCP port "
"number on which the server will accept connections "
"for RPCs. You specified %d arguments.\n", argc-1);
exit(1);
}
xmlrpc_env_init(&env);
registryP = xmlrpc_registry_new(&env);
xmlrpc_registry_add_method(
&env, registryP, NULL, "validator1.arrayOfStructsTest",
&array_of_structs, NULL);
xmlrpc_registry_add_method(
&env, registryP, NULL, "validator1.countTheEntities",
&count_entities, NULL);
xmlrpc_registry_add_method(
&env, registryP, NULL, "validator1.easyStructTest",
&easy_struct, NULL);
xmlrpc_registry_add_method(
&env, registryP, NULL, "validator1.echoStructTest",
&echo_struct, NULL);
xmlrpc_registry_add_method(
&env, registryP, NULL, "validator1.manyTypesTest",
&many_types, NULL);
xmlrpc_registry_add_method(
&env, registryP, NULL, "validator1.moderateSizeArrayCheck",
&moderate_array, NULL);
xmlrpc_registry_add_method(
&env, registryP, NULL, "validator1.nestedStructTest",
&nested_struct, NULL);
xmlrpc_registry_add_method(
&env, registryP, NULL, "validator1.simpleStructReturnTest",
&struct_return, NULL);
serverparm.config_file_name = NULL;
serverparm.registryP = registryP;
serverparm.port_number = atoi(argv[1]);
serverparm.log_file_name = NULL;
printf("Running XML-RPC server...\n");
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(log_file_name));
/* This never gets executed. */
return 0;
}
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.