feat(routing): support matching key segments in routes

routing
Jef Roosens 2024-02-22 22:15:19 +01:00
parent de4e509c9c
commit f652fa08c1
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 207 additions and 39 deletions

View File

@ -32,6 +32,7 @@ typedef enum lnm_err {
lnm_err_not_setup,
lnm_err_bad_regex,
lnm_err_not_found,
lnm_err_already_present,
lnm_err_invalid_route,
lnm_err_overlapping_route,
} lnm_err;

View File

@ -1,11 +1,22 @@
#ifndef LNM_HTTP_ROUTER
#define LNM_HTTP_ROUTER
#define LNM_HTTP_MAX_KEY_SEGMENTS 4
#include "lnm/common.h"
#include "lnm/http/consts.h"
typedef struct lnm_http_route lnm_http_route;
typedef struct lnm_http_route_match {
const lnm_http_route *route;
lnm_http_method method;
struct {
size_t start;
size_t len;
} key_segments[LNM_HTTP_MAX_KEY_SEGMENTS];
} lnm_http_route_match;
typedef struct lnm_http_router lnm_http_router;
typedef enum lnm_http_route_err {
@ -31,7 +42,7 @@ lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
lnm_err lnm_http_router_nest(lnm_http_router *parent,
const lnm_http_router *child, const char *prefix);
lnm_http_route_err lnm_http_router_route(const lnm_http_route **out,
lnm_http_route_err lnm_http_router_route(lnm_http_route_match *out,
const lnm_http_router *router,
lnm_http_method method,
const char *path);

View File

@ -5,7 +5,23 @@
#include "lnm/http/consts.h"
#include "lnm/http/router.h"
struct lnm_http_route {};
typedef struct lnm_http_route_segment_trie {
struct lnm_http_route_segment_trie *children[128];
size_t index;
bool represents_segment;
} lnm_http_route_segment_trie;
lnm_err lnm_http_route_segment_trie_init(lnm_http_route_segment_trie **out);
void lnm_http_route_segment_trie_free(lnm_http_route_segment_trie *trie);
lnm_err lnm_http_route_key_segment_insert(lnm_http_route *route,
const char *key, size_t key_len,
size_t index);
struct lnm_http_route {
lnm_http_route_segment_trie *key_segments;
};
struct lnm_http_router {
struct lnm_http_router *exact_children[128];

View File

@ -1,4 +1,3 @@
#include <stdio.h>
#include <string.h>
#include "lnm/common.h"
@ -29,6 +28,71 @@ lnm_err lnm_http_route_init(lnm_http_route **out) {
return lnm_err_ok;
}
void lnm_http_route_free(lnm_http_route *route) {
if (route == NULL) {
return;
}
lnm_http_route_segment_trie_free(route->key_segments);
free(route);
}
lnm_err lnm_http_route_segment_trie_init(lnm_http_route_segment_trie **out) {
lnm_http_route_segment_trie *trie =
calloc(1, sizeof(lnm_http_route_segment_trie));
if (trie == NULL) {
return lnm_err_failed_alloc;
}
*out = trie;
return lnm_err_ok;
}
void lnm_http_route_segment_trie_free(lnm_http_route_segment_trie *trie) {
if (trie == NULL) {
return;
}
for (size_t i = 0; i < 128; i++) {
if (trie->children[i] != NULL) {
lnm_http_route_segment_trie_free(trie->children[i]);
}
}
free(trie);
}
lnm_err lnm_http_route_key_segment_insert(lnm_http_route *route,
const char *key, size_t key_len,
size_t index) {
if (route->key_segments == NULL) {
LNM_RES(lnm_http_route_segment_trie_init(&route->key_segments));
}
lnm_http_route_segment_trie *trie = route->key_segments;
for (size_t key_index = 0; key_index < key_len; key_index++) {
unsigned char c = key[key_index];
if (trie->children[c] == NULL) {
LNM_RES(lnm_http_route_segment_trie_init(&trie->children[c]));
}
trie = trie->children[c];
}
if (trie->represents_segment) {
return lnm_err_already_present;
}
trie->represents_segment = true;
trie->index = index;
return lnm_err_ok;
}
static bool is_ascii(const char *s) {
while (*s != '0') {
if (*s > 127) {
@ -47,27 +111,58 @@ lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
return lnm_err_invalid_route;
}
lnm_http_route *route;
LNM_RES(lnm_http_route_init(&route));
size_t key_segments_count = 0;
lnm_err res = lnm_err_ok;
while (*path != '\0') {
unsigned char c = *path;
switch (c) {
case ':':
case ':': {
const char *next_slash_ptr = strchr(path, '/');
const char *new_path =
next_slash_ptr == NULL ? strchr(path, '\0') : next_slash_ptr;
size_t key_len = new_path - path - 1;
if (key_len == 0) {
res = lnm_err_invalid_route;
goto end;
}
res = lnm_http_route_key_segment_insert(route, path + 1, key_len,
key_segments_count);
if (res != lnm_err_ok) {
goto end;
}
key_segments_count++;
if (http_router->single_segment_child == NULL) {
LNM_RES(lnm_http_router_init(&http_router->single_segment_child));
res = lnm_http_router_init(&http_router->single_segment_child);
if (res != lnm_err_ok) {
goto end;
}
}
http_router = http_router->single_segment_child;
// All other characters in the segment are ignored
const char *next_slash_ptr = strchr(path, '/');
path = next_slash_ptr == NULL ? strchr(path, '\0') : next_slash_ptr;
break;
path = new_path;
} break;
case '*':
// TODO multi-segment wildcard
break;
default:
if (http_router->exact_children[c] == NULL) {
LNM_RES(lnm_http_router_init(&http_router->exact_children[c]));
res = lnm_http_router_init(&http_router->exact_children[c]);
if (res != lnm_err_ok) {
goto end;
}
}
http_router = http_router->exact_children[c];
@ -77,34 +172,41 @@ lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
}
if (http_router->routes[method] != NULL) {
return lnm_err_overlapping_route;
res = lnm_err_overlapping_route;
goto end;
}
LNM_RES(lnm_http_route_init(&http_router->routes[method]));
http_router->routes[method] = route;
http_router->represents_route = true;
end:
if (res != lnm_err_ok) {
lnm_http_route_free(route);
return res;
}
if (out != NULL) {
*out = http_router->routes[method];
*out = route;
}
return lnm_err_ok;
}
lnm_http_route_err lnm_http_router_route(const lnm_http_route **out,
const lnm_http_router *router,
lnm_http_method method,
const char *path) {
if (!is_ascii(path)) {
return lnm_http_route_err_unknown_route;
}
if (*path == '\0') {
static lnm_http_route_err __lnm_http_router_route(lnm_http_route_match *out,
const lnm_http_router *router,
lnm_http_method method,
const char *path,
size_t path_index,
size_t matched_key_segments) {
if (path[path_index] == '\0') {
if (!router->represents_route) {
return lnm_http_route_err_unknown_route;
}
if (out != NULL) {
*out = router->routes[method];
out->route = router->routes[method];
}
return router->routes[method] == NULL ? lnm_http_route_err_unknown_method
@ -112,11 +214,12 @@ lnm_http_route_err lnm_http_router_route(const lnm_http_route **out,
}
lnm_http_route_err res = lnm_http_route_err_unknown_route;
lnm_http_router *exact_router = router->exact_children[(unsigned char)*path];
const lnm_http_router *exact_router =
router->exact_children[(unsigned char)path[path_index]];
if (exact_router != NULL) {
lnm_http_route_err sub_res =
lnm_http_router_route(out, exact_router, method, path + 1);
lnm_http_route_err sub_res = __lnm_http_router_route(
out, exact_router, method, path, path_index + 1, matched_key_segments);
if (sub_res == lnm_http_route_err_match) {
return lnm_http_route_err_match;
@ -125,21 +228,43 @@ lnm_http_route_err lnm_http_router_route(const lnm_http_route **out,
res = LNM_MAX(res, sub_res);
}
lnm_http_router *single_segment_router = router->single_segment_child;
const lnm_http_router *single_segment_router = router->single_segment_child;
if (single_segment_router != NULL) {
const char *next_slash_ptr = strchr(path, '/');
path = next_slash_ptr == NULL ? strchr(path, '\0') : next_slash_ptr;
const char *next_slash_ptr = strchr(path + path_index, '/');
const char *new_path = next_slash_ptr == NULL
? strchr(path + path_index, '\0')
: next_slash_ptr;
size_t segment_len = new_path - (path + path_index);
lnm_http_route_err sub_res =
lnm_http_router_route(out, exact_router, method, path);
if (segment_len > 0) {
lnm_http_route_err sub_res = __lnm_http_router_route(
out, single_segment_router, method, path, path_index + segment_len,
matched_key_segments + 1);
if (sub_res == lnm_http_route_err_match) {
return lnm_http_route_err_match;
if (sub_res == lnm_http_route_err_match) {
if (out != NULL) {
out->key_segments[matched_key_segments].start = path_index;
out->key_segments[matched_key_segments].len = segment_len;
}
return lnm_http_route_err_match;
}
res = LNM_MAX(res, sub_res);
}
res = LNM_MAX(res, sub_res);
}
return res;
}
lnm_http_route_err lnm_http_router_route(lnm_http_route_match *out,
const lnm_http_router *router,
lnm_http_method method,
const char *path) {
if (!is_ascii(path)) {
return lnm_http_route_err_unknown_route;
}
return __lnm_http_router_route(out, router, method, path, 0, 0);
}

View File

@ -6,13 +6,28 @@ void test_routing_simple() {
lnm_http_router *router;
lnm_http_router_init(&router);
lnm_http_router_add(NULL, router, lnm_http_method_get, "/test");
lnm_http_router_add(NULL, router, lnm_http_method_get, "/test/test2");
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/test") == lnm_err_ok);
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/test/test2") == lnm_err_ok);
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/test/:hello") == lnm_err_ok);
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/test/:hello/:second") == lnm_err_ok);
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test") == lnm_http_route_err_match);
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test/te") == lnm_http_route_err_unknown_route);
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test2/t/e") == lnm_http_route_err_unknown_route);
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_head, "/test/test2") == lnm_http_route_err_unknown_method);
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test/test2") == lnm_http_route_err_match);
lnm_http_route_match match;
TEST_CHECK(lnm_http_router_route(&match, router, lnm_http_method_get, "/test/test_var") == lnm_http_route_err_match);
TEST_CHECK(match.key_segments[0].start == 6);
TEST_CHECK(match.key_segments[0].len == 8);
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test/") == lnm_http_route_err_unknown_route);
TEST_CHECK(lnm_http_router_route(&match, router, lnm_http_method_get, "/test/test_var/secondvar") == lnm_http_route_err_match);
TEST_CHECK(match.key_segments[0].start == 6);
TEST_CHECK(match.key_segments[0].len == 8);
TEST_CHECK(match.key_segments[1].start == 15);
TEST_CHECK(match.key_segments[1].len == 9);
}
TEST_LIST = {