diff options
| author | ame <[email protected]> | 2024-08-26 22:33:07 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2024-08-26 22:33:07 -0500 |
| commit | d0c7c5b0dd9d20bfc323dc10c67f9de12d58c343 (patch) | |
| tree | 893fe45044ce5869246328f82eca2e7b78f02dd6 | |
| parent | fda120441cabfefc511786d7a6af40eec9f181fa (diff) | |
initial param matching
| -rw-r--r-- | makefile | 2 | ||||
| -rw-r--r-- | src/net.c | 3 | ||||
| -rw-r--r-- | src/net/util.c | 48 | ||||
| -rw-r--r-- | src/net/util.h | 2 | ||||
| -rw-r--r-- | src/types/parray.c | 7 | ||||
| -rw-r--r-- | src/types/parray.h | 2 | ||||
| -rw-r--r-- | tests/net.lua | 8 |
7 files changed, 70 insertions, 2 deletions
@@ -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
@@ -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")
|
