wn_module_cgi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <dfs_posix.h>
  29. #endif
  30. #ifdef WEBNET_USING_CGI
  31. #define CGI_ROOT_PATH_MAX 64
  32. static char _cgi_root[CGI_ROOT_PATH_MAX] = {0};
  33. struct webnet_cgi_item
  34. {
  35. const char* name;
  36. void (*handler)(struct webnet_session* session);
  37. };
  38. static struct webnet_cgi_item* _cgi_items = RT_NULL;
  39. static rt_uint32_t _cgi_count = 0;
  40. void webnet_cgi_set_root(const char* root)
  41. {
  42. if (strlen(root) > CGI_ROOT_PATH_MAX)
  43. {
  44. RT_ASSERT(0);
  45. return;
  46. }
  47. strncpy(_cgi_root, root, strlen(root));
  48. if (_cgi_root[strlen(_cgi_root)] != '/')
  49. {
  50. _cgi_root[strlen(_cgi_root) + 1] = '/';
  51. _cgi_root[strlen(_cgi_root) + 1] = '\0';
  52. }
  53. }
  54. RTM_EXPORT(webnet_cgi_set_root);
  55. void webnet_cgi_register(const char* name, void (*handler)(struct webnet_session* session))
  56. {
  57. if (_cgi_items == RT_NULL)
  58. {
  59. _cgi_count = 1;
  60. _cgi_items = (struct webnet_cgi_item*) wn_malloc (sizeof(struct webnet_cgi_item) * _cgi_count);
  61. }
  62. else
  63. {
  64. _cgi_count += 1;
  65. _cgi_items = (struct webnet_cgi_item*) wn_realloc (_cgi_items, sizeof(struct webnet_cgi_item) * _cgi_count);
  66. }
  67. RT_ASSERT(_cgi_items != RT_NULL);
  68. _cgi_items[_cgi_count - 1].name = name;
  69. _cgi_items[_cgi_count - 1].handler = handler;
  70. }
  71. RTM_EXPORT(webnet_cgi_register);
  72. int webnet_module_cgi(struct webnet_session* session, int event)
  73. {
  74. if (event == WEBNET_EVENT_INIT)
  75. {
  76. /* set default cgi path */
  77. if (_cgi_root[0] == '\0')
  78. {
  79. strcpy(_cgi_root, "/cgi-bin/");
  80. }
  81. }
  82. else if (event == WEBNET_EVENT_URI_PHYSICAL)
  83. {
  84. struct webnet_request* request;
  85. char *cgi_path = RT_NULL;
  86. RT_ASSERT(session != RT_NULL);
  87. request = session->request;
  88. RT_ASSERT(request != RT_NULL);
  89. /* check whether a cgi request */
  90. cgi_path = strstr(request->path, _cgi_root);
  91. if (cgi_path != RT_NULL)
  92. {
  93. char* cgi_name;
  94. rt_uint32_t index;
  95. cgi_name = cgi_path + strlen(_cgi_root);
  96. for (index = 0; index < _cgi_count; index ++)
  97. {
  98. if ((strlen(cgi_name) == strlen(_cgi_items[index].name))
  99. && strncasecmp(cgi_name, _cgi_items[index].name, strlen(_cgi_items[index].name)) == 0)
  100. {
  101. /* found it */
  102. _cgi_items[index].handler(session);
  103. return WEBNET_MODULE_FINISHED;
  104. }
  105. }
  106. /* set 404 not found error */
  107. request->result_code = 404;
  108. }
  109. }
  110. return WEBNET_MODULE_CONTINUE;
  111. }
  112. #endif /* WEBNET_USING_CGI */