feat: implement router free function

routing
Jef Roosens 2024-02-24 13:25:06 +01:00
parent 9fa009ccf4
commit fbd41f7e4e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 25 additions and 5 deletions

View File

@ -12,6 +12,15 @@ lnm_err lnm_http_route_init(lnm_http_route **out) {
return lnm_err_ok; 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_err lnm_http_route_segment_trie_init(lnm_http_route_segment_trie **out) {
lnm_http_route_segment_trie *trie = lnm_http_route_segment_trie *trie =
calloc(1, sizeof(lnm_http_route_segment_trie)); calloc(1, sizeof(lnm_http_route_segment_trie));

View File

@ -15,13 +15,22 @@ lnm_err lnm_http_router_init(lnm_http_router **out) {
return lnm_err_ok; return lnm_err_ok;
} }
void lnm_http_route_free(lnm_http_route *route) { void lnm_http_router_free(lnm_http_router *router) {
if (route == NULL) { if (router == NULL) {
return; return;
} }
lnm_http_route_segment_trie_free(route->key_segments); for (size_t i = 0; i < 128; i++) {
free(route); lnm_http_router_free(router->exact_children[i]);
}
lnm_http_router_free(router->single_segment_child);
for (size_t i = 0; i < lnm_http_method_total; i++) {
lnm_http_route_free(router->routes[i]);
}
free(router);
} }
static bool is_ascii(const char *s) { static bool is_ascii(const char *s) {

View File

@ -4,7 +4,7 @@
void test_routing_simple() { void test_routing_simple() {
lnm_http_router *router; lnm_http_router *router;
lnm_http_router_init(&router); TEST_CHECK(lnm_http_router_init(&router) == lnm_err_ok);
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") == 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/test2") == lnm_err_ok);
@ -37,6 +37,8 @@ void test_routing_simple() {
TEST_CHECK((segment = lnm_http_route_match_get(&match, "hello")) != NULL); TEST_CHECK((segment = lnm_http_route_match_get(&match, "hello")) != NULL);
TEST_CHECK(segment->start == 6); TEST_CHECK(segment->start == 6);
TEST_CHECK(segment->len == 8); TEST_CHECK(segment->len == 8);
lnm_http_router_free(router);
} }
TEST_LIST = { TEST_LIST = {