wn_module_asp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * File : wn_module_asp.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 <rtthread.h>
  25. #ifdef RT_USING_DFS
  26. #include <dfs_posix.h>
  27. #endif
  28. #ifdef RT_USING_SAL
  29. #include <sys/socket.h>
  30. #else
  31. #include <lwip/netif.h>
  32. #endif /* RT_USING_SAL */
  33. #include <webnet.h>
  34. #include <wn_module.h>
  35. #include <wn_utils.h>
  36. #ifdef WEBNET_USING_ASP
  37. struct webnet_asp_variable
  38. {
  39. char* name;
  40. void (*handler)(struct webnet_session* session);
  41. };
  42. static struct webnet_asp_variable* _webnet_asp_vars = RT_NULL;
  43. rt_uint32_t _webnet_asp_vars_count = 0;
  44. void webnet_asp_add_var(const char* name, void (*handler)(struct webnet_session* session))
  45. {
  46. if (_webnet_asp_vars == RT_NULL)
  47. {
  48. _webnet_asp_vars_count = 1;
  49. _webnet_asp_vars = (struct webnet_asp_variable*)wn_malloc (sizeof(struct webnet_asp_variable) *
  50. _webnet_asp_vars_count);
  51. }
  52. else
  53. {
  54. _webnet_asp_vars_count += 1;
  55. _webnet_asp_vars = (struct webnet_asp_variable*) wn_realloc (_webnet_asp_vars, sizeof(struct webnet_asp_variable) *
  56. _webnet_asp_vars_count);
  57. }
  58. RT_ASSERT(_webnet_asp_vars != RT_NULL);
  59. _webnet_asp_vars[_webnet_asp_vars_count - 1].name = wn_strdup(name);
  60. _webnet_asp_vars[_webnet_asp_vars_count - 1].handler = handler;
  61. }
  62. RTM_EXPORT(webnet_asp_add_var);
  63. static void _default_asp_handler(struct webnet_session* session, const char* name)
  64. {
  65. if (strncmp(name, "VERION", 6) == 0)
  66. {
  67. webnet_session_printf(session, "WebNet 1.0.0");
  68. }
  69. else if (strncmp(name, "REMOTE_ADDR", 11) == 0)
  70. {
  71. webnet_session_printf(session, "%s", inet_ntoa(session->cliaddr.sin_addr));
  72. }
  73. else if (strncmp(name, "REMOTE_PORT", 11) == 0)
  74. {
  75. webnet_session_printf(session, "%d", ntohs(session->cliaddr.sin_port));
  76. }
  77. else if (strncmp(name, "SERVER_ADDR", 11) == 0)
  78. {
  79. #if defined(RT_USING_LWIP) && !defined(RT_USING_SAL)
  80. webnet_session_printf(session, "%s", inet_ntoa(*(struct in_addr*)&(netif_default->ip_addr)));
  81. #endif
  82. }
  83. else if (strncmp(name, "SERVER_PORT", 11) == 0)
  84. {
  85. webnet_session_printf(session, "%d", WEBNET_PORT);
  86. }
  87. else if (strncmp(name, "DOCUMENT_ROOT", 13) == 0)
  88. {
  89. webnet_session_printf(session, "%s", webnet_get_root());
  90. }
  91. else if (strncmp(name, "SERVER", 6) == 0)
  92. {
  93. webnet_session_printf(session, "RT-Thread/WebNet");
  94. }
  95. else if (strncmp(name, "HOST", 4) == 0)
  96. {
  97. webnet_session_printf(session, "WebNet");
  98. }
  99. else if (strncmp(name, "DATE", 4) == 0)
  100. {
  101. webnet_session_printf(session, "2011/08/01");
  102. }
  103. else if (strncmp(name, "USER_AGENT", 10) == 0)
  104. {
  105. webnet_session_printf(session, "%s", session->request->user_agent);
  106. }
  107. else if (strncmp(name, "COOKIE", 6) == 0)
  108. {
  109. webnet_session_printf(session, "%s", session->request->cookie);
  110. }
  111. else if (strncmp(name, "MEMUSAGE", 8) == 0)
  112. {
  113. rt_uint32_t total, used, max_used;
  114. //rt_memory_info(&total, &used, &max_used);
  115. total = 1024*32;
  116. used = 1024*16;
  117. max_used = 1024*24;
  118. webnet_session_printf(session, "current %d/maximal used %d/total %d", used, max_used, total);
  119. }
  120. else if (strncmp(name, "TICK", 4) == 0)
  121. {
  122. rt_uint32_t tick;
  123. rt_uint32_t hour, min, second;
  124. tick = rt_tick_get()/RT_TICK_PER_SECOND;
  125. second = tick % 60;
  126. min = (tick/60) % 60;
  127. hour = tick / (60 * 60);
  128. webnet_session_printf(session, "%02d:%02d:%02d", hour, min, second);
  129. }
  130. }
  131. static void _webnet_asp_dofile(struct webnet_session* session, int fd)
  132. {
  133. char *asp_begin, *asp_end;
  134. char *offset, *end;
  135. char *buffer;
  136. rt_uint32_t length, index;
  137. /* get file length */
  138. length = lseek(fd, 0, SEEK_END);
  139. lseek(fd, 0, SEEK_SET);
  140. /* allocate read buffer */
  141. buffer = (char*) wn_malloc (length);
  142. if (buffer == RT_NULL)
  143. {
  144. session->request->result_code = 500;
  145. return;
  146. }
  147. /* write page header */
  148. webnet_session_set_header(session, "text/html", 200, "OK", -1);
  149. /* read file to buffer */
  150. if (read(fd, buffer, (size_t)length) != length) /* read failed */
  151. {
  152. wn_free(buffer);
  153. session->request->result_code = 500;
  154. return;
  155. }
  156. offset = buffer;
  157. end = buffer + length;
  158. while (offset < end)
  159. {
  160. /* get beginning of asp variable */
  161. asp_begin = strstr(offset, "<%");
  162. if (asp_begin == RT_NULL)
  163. {
  164. /* write content directly */
  165. webnet_session_write(session, (const rt_uint8_t*)offset, end - offset);
  166. break;
  167. }
  168. /* get end of aps variable */
  169. asp_end = strstr(asp_begin, "%>");
  170. if (asp_end == RT_NULL)
  171. {
  172. /* write content directly */
  173. webnet_session_write(session, (const rt_uint8_t*)offset, end - offset);
  174. break;
  175. }
  176. else
  177. {
  178. /* write content */
  179. webnet_session_write(session, (const rt_uint8_t*)offset, asp_begin - offset);
  180. offset = asp_begin + 2;
  181. while ((*offset == ' ') || (*offset == '\t')) offset ++;
  182. /* extrace asp variable */
  183. for (index = 0; index < _webnet_asp_vars_count; index ++)
  184. {
  185. if (str_begin_with(offset, _webnet_asp_vars[index].name))
  186. {
  187. /* found asp variable */
  188. _webnet_asp_vars[index].handler(session);
  189. break;
  190. }
  191. }
  192. if (index == _webnet_asp_vars_count)
  193. {
  194. /* use default handler */
  195. _default_asp_handler(session, offset);
  196. }
  197. /* move to the end of asp varaialbe */
  198. offset = asp_end + 2;
  199. }
  200. }
  201. /* release read buffer */
  202. wn_free(buffer);
  203. }
  204. int webnet_module_asp(struct webnet_session* session, int event)
  205. {
  206. struct webnet_request* request;
  207. RT_ASSERT(session != RT_NULL);
  208. request = session->request;
  209. RT_ASSERT(request != RT_NULL);
  210. if (event == WEBNET_EVENT_URI_POST)
  211. {
  212. int fd;
  213. /* check whether a asp file */
  214. if ((strstr(request->path, ".asp") != RT_NULL) ||
  215. (strstr(request->path, ".ASP") != RT_NULL))
  216. {
  217. /* try to open this file */
  218. fd = open(request->path, O_RDONLY, 0);
  219. if ( fd >= 0)
  220. {
  221. _webnet_asp_dofile(session, fd);
  222. close(fd);
  223. return WEBNET_MODULE_FINISHED;
  224. }
  225. else
  226. {
  227. /* no this file */
  228. request->result_code = 404;
  229. return WEBNET_MODULE_FINISHED;
  230. }
  231. }
  232. else
  233. {
  234. /* try index.asp */
  235. char *asp_filename;
  236. asp_filename = (char*) wn_malloc (WEBNET_PATH_MAX);
  237. if (asp_filename != RT_NULL)
  238. {
  239. rt_snprintf(asp_filename, WEBNET_PATH_MAX, "%s/index.asp", request->path);
  240. fd = open(asp_filename, O_RDONLY, 0);
  241. if (fd >= 0)
  242. {
  243. wn_free(asp_filename);
  244. _webnet_asp_dofile(session, fd);
  245. close(fd);
  246. return WEBNET_MODULE_FINISHED;
  247. }
  248. }
  249. wn_free(asp_filename);
  250. }
  251. }
  252. return WEBNET_MODULE_CONTINUE;
  253. }
  254. #endif /* WEBNET_USING_ASP */