diff options
| -rw-r--r-- | src/lua.h | 23 | ||||
| -rw-r--r-- | src/net.c | 24 | ||||
| -rw-r--r-- | src/reg.c | 28 |
3 files changed, 37 insertions, 38 deletions
@@ -1,8 +1,21 @@ -#include <lua5.4/lua.h> -#include <lua5.4/lualib.h> -#include <lua5.4/lauxlib.h> +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> -#define lua_objlen(L,i) lua_rawlen(L,(i)) -#define luaL_register(L, M, F) luaL_newlib(L, F); + +#if LUA_VERSION_NUM == 504 + #define lreg(N, FN)\ + lua_pushstring(L, N);\ + luaL_register(L, NULL, FN);\ + lua_settable(L, -3); + + #define lua_objlen(L,i) lua_rawlen(L,(i)) + #define luaL_register(L, M, F) luaL_newlib(L, F); +#else + #define lreg(N, FN)\ + lua_newtable(L);\ + luaL_register(L, NULL, FN);\ + lua_setfield(L, 2, N); +#endif @@ -22,7 +22,7 @@ #include "table.h" #include "i_str.h" -#define max_con 10 +#define max_con 200 #define BUFFER_SIZE 2048 static int ports[65535] = { 0 }; @@ -258,15 +258,15 @@ void* handle_client(void *_arg){ //str* resp; //http_build(&resp, 200, "OK","text/html", "<h1>hello world!</h1>"); - + lua_State* L = args->L; - + lua_rawgeti(L, LUA_REGISTRYINDEX, ports[args->port]); int k = stable_key(table, "Path", len); lua_pushstring(L, table[k]->c); lua_gettable(L, -2); - if(lua_type(L, -1) == LUA_TNIL){ + if(1 || lua_type(L, -1) == LUA_TNIL){ str* resp; http_build(&resp, 404, "Not Found","text/html", "<h1>404</h1>"); send(client_fd, resp->c, resp->len, 0); @@ -327,6 +327,11 @@ void* handle_client(void *_arg){ //str_free(resp); } + + for(int i = 0; i != len; i++){ + str_free(table[i]); + } + free(table); } closesocket(client_fd); free(args); @@ -352,7 +357,7 @@ void dcopy_lua(lua_State* dest, lua_State* src, int port){ lua_pushnil(src); for(;lua_next(src,tt) != 0;){ char* key2 = (char*)luaL_checkstring(src, -2); - + //copy function lua_pushstring(dest, key2); lua_xmove(src, dest, 1); @@ -411,12 +416,9 @@ int start_serv(lua_State* L, int port){ abort(); } - lua_State* oL = luaL_newstate(); - - dcopy_lua(oL, L, port); - //lua_rawgeti(oL, LUA_REGISTRYINDEX, ports[port]); - - + //open a state to call shit, should be somewhat thread safe + lua_State* oL = lua_newthread(L); + printf("%i\n",lua_gettop(L)); thread_arg_struct* args = malloc(sizeof * args); args->fd = *client_fd; args->port = port; @@ -36,29 +36,13 @@ int luaopen_llib(lua_State* L) { lua_newtable(L); //lua_newtable(L); - lua_pushstring(L, "array"); - luaL_register(L, NULL, array_function_list); - lua_settable(L, -3); - lua_pushstring(L, "crypto"); - luaL_register(L, NULL, crypto_function_list); - lua_settable(L, -3); - - lua_pushstring(L, "io"); - luaL_register(L, NULL, io_function_list); - lua_settable(L, -3); - - lua_pushstring(L, "math"); - luaL_register(L, NULL, math_function_list); - lua_settable(L, -3); - - lua_pushstring(L, "config"); - luaL_register(L, NULL, config_function_list); - lua_settable(L, -3); - - lua_pushstring(L, "net"); - luaL_register(L, NULL, net_function_list); - lua_settable(L, -3); + lreg("array", array_function_list); + lreg("crypto", crypto_function_list); + lreg("io", io_function_list); + lreg("math", math_function_list); + lreg("config", config_function_list); + lreg("net", net_function_list); //make llib global lua_setglobal(L, "llib"); |
