wn_module_cgi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * File : wn_module_cgi.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This software is dual-licensed: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. For the terms of this
  9. * license, see <http://www.gnu.org/licenses/>.
  10. *
  11. * You are free to use this software under the terms of the GNU General
  12. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  13. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * Alternatively for commercial application, you can contact us
  17. * by email <business@rt-thread.com> for commercial license.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2011-08-02 Bernard the first version
  22. */
  23. #include <string.h>
  24. #include <webnet.h>
  25. #include <wn_module.h>
  26. #include <wn_utils.h>
  27. #ifdef RT_USING_DFS
  28. #if RT_VER_NUM >= 0x40100
  29. #include <strings.h> /* fix strncasecmp */
  30. #else
  31. #include <dfs_posix.h>
  32. #endif /*RT_VER_NUM >= 0x40100*/
  33. #endif
  34. #ifdef WEBNET_USING_CGI
  35. #define CGI_ROOT_PATH_MAX 64
  36. static char _cgi_root[CGI_ROOT_PATH_MAX] = {0};
  37. struct webnet_cgi_item
  38. {
  39. const char* name;
  40. void (*handler)(struct webnet_session* session);
  41. };
  42. static struct webnet_cgi_item* _cgi_items = RT_NULL;
  43. static rt_uint32_t _cgi_count = 0;
  44. void webnet_cgi_set_root(const char* root)
  45. {
  46. if (strlen(root) > CGI_ROOT_PATH_MAX)
  47. {
  48. RT_ASSERT(0);
  49. return;
  50. }
  51. strncpy(_cgi_root, root, strlen(root));
  52. if (_cgi_root[strlen(_cgi_root)] != '/')
  53. {
  54. _cgi_root[strlen(_cgi_root) + 1] = '/';
  55. _cgi_root[strlen(_cgi_root) + 1] = '\0';
  56. }
  57. }
  58. RTM_EXPORT(webnet_cgi_set_root);
  59. void webnet_cgi_register(const char* name, void (*handler)(struct webnet_session* session))
  60. {
  61. if (_cgi_items == RT_NULL)
  62. {
  63. _cgi_count = 1;
  64. _cgi_items = (struct webnet_cgi_item*) wn_malloc (sizeof(struct webnet_cgi_item) * _cgi_count);
  65. }
  66. else
  67. {
  68. _cgi_count += 1;
  69. _cgi_items = (struct webnet_cgi_item*) wn_realloc (_cgi_items, sizeof(struct webnet_cgi_item) * _cgi_count);
  70. }
  71. RT_ASSERT(_cgi_items != RT_NULL);
  72. _cgi_items[_cgi_count - 1].name = name;
  73. _cgi_items[_cgi_count - 1].handler = handler;
  74. }
  75. RTM_EXPORT(webnet_cgi_register);
  76. int webnet_module_cgi(struct webnet_session* session, int event)
  77. {
  78. if (event == WEBNET_EVENT_INIT)
  79. {
  80. /* set default cgi path */
  81. if (_cgi_root[0] == '\0')
  82. {
  83. strcpy(_cgi_root, "/cgi-bin/");
  84. }
  85. }
  86. else if (event == WEBNET_EVENT_URI_PHYSICAL)
  87. {
  88. struct webnet_request* request;
  89. char *cgi_path = RT_NULL;
  90. RT_ASSERT(session != RT_NULL);
  91. request = session->request;
  92. RT_ASSERT(request != RT_NULL);
  93. /* check whether a cgi request */
  94. cgi_path = strstr(request->path, _cgi_root);
  95. if (cgi_path != RT_NULL)
  96. {
  97. char* cgi_name;
  98. rt_uint32_t index;
  99. cgi_name = cgi_path + strlen(_cgi_root);
  100. for (index = 0; index < _cgi_count; index ++)
  101. {
  102. if ((strlen(cgi_name) == strlen(_cgi_items[index].name))
  103. && strncasecmp(cgi_name, _cgi_items[index].name, strlen(_cgi_items[index].name)) == 0)
  104. {
  105. /* found it */
  106. _cgi_items[index].handler(session);
  107. return WEBNET_MODULE_FINISHED;
  108. }
  109. }
  110. /* set 404 not found error */
  111. request->result_code = 404;
  112. }
  113. }
  114. return WEBNET_MODULE_CONTINUE;
  115. }
  116. #endif /* WEBNET_USING_CGI */