soap-admin.c

Go to the documentation of this file.
00001 /******************************************************************
00002 *  $Id: soap-admin.c,v 1.11 2006/12/31 17:24:22 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_SYS_TIME_H
00029 #include <sys/time.h>
00030 #endif
00031 
00032 #ifdef HAVE_STDIO_H
00033 #include <stdio.h>
00034 #endif
00035 
00036 #ifdef HAVE_STRING_H
00037 #include <string.h>
00038 #endif
00039 
00040 #ifdef HAVE_NETINET_IN_H
00041 #include <netinet/in.h>
00042 #endif
00043 
00044 #include <libxml/tree.h>
00045 #include <libxml/uri.h>
00046 
00047 #include <nanohttp/nanohttp-error.h>
00048 #include <nanohttp/nanohttp-common.h>
00049 #include <nanohttp/nanohttp-stream.h>
00050 #include <nanohttp/nanohttp-request.h>
00051 #include <nanohttp/nanohttp-server.h>
00052 #include <nanohttp/nanohttp-admin.h>
00053 
00054 #include "soap-env.h"
00055 #include "soap-ctx.h"
00056 #include "soap-service.h"
00057 #include "soap-router.h"
00058 #include "soap-server.h"
00059 #include "soap-admin.h"
00060 
00061 static void
00062 _soap_admin_send_title(httpd_conn_t *conn, const char *title)
00063 {
00064   httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE);
00065 
00066   http_output_stream_write_string(conn->out,
00067    "<html>"
00068      "<head>");
00069 
00070   http_output_stream_write_string(conn->out,
00071      "<style>"
00072        ".logo {"
00073        " color: #005177;"
00074        " background-color: transparent;"
00075        " font-family: Calligraphic, arial, sans-serif;"
00076        " font-size: 36px;"
00077        "}"
00078      "</style>");
00079 
00080   http_output_stream_write_string(conn->out,
00081    "</head>"
00082    "<body>"
00083      "<span class=\"logo\">csoap</span> ");
00084   http_output_stream_write_string(conn->out, title);
00085   http_output_stream_write_string(conn->out, "<hr />");
00086 
00087   return;
00088 }
00089 
00090 static inline void
00091 _soap_admin_send_footer(httpd_conn_t *conn)
00092 {
00093   http_output_stream_write_string(conn->out, "</body></html>");
00094 
00095   return;
00096 }
00097 
00098 static void
00099 _soap_admin_list_routers(httpd_conn_t *conn)
00100 {
00101   SoapRouterNode *node;
00102   char buffer[1024];
00103 
00104   _soap_admin_send_title(conn, "Available routers");
00105 
00106   http_output_stream_write_string(conn->out, "<ul>");
00107   for (node = soap_server_get_routers(); node; node = node->next)
00108   {
00109     sprintf(buffer,
00110       "<li>"
00111         "<a href=\"?" CSOAP_ADMIN_QUERY_ROUTER "=%s\">%s</a> - "
00112         "<a href=\"%s\">[Service Description]</a> - "
00113         "<a href=\"../nhttp?" NHTTPD_ADMIN_QUERY_STATISTICS "=%s\">[Statistics]</a>"
00114       "</li>",
00115       node->context, node->context, node->context, node->context);
00116     http_output_stream_write_string(conn->out, buffer);
00117   }
00118   http_output_stream_write_string(conn->out, "</ul>");
00119   
00120   _soap_admin_send_footer(conn);
00121 
00122   return;
00123 }
00124 
00125 static void
00126 _soap_admin_list_services(httpd_conn_t *conn, const char *routername)
00127 {
00128   struct SoapRouter *router;
00129   SoapServiceNode *node;
00130   char buffer[1024];
00131   
00132   sprintf(buffer, "Listing Services for Router <b>%s</b>", routername);
00133   _soap_admin_send_title(conn, buffer);
00134 
00135   router = soap_server_find_router(routername);
00136   if (!router)
00137   {
00138     http_output_stream_write_string(conn->out, "Router not found!");
00139     http_output_stream_write_string(conn->out, "</body></html>");
00140     return;
00141   }
00142 
00143   node = router->service_head;
00144 
00145   http_output_stream_write_string(conn->out, "<ul>");
00146   for (node = router->service_head; node; node = node->next)
00147   {
00148     switch (node->service->status)
00149     {
00150       case CSOAP_SERVICE_DOWN:
00151         sprintf(buffer,
00152           "<li>"
00153             "%s (%s) <a href=\"?" CSOAP_ADMIN_QUERY_ACTIVATE "=%s&amp;" CSOAP_ADMIN_URN "=%s\">[Activate]</a>"
00154           "</li>",
00155           node->service->urn,
00156           node->service->method,
00157           node->service->method,
00158           node->service->urn);
00159         break;
00160       case CSOAP_SERVICE_UP:
00161       default:
00162         sprintf(buffer,
00163         "<li>"
00164           "%s (%s) <a href=\"?" CSOAP_ADMIN_QUERY_PASSIVATE "=%s&amp;" CSOAP_ADMIN_URN "=%s\">[Passivate]</a>"
00165         "</li>",
00166         node->service->urn,
00167         node->service->method,
00168         node->service->method,
00169         node->service->urn);
00170         break;
00171     }
00172     http_output_stream_write_string(conn->out, buffer);
00173   }
00174   http_output_stream_write_string(conn->out, "</ul>");
00175 
00176   _soap_admin_send_footer(conn);
00177 
00178   return;
00179 }
00180 
00181 static void
00182 _soap_admin_handle_get(httpd_conn_t * conn, struct hrequest_t * req)
00183 {
00184   char *param;
00185 
00186   if ((param = hpairnode_get_ignore_case(req->query, CSOAP_ADMIN_QUERY_ROUTERS)))
00187   {
00188     _soap_admin_list_routers(conn);
00189   }
00190   else if ((param = hpairnode_get_ignore_case(req->query, CSOAP_ADMIN_QUERY_ROUTER)))
00191   {
00192     _soap_admin_list_services(conn, param);
00193   }
00194   else if ((param = hpairnode_get_ignore_case(req->query, CSOAP_ADMIN_QUERY_ACTIVATE)))
00195   {
00196     _soap_admin_send_title(conn, "Not implemented");
00197     _soap_admin_send_footer(conn);
00198     // _soap_admin_activate_service(conn, param);
00199   }
00200   else if ((param = hpairnode_get_ignore_case(req->query, CSOAP_ADMIN_QUERY_PASSIVATE)))
00201   {
00202     _soap_admin_send_title(conn, "Not implemented");
00203     _soap_admin_send_footer(conn);
00204     // _soap_admin_passivate_service(conn, param);
00205   }
00206   else
00207   {
00208     _soap_admin_send_title(conn, "Welcome to the admin site");
00209 
00210     http_output_stream_write_string(conn->out,
00211       "<ul>"
00212         "<li><a href=\"?" CSOAP_ADMIN_QUERY_ROUTERS "\">Routers</a></li>"
00213         "<li><a href=\"../inspection.wsil\">inspection.wsil</a> (try: -CSOAPwsil)</li>"
00214         "<li><a href=\"../nhttp\">nanoHTTP</a></li>"
00215       "</ul>");
00216 
00217     _soap_admin_send_footer(conn);
00218   }
00219 
00220   return;
00221 }
00222 
00223 
00224 static void
00225 _soap_admin_entry(httpd_conn_t * conn, struct hrequest_t * req)
00226 {
00227   if (req->method == HTTP_REQUEST_GET)
00228   {
00229     _soap_admin_handle_get(conn, req);
00230   }
00231   else
00232   {
00233     httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE);
00234     http_output_stream_write_string(conn->out,
00235               "<html>"
00236                   "<head>"
00237                   "</head>"
00238                   "<body>"
00239                       "<h1>Sorry!</h1>"
00240                       "<hr />"
00241                       "<div>POST Service is not implemented now. Use your browser</div>"
00242                   "</body>"
00243               "</html>");
00244   }
00245 
00246   return;
00247 }
00248 
00249 
00250 herror_t
00251 soap_admin_init_args(int argc, char **argv)
00252 {
00253   int i;
00254 
00255   for (i=0; i<argc; i++) {
00256 
00257     if (!strcmp(argv[i], CSOAP_ENABLE_ADMIN)) {
00258 
00259       httpd_register("/csoap", _soap_admin_entry);
00260       break;
00261     }
00262   }
00263 
00264   return H_OK;
00265 }

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