From 6cbc0f817066b8bbab84a9be466223652b628d42 Mon Sep 17 00:00:00 2001 From: ame Date: Mon, 26 Aug 2024 22:33:07 -0500 Subject: initial param matching --- src/net/util.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/net/util.h | 2 ++ 2 files changed, 50 insertions(+) (limited to 'src/net') 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); -- cgit v1.2.3