static void prepare_lua_environment(struct mg_connection *conn, lua_State *L) { const struct mg_request_info *ri = &conn->request_info; extern void luaL_openlibs(lua_State *); int i; luaL_openlibs(L); #ifdef USE_LUA_SQLITE3 { extern int luaopen_lsqlite3(lua_State *); luaopen_lsqlite3(L); } #endif luaL_newmetatable(L, LUASOCKET); lua_pushliteral(L, "__index"); luaL_newlib(L, luasocket_methods); lua_rawset(L, -3); lua_pop(L, 1); lua_register(L, "connect", lsp_connect); if (conn == NULL) return; // Register mg module lua_newtable(L); reg_function(L, "read", lsp_read, conn); reg_function(L, "write", lsp_write, conn); reg_function(L, "cry", lsp_cry, conn); reg_function(L, "include", lsp_include, conn); reg_function(L, "redirect", lsp_redirect, conn); reg_string(L, "version", MONGOOSE_VERSION); // Export request_info lua_pushstring(L, "request_info"); lua_newtable(L); reg_string(L, "request_method", ri->request_method); reg_string(L, "uri", ri->uri); reg_string(L, "http_version", ri->http_version); reg_string(L, "query_string", ri->query_string); reg_int(L, "remote_ip", ri->remote_ip); reg_int(L, "remote_port", ri->remote_port); reg_int(L, "num_headers", ri->num_headers); lua_pushstring(L, "http_headers"); lua_newtable(L); for (i = 0; i < ri->num_headers; i++) { reg_string(L, ri->http_headers[i].name, ri->http_headers[i].value); } lua_rawset(L, -3); lua_rawset(L, -3); lua_setglobal(L, "mg"); // Register default mg.onerror function luaL_dostring(L, "mg.onerror = function(e) mg.write('\\nLua error:\\n', " "debug.traceback(e, 1)) end"); }
void init_lua_binding(){ config_t *cfg; cfg = get_config(); init_lua_state(cfg->master_lua_file, cfg->master_lua_path, cfg->master_lua_cpath); reg_function("message_gate",_message_gate); reg_function("message_worker",_message_worker); reg_function("message_all_gate",_message_all_gate); reg_function("message_all_worker",_message_all_worker); }
static void prepare_lua_environment(struct mg_connection *ri, lua_State *L) { extern void luaL_openlibs(lua_State *); int i; luaL_openlibs(L); if (ri == NULL) return; // Register mg module lua_newtable(L); reg_function(L, "write", lua_write, ri); reg_function(L, "header", lua_header, ri); // Export request_info lua_pushstring(L, "request_info"); lua_newtable(L); reg_string(L, "request_method", ri->request_method); reg_string(L, "uri", ri->uri); reg_string(L, "http_version", ri->http_version); reg_string(L, "query_string", ri->query_string); reg_string(L, "remote_ip", ri->remote_ip); reg_int(L, "remote_port", ri->remote_port); reg_string(L, "local_ip", ri->local_ip); reg_int(L, "local_port", ri->local_port); lua_pushstring(L, "content"); lua_pushlstring(L, ri->content == NULL ? "" : ri->content, ri->content_len); lua_rawset(L, -3); reg_int(L, "num_headers", ri->num_headers); lua_pushstring(L, "http_headers"); lua_newtable(L); for (i = 0; i < ri->num_headers; i++) { reg_string(L, ri->http_headers[i].name, ri->http_headers[i].value); } lua_rawset(L, -3); lua_rawset(L, -3); lua_setglobal(L, "mg"); }