123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- /* 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).
- ============================================================================*/
- #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;
- }
|