From 6cbc0f817066b8bbab84a9be466223652b628d42 Mon Sep 17 00:00:00 2001 From: ame Date: Mon, 26 Aug 2024 22:33:07 -0500 Subject: initial param matching --- makefile | 2 +- src/net.c | 3 ++- src/net/util.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/net/util.h | 2 ++ src/types/parray.c | 7 +++++++ src/types/parray.h | 2 ++ tests/net.lua | 8 ++++++++ 7 files changed, 70 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 72e3243..11731cb 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ CC := clang GIT_COMMIT := "$(shell git describe --tags)-$(shell git describe --always --match 'NOT A TAG')" -CFLAGS := -fPIC -DGIT_COMMIT='$(GIT_COMMIT)' -Wall +CFLAGS := -fPIC -DGIT_COMMIT='$(GIT_COMMIT)' LFLAGS := -lm -shared LINKER := clang diff --git a/src/net.c b/src/net.c index 7db887b..2966905 100644 --- a/src/net.c +++ b/src/net.c @@ -70,7 +70,8 @@ void* handle_client(void *_arg){ str_push(aa, sk->c); - void* v = parray_find(paths, aa->c); + //parray_t* v = parray_find(paths, aa->c); + parray_t* v = route_match(paths, aa->c); if(sT != NULL) rolling_file_parse(L, &files_idx, &body_idx, buffer + header_eof + 4, sT, bytes_received - header_eof - 4, file_cont); diff --git a/src/net/util.c b/src/net/util.c index 213be9f..bc2c6bb 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -239,3 +239,51 @@ int content_disposition(str* src, parray_t** _dest){ return 1; } + +int match_param(char* path, char* match){ + int pi, mi = pi = 0; + int step = 0; + //0 increment both + //1 move match to find '/' or '\0' + //2 move path to find '}' + for(; path[pi] != '\0' && match[mi] != '\0';){ + if(step == 0){ + if(path[pi] == '{'){ + step = 1; + } else { + pi++; + mi++; + } + } else if (step == 1){ + if(match[mi] == '/'){ + printf("\n"); + step = 2; + } else { + printf("%c",match[mi]); + mi++; + } + } else if (step == 2){ + if(path[pi] == '}'){ + step = 0; + } else { + pi++; + } + } + } + return 0; +} + +parray_t* route_match(parray_t* paths, char* request){ + parray_t* out = parray_initl(paths->len); + out->len = 0; + + for(int i = 0; i != paths->len; i++){ + match_param(paths->P[i].key->c, request); + if(strcmp(request, paths->P[i].key->c) == 0){ + out->P[out->len] = paths->P[i]; + out->len++; + + } + } + return out; +} diff --git a/src/net/util.h b/src/net/util.h index d723b6f..ea7c294 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -45,3 +45,5 @@ void http_code(int code, char* code_det); void client_fd_errors(int client_fd); int content_disposition(str* src, parray_t** _dest); + +parray_t* route_match(parray_t* paths, char* path); diff --git a/src/types/parray.c b/src/types/parray.c index a1109c5..b1e41a2 100644 --- a/src/types/parray.c +++ b/src/types/parray.c @@ -31,6 +31,13 @@ parray_t* parray_init(){ return awa; } +parray_t* parray_initl(int len){ + parray_t* awa = malloc(sizeof * awa); + awa->P = malloc(sizeof * awa->P * len); + awa->len = len; + return awa; +} + /** * @brief sets value at key to value, adds a new index if new * diff --git a/src/types/parray.h b/src/types/parray.h index fb89689..7972cc1 100644 --- a/src/types/parray.h +++ b/src/types/parray.h @@ -17,6 +17,8 @@ enum free_type { }; parray_t* parray_init(); +parray_t* parray_initl(int len); + void parray_set(parray_t*, char*, void*); void parray_push(parray_t*, char*, void*); void* parray_get(parray_t* , char*); diff --git a/tests/net.lua b/tests/net.lua index 25ca696..fdb17d4 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -17,6 +17,14 @@ net.listen( io.pprint("online") _G.server = server + + server:all("/{name}", function(res, req) + print("name is "..req.name) + end) + server:all("*", function(res, req) + print("all") + end) + server:all("/", function(res, req) b = crypto.md5("hello") -- cgit v1.2.3