You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: soap-admin.c,v 1.4 2006/05/15 06:45:07 m0gg Exp $
  3. *
  4. * CSOAP Project: A SOAP client/server library in C
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ayaz@jprogrammer.net
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <nanohttp/nanohttp-server.h>
  28. #include "soap-router.h"
  29. #include "soap-server.h"
  30. #include "soap-admin.h"
  31. #define SOAP_ADMIN_QUERY_ROUTERS "routers"
  32. #define SOAP_ADMIN_QUERY_ROUTER "router"
  33. #define SOAP_ADMIN_QUERY_SERVICES "services"
  34. static void
  35. _soap_admin_send_title(httpd_conn_t *conn, const char *title)
  36. {
  37. httpd_send_header(conn, 200, "OK");
  38. http_output_stream_write_string(conn->out,
  39. "<html><head><style>");
  40. http_output_stream_write_string(conn->out,
  41. ".logo {"
  42. " color: #005177;"
  43. " background-color: transparent;"
  44. " font-family: Calligraphic, arial, sans-serif;"
  45. " font-size: 36px;"
  46. "}");
  47. http_output_stream_write_string(conn->out,
  48. "</style></head><body><span class=\"logo\">csoap</span> ");
  49. http_output_stream_write_string(conn->out, title);
  50. http_output_stream_write_string(conn->out, "<hr />");
  51. }
  52. static void
  53. _soap_admin_list_routers(httpd_conn_t *conn)
  54. {
  55. SoapRouterNode *node;
  56. char buffer[1024];
  57. _soap_admin_send_title(conn, "Available routers");
  58. http_output_stream_write_string(conn->out, "<ul>");
  59. for (node = soap_server_get_routers(); node; node = node->next)
  60. {
  61. sprintf(buffer, "<li><a href=\"?" SOAP_ADMIN_QUERY_ROUTER "=%s\">%s</a> - <a href=\"%s\">[Service Description]</a></li>",
  62. node->context, node->context, node->context);
  63. http_output_stream_write_string(conn->out, buffer);
  64. }
  65. http_output_stream_write_string(conn->out, "</ul>");
  66. http_output_stream_write_string(conn->out, "</body></html>");
  67. }
  68. static void
  69. _soap_admin_list_services(httpd_conn_t *conn, const char *routername)
  70. {
  71. SoapRouter *router;
  72. SoapServiceNode *node;
  73. char buffer[1024];
  74. sprintf(buffer, "Listing Services for Router <b>%s</b>", routername);
  75. _soap_admin_send_title(conn, buffer);
  76. router = soap_server_find_router(routername);
  77. if (!router)
  78. {
  79. http_output_stream_write_string(conn->out, "Router not found!");
  80. http_output_stream_write_string(conn->out, "</body></html>");
  81. return;
  82. }
  83. node = router->service_head;
  84. http_output_stream_write_string(conn->out, "<ul>");
  85. while (node)
  86. {
  87. sprintf(buffer, "<li> [%s] (%s) </li>",
  88. node->service->urn,
  89. node->service->method);
  90. http_output_stream_write_string(conn->out, buffer);
  91. node = node->next;
  92. }
  93. http_output_stream_write_string(conn->out, "</ul>");
  94. http_output_stream_write_string(conn->out, "</body></html>");
  95. }
  96. static void
  97. _soap_admin_handle_get(httpd_conn_t * conn, hrequest_t * req)
  98. {
  99. char *param;
  100. if ((param = hpairnode_get_ignore_case(req->query, SOAP_ADMIN_QUERY_ROUTERS)))
  101. {
  102. _soap_admin_list_routers(conn);
  103. }
  104. else if ((param = hpairnode_get_ignore_case(req->query, SOAP_ADMIN_QUERY_ROUTER)))
  105. {
  106. _soap_admin_list_services(conn, param);
  107. }
  108. else
  109. {
  110. _soap_admin_send_title(conn, "Welcome to the admin site");
  111. http_output_stream_write_string(conn->out, "<ul>");
  112. http_output_stream_write_string(conn->out,
  113. "<li><a href=\"?" SOAP_ADMIN_QUERY_ROUTERS "\">Routers</a></li>");
  114. http_output_stream_write_string(conn->out, "</ul>");
  115. http_output_stream_write_string(conn->out, "</body></html>");
  116. }
  117. }
  118. static void
  119. _soap_admin_entry(httpd_conn_t * conn, hrequest_t * req)
  120. {
  121. if (req->method == HTTP_REQUEST_GET)
  122. {
  123. _soap_admin_handle_get(conn, req);
  124. }
  125. else
  126. {
  127. httpd_send_header(conn, 200, "OK");
  128. http_output_stream_write_string(conn->out,
  129. "<html>"
  130. "<head>"
  131. "</head>"
  132. "<body>"
  133. "<h1>Sorry!</h1>"
  134. "<hr />"
  135. "<div>POST Service is not implemented now. Use your browser</div>"
  136. "</body>"
  137. "</html>");
  138. }
  139. }
  140. herror_t
  141. soap_admin_init_args(int argc, char **argv)
  142. {
  143. int i;
  144. for (i=0; i<argc; i++) {
  145. if (!strcmp(argv[i], CSOAP_ENABLE_ADMIN)) {
  146. httpd_register("/csoap", _soap_admin_entry);
  147. break;
  148. }
  149. }
  150. return H_OK;
  151. }