From 76e750677841a659556fd20741aec33b922bcfdb Mon Sep 17 00:00:00 2001 From: ame Date: Mon, 5 Feb 2024 16:03:56 -0600 Subject: cleaning and fixes --- docs/net.md | 18 +++--------------- src/io.c | 4 +++- src/lua.h | 17 +++++++++++++++++ src/net.c | 60 +++++++++++++++++++++++++++-------------------------------- tests/net.lua | 12 ++++++++---- 5 files changed, 58 insertions(+), 53 deletions(-) diff --git a/docs/net.md b/docs/net.md index b129aa1..cfc064e 100644 --- a/docs/net.md +++ b/docs/net.md @@ -31,7 +31,7 @@ server:unlock() ... ``` -### server:close ** +### server:close closes server @@ -43,11 +43,9 @@ the function has 2 arguments, the first (res) contains functions and info about the second (req) contains info on the request, the path allows for wildcards, multiple get requests per path is allowed ```lua - - server:GET("*", function(res, req, next) if(req['Version'] ~= "HTTP/1.1") then - res:deny() + res:close() end end) @@ -59,16 +57,6 @@ end) ... ``` -#### res:deny ** - -denies request as if there was no server - -```lua -... -res:deny() --make the client timeout, lol -... -``` - #### res:write 'takes a string @@ -95,7 +83,7 @@ res:send("

hello world

") ... ``` -#### res:end +#### res:close closes connection, sets res.client_fd to -1, any calls that use this value will fail diff --git a/src/io.c b/src/io.c index 14117ec..5eb9de8 100644 --- a/src/io.c +++ b/src/io.c @@ -114,10 +114,12 @@ void i_pprint(lua_State* L, int indent, int skip_indent){ } else { if(!skip) print_indentation(indent + 1); } + int tuwu = lua_gettop(L); i_pprint(L,indent+1,1); printf(","); if(!skip) printf("\n"); - + + lua_settop(L, tuwu); lua_pop(L,1); } print_indentation(indent); diff --git a/src/lua.h b/src/lua.h index a4933ff..7145098 100644 --- a/src/lua.h +++ b/src/lua.h @@ -4,6 +4,23 @@ void i_dcopy(lua_State* src, lua_State* dest, void*); +//generic macro that takes other macros (see below) +#define _tset_b(L, Tidx, K, V, F)\ + lua_pushvalue(L, Tidx);\ + lua_pushstring(L, K);\ + F(L, V);\ + lua_settable(L, Tidx); + +//some macros to make batch adding easier (may switch to arrays for this later) +#define luaI_tseti(L, Tidx, K, V)\ + _tset_b(L, Tidx, K, V, lua_pushinteger) +#define luaI_tsets(L, Tidx, K, V)\ + _tset_b(L, Tidx, K, V, lua_pushstring) +#define luaI_tsetv(L, Tidx, K, V)\ + _tset_b(L, Tidx, K, V, lua_pushvalue) +#define luaI_tsetcf(L, Tidx, K, V)\ + _tset_b(L, Tidx, K, V, lua_pushcfunction) + #if LUA_VERSION_NUM == 504 #define lreg(N, FN)\ lua_pushstring(L, N);\ diff --git a/src/net.c b/src/net.c index 058eda9..3ce40c5 100644 --- a/src/net.c +++ b/src/net.c @@ -147,6 +147,7 @@ typedef struct { int fd; int port; lua_State* L; + struct sockaddr_in cli; } thread_arg_struct; void http_code(int code, char* code_det){ @@ -314,7 +315,7 @@ int l_send(lua_State* L){ return 0; } -int l_end(lua_State* L){ +int l_close(lua_State* L){ int res_idx = 1; lua_pushvalue(L, res_idx); @@ -327,12 +328,13 @@ int l_end(lua_State* L){ lua_pushinteger(L, -1); lua_settable(L, res_idx); closesocket(client_fd); - + return 0; } volatile size_t threads = 0; void* handle_client(void *_arg){ + //pthread_mutex_lock(&mutex); thread_arg_struct* args = (thread_arg_struct*)_arg; int client_fd = args->fd; @@ -353,6 +355,7 @@ void* handle_client(void *_arg){ int len = 0; //checks for a valid header if(parse_header(buffer, header_eof, &table, &len) != -1){ + int k = stable_key(table, "Path", len); char portc[10] = {0}; @@ -370,57 +373,46 @@ void* handle_client(void *_arg){ str_free(resp); } else { lua_newtable(L); + int req_idx = lua_gettop(L); lua_newtable(L); + int res_idx = lua_gettop(L); + for(int i = 0; i != len * 2; i+=2){ //printf("'%s' :: '%s'\n",table[i]->c, table[i+1]->c); - lua_pushstring(L, table[i]->c); - lua_pushstring(L, table[i+1]->c); - lua_settable(L, -3); + luaI_tsets(L, req_idx, table[i]->c, table[i+1]->c); } + + luaI_tsets(L, req_idx, "ip", inet_ntoa(args->cli.sin_addr)); - int req_idx = lua_gettop(L); - lua_newtable(L); - int res_idx = lua_gettop(L); //functions - lua_pushstring(L, "send"); - lua_pushcfunction(L, l_send); - lua_settable(L, -3); - - lua_pushstring(L, "write"); - lua_pushcfunction(L, l_write); - lua_settable(L, -3); - - lua_pushstring(L, "end"); - lua_pushcfunction(L, l_end); - lua_settable(L, -3); + luaI_tsetcf(L, res_idx, "send", l_send); + luaI_tsetcf(L, res_idx, "write", l_write); + luaI_tsetcf(L, res_idx, "close", l_close); //values - lua_pushstring(L, "client_fd"); - lua_pushinteger(L, client_fd); - lua_settable(L, -3); - + luaI_tseti(L, res_idx, "client_fd", client_fd); + //header table lua_newtable(L); - lua_pushstring(L, "Code"); - lua_pushinteger(L, 200); - lua_settable(L, -3); - - lua_pushstring(L, "Content-Type"); - lua_pushstring(L, "text/html"); - lua_settable(L, -3); + int header_idx = lua_gettop(L); + luaI_tseti(L, header_idx, "Code", 200); + luaI_tsets(L, header_idx, "Content-Type", "text/html"); - lua_pushstring(L, "header"); - lua_pushvalue(L, -2); - lua_settable(L, res_idx); + luaI_tsetv(L, res_idx, "header", header_idx); //the function(s) //get all function that kinda match parray_t* owo = (parray_t*)v; + uint64_t passes = 0; for(int i = 0; i != owo->len; i++){ //though these are arrays of arrays we have to iterate *again* struct sarray_t* awa = (struct sarray_t*)owo->P[i].value; for(int z = 0; z != awa->len; z++){ + + luaI_tseti(L, res_idx, "passes", passes); + passes++; + struct lchar* wowa = awa->cs[z]; luaL_loadbuffer(L, wowa->c, wowa->len, wowa->c); @@ -511,12 +503,14 @@ int start_serv(lua_State* L, int port){ printf("failed to accept\n"); abort(); } + //printf("%s\n",inet_ntoa(client_addr.sin_addr)); //printf("%i\n",threads); //open a state to call shit, should be somewhat thread safe thread_arg_struct* args = malloc(sizeof * args); args->fd = *client_fd; args->port = port; + args->cli = client_addr; //args->L = oL; pthread_mutex_lock(&mutex); diff --git a/tests/net.lua b/tests/net.lua index 443880c..ba5dc29 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -20,14 +20,18 @@ llib.io.pprint(llib.net.listen( --res.Code = 201 --wwo.sleep(1) --wwo.llib.io.pprint(wwo.sleep) + require "llib" + llib.io.pprint(req) print("hi from first") end) server:GET("/", function(res, req) - - res:write("test") - res:write("test2") - res:end() + require "llib" + res.header["Content-Type"] = "text/plain" + + res:write("hi\n") + res:write("next") + res:close() end) server:GET("/test", function(res, req) -- cgit v1.2.3