aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorame <[email protected]>2024-08-26 22:33:07 -0500
committerame <[email protected]>2024-08-26 22:33:07 -0500
commitd0c7c5b0dd9d20bfc323dc10c67f9de12d58c343 (patch)
tree893fe45044ce5869246328f82eca2e7b78f02dd6 /src
parentfda120441cabfefc511786d7a6af40eec9f181fa (diff)
initial param matching
Diffstat (limited to 'src')
-rw-r--r--src/net.c3
-rw-r--r--src/net/util.c48
-rw-r--r--src/net/util.h2
-rw-r--r--src/types/parray.c7
-rw-r--r--src/types/parray.h2
5 files changed, 61 insertions, 1 deletions
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*);