Howto write a SOAP server

Table of contents

Server initialization

 static const char const *url = "/csoapserver";
 static const char const *urn = "urn:examples";
 static const char const *method = "sayHello";

 int main(int argc, char **argv)
 {
   herror_t err;
   struct SoapRouter *router;

   if ((err = soap_server_init_args(argc, argv)) != H_OK)
   {
     printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err));
     herror_release(err);
     exit(1);
   }

Router creation

   if (!(router = soap_router_new()))
   {
     printf("soap_router_new failed (router == %p)\n", router);
     herror_release(err);
     exit(1);
   }

Service registration

   if ((err = soap_router_register_service(router, say_hello, method, urn)) != H_OK)
   {
     printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err));
     herror_release(err);
     exit(1);
   }

   if ((err = soap_server_register_router(router, url)))
   {
     printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err));
     herror_release(err);
     exit(1);
   }
   printf("router (%p) registered for \"%s\"\n", router, url);

Serving requests

   printf("press ctrl-c to shutdown\n");
   if ((err = soap_server_run()) != H_OK)
   {
     printf("%s(): %s [%d]\n", herror_func(err), herror_message(err), herror_code(err));
     herror_release(err);
     exit(1);
   }

Server cleanup

   soap_server_destroy();

   exit(0);
 }

Service function

 herror_t say_hello(struct SoapCtx *req, struct SoapCtx *res)
 {
   herror_t err;
   char *name;
   xmlNodePtr method, node;

   printf("processing service request\n");

   xmlDocFormatDump(stdout, req->env->root->doc, 1);

   if ((err = soap_env_new_with_response(req->env, &res->env)) != H_OK)
   {
     printf("soap_env_new_with_response failed (%s)\n", herror_message(err));
     return err;
   }
   printf("empty response created\n");

   if (!(method = soap_env_get_method(req->env)))
   {
     printf("soap_env_get_method failed\n");
     return herror_new("say_hello", 0, "There may be a bug in the library...");
   }
   printf("method found\n");

   printf("adding response content...\n");
   node = soap_xml_get_children(method);

   while (node)
   {
     name = (char *) xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
     soap_env_add_itemf(res->env, "xsd:string", "echo", "Hello '%s'", name);
     node = soap_xml_get_next(node);
     if (name)
       xmlFree(name);
   }
   printf("service request done\n");
   return H_OK;
 }

Generated on Thu Jan 25 23:36:04 2007 for csoap by  doxygen 1.4.6