From af2da4242cfe192e49ee490d8dd3a3b7fff02000 Mon Sep 17 00:00:00 2001 From: amelia squires Date: Wed, 4 Sep 2024 01:17:25 -0500 Subject: add to server, and fix leaks --- src/net.c | 34 ++++++++++++++++++++++++++++++++-- src/net/util.c | 18 +++++++++++++++--- src/net/util.h | 4 ++-- tests/net.lua | 7 ++++++- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/net.c b/src/net.c index 2966905..48c685d 100644 --- a/src/net.c +++ b/src/net.c @@ -71,7 +71,22 @@ void* handle_client(void *_arg){ str_push(aa, sk->c); //parray_t* v = parray_find(paths, aa->c); - parray_t* v = route_match(paths, aa->c); + larray_t* params = larray_init(); + parray_t* v = route_match(paths, aa->c, ¶ms); + + /*for(int i = 0; i != params->len; i++){ + int id = larray_geti(params, i); + parray_t* par = params->arr[id].value; + printf("%i\n", i); + for(int x = 0; x != par->len; x++){ + printf("\t%s : %s\n",par->P[x].key->c, (char*)par->P[x].value); + } + + parray_clear(par, STR); + + } + + larray_clear(params);*/ if(sT != NULL) rolling_file_parse(L, &files_idx, &body_idx, buffer + header_eof + 4, sT, bytes_received - header_eof - 4, file_cont); @@ -154,13 +169,27 @@ void* handle_client(void *_arg){ luaI_tsets(L, header_idx, "Content-Type", "text/html"); luaI_tsetv(L, res_idx, "header", header_idx); - + //get all function that kinda match parray_t* owo = (parray_t*)v; 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; + //push url params + lua_newtable(L); + int new_param_idx = lua_gettop(L); + + int id = larray_geti(params, i); + parray_t* par = params->arr[id].value; + + for(int z = 0; z != par->len; z++){ + luaI_tsets(L, new_param_idx, par->P[z].key->c, (char*)par->P[z].value); + } + parray_clear(par, FREE); + + luaI_tsetv(L, req_idx, "paramaters", new_param_idx); + for(int z = 0; z != awa->len; z++){ char* path; struct lchar* wowa = awa->cs[z]; @@ -181,6 +210,7 @@ void* handle_client(void *_arg){ } } } + larray_clear(params); parray_lclear(owo); //dont free the rest lua_pushstring(L, "client_fd"); diff --git a/src/net/util.c b/src/net/util.c index 41069fb..868da0f 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -316,19 +316,31 @@ int match_param(char* path, char* match, parray_t* arr){ return path[pi] == 0 && match[mi] == 0; } -parray_t* route_match(parray_t* paths, char* request){ - parray_t* out = parray_initl(paths->len); +parray_t* route_match(parray_t* paths, char* request, larray_t** _params){ + larray_t* params = *_params; + parray_t* out = parray_initl(paths->len * 2); + parray_t* temp; out->len = 0; for(int i = 0; i != paths->len; i++){ //if(match_param(paths->P[i].key->c, request)) //*if(strcmp(request, paths->P[i].key->c) == 0)*/{ //printf("pass!\n"); + //printf("%i\n", i); + + temp = parray_init(); + + if(match_param(paths->P[i].key->c, request, temp)){ out->P[out->len] = paths->P[i]; + larray_set(¶ms, out->len, (void*)temp); out->len++; + } else { + parray_clear(temp, FREE); + } //} } - printf("\n"); + + *_params = params; return out; } diff --git a/src/net/util.h b/src/net/util.h index 17a99ac..98d4d93 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -1,5 +1,5 @@ #include "common.h" - +#include "../types/larray.h" /** * @brief calls recv into buffer until everything is read * @@ -46,7 +46,7 @@ void client_fd_errors(int client_fd); int content_disposition(str* src, parray_t** _dest); -parray_t* route_match(parray_t* paths, char* path); +parray_t* route_match(parray_t* paths, char* path, larray_t** params); int match_param(char* path, char* match, parray_t* arr); diff --git a/tests/net.lua b/tests/net.lua index 940e2fb..90a18c3 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -21,19 +21,24 @@ net.listen( server:all("/{name}", function(res, req) --print("name is "..req.name) print("name") + io.pprint(req.paramaters) end) server:all("/{name}/nya/{user}", function(res, req) --print("name is "..req.name) - print("name id user") + print("name user") + io.pprint(req.paramaters) end) server:all("*", function(res, req) print("all") + io.pprint(req.paramaters) end) server:all("/{name}/user/*/{id}", function(res, req) print("owo") + io.pprint(req.paramaters) + end) server:all("/", function(res, req) -- cgit v1.2.3