wn_module_asp.c 8.3 KB

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