soap-router.c

Go to the documentation of this file.
00001 /******************************************************************
00002 *  $Id: soap-router.c,v 1.14 2006/11/25 15:06:57 m0gg Exp $
00003 *
00004 * CSOAP Project:  A SOAP client/server library in C
00005 * Copyright (C) 2003  Ferhat Ayaz
00006 *
00007 * This library is free software; you can redistribute it and/or
00008 * modify it under the terms of the GNU Library General Public
00009 * License as published by the Free Software Foundation; either
00010 * version 2 of the License, or (at your option) any later version.
00011 *
00012 * This library is distributed in the hope that it will be useful,
00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 * Library General Public License for more details.
00016 *
00017 * You should have received a copy of the GNU Library General Public
00018 * License along with this library; if not, write to the
00019 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020 * Boston, MA  02111-1307, USA.
00021 * 
00022 * Email: ayaz@jprogrammer.net
00023 ******************************************************************/
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #ifdef HAVE_STDIO_H
00029 #include <stdio.h>
00030 #endif
00031 
00032 #ifdef HAVE_STRING_H
00033 #include <string.h>
00034 #endif
00035 
00036 #ifdef HAVE_ERRNO_H
00037 #include <errno.h>
00038 #endif
00039 
00040 #include <libxml/tree.h>
00041 
00042 #include <nanohttp/nanohttp-error.h>
00043 #include <nanohttp/nanohttp-logging.h>
00044 
00045 #include "soap-fault.h"
00046 #include "soap-ctx.h"
00047 #include "soap-service.h"
00048 
00049 #include "soap-router.h"
00050 
00051 struct SoapRouter *
00052 soap_router_new(void)
00053 {
00054   struct SoapRouter *router;
00055 
00056   if (!(router = (struct SoapRouter *) malloc(sizeof(struct SoapRouter))))
00057   {
00058     log_error2("malloc failed (%s)", strerror(errno));
00059     return NULL;
00060   }
00061   memset(router, 0, sizeof(struct SoapRouter));
00062 
00063   return router;
00064 }
00065 
00066 herror_t
00067 soap_router_register_service(struct SoapRouter *router, SoapServiceFunc func, const char *method, const char *urn)
00068 {
00069   SoapService *service;
00070 
00071   log_verbose4("registering service (router=%p, method=\"%s\", urn=\"%s\")", router, method, urn);
00072 
00073   if (!(service = soap_service_new(urn, method, func)))
00074     return herror_new("soap_router_register_service", 0, "soap_service_new failed");
00075 
00076   if (router->service_tail == NULL)
00077   {
00078     router->service_head =
00079       router->service_tail = soap_service_node_new(service, NULL);
00080   }
00081   else
00082   {
00083     router->service_tail->next = soap_service_node_new(service, NULL);
00084     router->service_tail = router->service_tail->next;
00085   }
00086 
00087   return H_OK;
00088 }
00089 
00090 void
00091 soap_router_register_security(struct SoapRouter * router, soap_auth auth)
00092 {
00093   router->auth = auth;
00094 
00095   return;
00096 }
00097 
00098 void
00099 soap_router_register_description(struct SoapRouter * router, xmlDocPtr description)
00100 {
00101   if (router->description)
00102     xmlFreeDoc(router->description);
00103 
00104   router->description = xmlCopyDoc(description, 1);
00105 
00106   return;
00107 }
00108 
00109 herror_t
00110 soap_router_register_default_service(struct SoapRouter *router, SoapServiceFunc func, const char *method, const char *urn) {
00111 
00112   SoapService *service;
00113 
00114   if (!(service = soap_service_new(urn, method, func)))
00115     return herror_new("soap_router_register_default_service", 0, "soap_service_new failed");
00116 
00117   if (router->service_tail == NULL)
00118   {
00119     router->service_head = router->service_tail = soap_service_node_new(service, NULL);
00120   }
00121   else
00122   {
00123     router->service_tail->next = soap_service_node_new(service, NULL);
00124     router->service_tail = router->service_tail->next;
00125   }
00126 
00127   router->default_service = service;
00128 
00129   return H_OK;
00130 }
00131 
00132 SoapService *
00133 soap_router_find_service(struct SoapRouter *router, const char *urn, const char *method)
00134 {
00135   SoapServiceNode *node;
00136 
00137   if (router == NULL)
00138   {
00139     log_verbose1("router is null");
00140     return NULL;
00141   }
00142 
00143   if (urn == NULL)
00144   {
00145     log_verbose1("URN is null");
00146     return NULL;
00147   }
00148 
00149   if (method == NULL)
00150   {
00151     log_verbose1("method is null"); 
00152     return NULL;
00153   }
00154 
00155   log_verbose2("router = %p", router);
00156   log_verbose2("router->service_head = %p", router->service_head);
00157 
00158   node = router->service_head;
00159 
00160   while (node)
00161   {
00162     if (node->service && node->service->urn && node->service->method)
00163     {
00164       log_verbose4("checking service (node=%p, method=\"%s\", urn=\"%s\")", node->service, node->service->method, node->service->urn);
00165       if (!strcmp(node->service->urn, urn) && !strcmp(node->service->method, method))
00166         return node->service;
00167     }
00168     node = node->next;
00169   }
00170   return router->default_service;
00171 }
00172 
00173 void
00174 soap_router_free(struct SoapRouter * router)
00175 {
00176   SoapServiceNode *node;
00177   log_verbose2("enter: router=%p", router);
00178 
00179   if (!router)
00180     return;
00181 
00182   while (router->service_head)
00183   {
00184     node = router->service_head->next;
00185     /* log_verbose2("soap_service_free(%p)\n",
00186        router->service_head->service); */
00187     soap_service_free(router->service_head->service);
00188     free(router->service_head);
00189     router->service_head = node;
00190   }
00191   if (router->description)
00192     xmlFreeDoc(router->description);
00193 
00194   free(router);
00195   log_verbose1("leave with success");
00196 
00197   return;
00198 }

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