Pārlūkot izejas kodu

Support a pre-loaded file for the Lua environment

bel 11 gadi atpakaļ
vecāks
revīzija
8d3a48398c
3 mainītis faili ar 11 papildinājumiem un 1 dzēšanām
  1. 2 1
      src/civetweb.c
  2. 3 0
      src/main.c
  3. 6 0
      src/mod_lua.inl

+ 2 - 1
src/civetweb.c

@@ -526,7 +526,7 @@ enum {
     NUM_THREADS, RUN_AS_USER, REWRITE, HIDE_FILES, REQUEST_TIMEOUT,
 
 #if defined(USE_LUA)
-    LUA_SCRIPT_EXTENSIONS, LUA_SERVER_PAGE_EXTENSIONS,
+    LUA_PRELOAD_FILE, LUA_SCRIPT_EXTENSIONS, LUA_SERVER_PAGE_EXTENSIONS,
 #endif
 #if defined(USE_WEBSOCKET)
     WEBSOCKET_ROOT,
@@ -570,6 +570,7 @@ static const char *config_options[] = {
     "request_timeout_ms", "30000",
 
 #if defined(USE_LUA)
+    "lua_preload_file", NULL,
     "lua_script_pattern", "**.lua$",
     "lua_server_page_pattern", "**.lp$|**.lsp$",
 #endif

+ 3 - 0
src/main.c

@@ -552,6 +552,9 @@ static int is_filename_option(const char *option_name)
            !strcmp(option_name, "put_delete_auth_file") ||
            !strcmp(option_name, "access_log_file") ||
            !strcmp(option_name, "error_log_file") ||
+#ifdef USE_LUA
+           !strcmp(option_name, "lua_preload_file") ||
+#endif
            !strcmp(option_name, "ssl_certificate");
 }
 

+ 6 - 0
src/mod_lua.inl

@@ -605,6 +605,7 @@ static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, co
 {
     const struct mg_request_info *ri = mg_get_request_info(conn);
     char src_addr[IP_ADDR_STR_LEN];
+    const char * preload_file = conn->ctx->config[LUA_PRELOAD_FILE];
     int i;
 
     extern void luaL_openlibs(lua_State *);
@@ -719,6 +720,11 @@ static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, co
     /* Register default mg.onerror function */
     IGNORE_UNUSED_RESULT(luaL_dostring(L, "mg.onerror = function(e) mg.write('\\nLua error:\\n', "
         "debug.traceback(e, 1)) end"));
+
+    /* Preload */
+    if ((preload_file != NULL) && (*preload_file != 0)) {
+        IGNORE_UNUSED_RESULT(luaL_dofile(L, preload_file));
+    }    
 }
 
 static int lua_error_handler(lua_State *L)