wn_module_lua.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * File : wn_module_lua.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. * 2013-11-23 aozima redirect lua print.
  23. */
  24. #include <string.h>
  25. #include <webnet.h>
  26. #include <wn_module.h>
  27. #ifdef RT_USING_DFS
  28. #include <dfs_posix.h>
  29. #endif
  30. #if defined(WEBNET_USING_LUA) && defined(RT_USING_LUA)
  31. #include <lua.h>
  32. #include <lualib.h>
  33. #include <lauxlib.h>
  34. static struct webnet_session* lua_session;
  35. static int print(lua_State *L)
  36. {
  37. int n = lua_gettop(L);
  38. int i;
  39. if(lua_session == RT_NULL)
  40. return 0;
  41. for (i=1; i<=n; i++)
  42. {
  43. if (lua_isstring(L,i))
  44. webnet_session_printf(lua_session, "%s", lua_tostring(L,i));
  45. else if (lua_isnumber(L, i))
  46. webnet_session_printf(lua_session, "%d", lua_tointeger(L,i));
  47. else if (lua_isnil(L, i))
  48. webnet_session_printf(lua_session, "%s", "nil");
  49. else if (lua_isboolean(L, i))
  50. webnet_session_printf(lua_session, "%s", lua_toboolean(L,i) ? "true" : "false");
  51. else
  52. webnet_session_printf(lua_session, "%s:%p", luaL_typename(L,i), lua_topointer(L, i));
  53. }
  54. return 0;
  55. }
  56. static void _webnet_lua_dofile(struct webnet_session* session, const char * path)
  57. {
  58. int ret = 0;
  59. lua_State *L = lua_open();
  60. if(L == RT_NULL)
  61. return;
  62. lua_session = session;
  63. // luaopen_base(L);
  64. // luaopen_table(L);
  65. // luaopen_io(L);
  66. // luaopen_os(L);
  67. // luaopen_string(L);
  68. // luaopen_math(L);
  69. // luaopen_debug(L);
  70. // luaopen_package(L);
  71. luaL_openlibs(L);
  72. lua_register(L, "print", print);
  73. ret = luaL_loadfile(L, path);
  74. if ( ret != 0 ) goto _exception;
  75. ret = lua_pcall(L,0, LUA_MULTRET,0) ;
  76. if ( ret != 0 ) goto _exception;
  77. _exception:
  78. if(ret != 0)
  79. {
  80. const char* fmt = "<html><head><title>%d %s</title></head><body>%d %s</body></html>\r\n";
  81. struct webnet_request* request;
  82. const char* title = "Internal Server Error";
  83. int code = 500;
  84. RT_ASSERT(session != RT_NULL);
  85. request = session->request;
  86. RT_ASSERT(request != RT_NULL);
  87. webnet_session_set_header(session, "text/html", request->result_code, title, -1);
  88. webnet_session_printf(session, fmt, request->result_code, title, code, lua_tostring(L,-1));
  89. request->result_code = 200; /* set code 200 to end the session. */
  90. }
  91. lua_close(L);
  92. lua_session = RT_NULL;
  93. return;
  94. }
  95. int webnet_module_lua(struct webnet_session* session, int event)
  96. {
  97. struct webnet_request* request;
  98. RT_ASSERT(session != RT_NULL);
  99. request = session->request;
  100. RT_ASSERT(request != RT_NULL);
  101. if (event == WEBNET_EVENT_URI_POST)
  102. {
  103. int fd;
  104. /* check whether a lua script */
  105. if ((strstr(request->path, ".lua") != RT_NULL) ||
  106. (strstr(request->path, ".LUA") != RT_NULL))
  107. {
  108. /* try to open this file */
  109. fd = open(request->path, O_RDONLY, 0);
  110. if ( fd >= 0)
  111. {
  112. close(fd);
  113. _webnet_lua_dofile(session, request->path);
  114. return WEBNET_MODULE_FINISHED;
  115. }
  116. else
  117. {
  118. /* no this file */
  119. request->result_code = 404;
  120. return WEBNET_MODULE_FINISHED;
  121. }
  122. }
  123. else
  124. {
  125. /* try index.lua */
  126. char *lua_filename;
  127. lua_filename = (char*) wn_malloc (WEBNET_PATH_MAX);
  128. if (lua_filename != RT_NULL)
  129. {
  130. rt_snprintf(lua_filename, WEBNET_PATH_MAX, "%s/index.lua", request->path);
  131. fd = open(lua_filename, O_RDONLY, 0);
  132. if (fd >= 0)
  133. {
  134. close(fd);
  135. _webnet_lua_dofile(session, lua_filename);
  136. wn_free(lua_filename);
  137. return WEBNET_MODULE_FINISHED;
  138. }
  139. wn_free(lua_filename);
  140. }
  141. }
  142. }
  143. return WEBNET_MODULE_CONTINUE;
  144. }
  145. #endif /* defined(WEBNET_USING_LUA) && defined(RT_USING_LUA) */