aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-01-23 14:13:45 -0600
committerame <[email protected]>2024-01-23 14:13:45 -0600
commit0fcff4f28f4b1f2493ecb17b3fac46bff8c8140c (patch)
tree0671f1abedab63573f3212d82d5c759b80c15422
parent85c4718212e44b0e01485260b5f1574d5c7411a9 (diff)
almost working great, fix 35th con
-rw-r--r--src/lua.h23
-rw-r--r--src/net.c24
-rw-r--r--src/reg.c28
3 files changed, 37 insertions, 38 deletions
diff --git a/src/lua.h b/src/lua.h
index 6b146f6..625a0c1 100644
--- a/src/lua.h
+++ b/src/lua.h
@@ -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
diff --git a/src/net.c b/src/net.c
index 7beec3a..b0df7b9 100644
--- a/src/net.c
+++ b/src/net.c
@@ -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;
diff --git a/src/reg.c b/src/reg.c
index 030a93f..3d991a2 100644
--- a/src/reg.c
+++ b/src/reg.c
@@ -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");