Skip to content

Commit

Permalink
fixed bug with gateway lock
Browse files Browse the repository at this point in the history
  • Loading branch information
joexbayer committed Nov 19, 2024
1 parent 40f3a07 commit 4225eb8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions example/routes.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ counter.c
todo.c
json.c
chat.c
static.c
2 changes: 1 addition & 1 deletion example/static.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
14 changes: 9 additions & 5 deletions src/router.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(&regex, 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(&regex, anchored_pattern, REG_EXTENDED | REG_NOSUB) != 0) {
fprintf(stderr, "Invalid regex pattern: %s\n", anchored_pattern);
continue;
}

Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 4225eb8

Please sign in to comment.