From 4225eb87c5d1b8d1ebef1d7cd211c565ed669257 Mon Sep 17 00:00:00 2001 From: joexbayer Date: Tue, 19 Nov 2024 18:25:38 +0100 Subject: [PATCH] fixed bug with gateway lock --- example/routes.ini | 1 + example/static.c | 2 +- src/router.c | 14 +++++++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/example/routes.ini b/example/routes.ini index 0e5b4f5..f37c50a 100644 --- a/example/routes.ini +++ b/example/routes.ini @@ -9,3 +9,4 @@ counter.c todo.c json.c chat.c +static.c diff --git a/example/static.c b/example/static.c index d2cccc2..31cae9e 100644 --- a/example/static.c +++ b/example/static.c @@ -77,5 +77,5 @@ export module_t config = { /* Allows regex in route paths */ {"/static/.*", "GET", download, NONE}, }, - .size = sizeof(routes) / sizeof(routes[0]), + .size = 1, }; \ No newline at end of file diff --git a/src/router.c b/src/router.c index cb3e032..e7f3ef0 100644 --- a/src/router.c +++ b/src/router.c @@ -39,15 +39,19 @@ struct route route_find(char *route, char *method) { pthread_rwlock_rdlock(&gateway.entries[i].rwlock); for (int j = 0; j < gateway.entries[i].module->size; j++) { struct route_info *entry = &gateway.entries[i].module->routes[j]; - if(entry->path == NULL || entry->method == NULL) { + if (entry->path == NULL || entry->method == NULL) { continue; } if (strcmp(method, entry->method) == 0) { regex_t regex; + char anchored_pattern[1024]; - if (regcomp(®ex, entry->path, REG_EXTENDED | REG_NOSUB) != 0) { - fprintf(stderr, "Invalid regex pattern: %s\n", entry->path); + /* Create an anchored regex pattern, else partial paths will be matched... */ + snprintf(anchored_pattern, sizeof(anchored_pattern), "^%s$", entry->path); + + if (regcomp(®ex, anchored_pattern, REG_EXTENDED | REG_NOSUB) != 0) { + fprintf(stderr, "Invalid regex pattern: %s\n", anchored_pattern); continue; } @@ -161,7 +165,7 @@ static int load_from_shared_object(char* so_path){ return -1; } - pthread_rwlock_wrlock(&gateway.rwlock); + pthread_rwlock_rdlock(&gateway.rwlock); /* Check if module already exists */ for (int i = 0; i < gateway.count; i++) { @@ -185,7 +189,7 @@ static int load_from_shared_object(char* so_path){ if (route.route) { pthread_rwlock_unlock(route.rwlock); pthread_rwlock_unlock(&gateway.rwlock); - fprintf(stderr, "[ERROR] Route conflict: %s %s\n", module->routes[i].method, module->routes[i].path); + fprintf(stderr, "[ERROR] Route conflict: %s %s - \n", module->routes[i].method, module->routes[i].path); dlclose(handle); return -1; }