From fbd41f7e4eaa303c799084f889bce59d90de8c41 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 24 Feb 2024 13:25:06 +0100 Subject: [PATCH] feat: implement router free function --- src/http/lnm_http_route.c | 9 +++++++++ src/http/lnm_http_router.c | 17 +++++++++++++---- test/routing.c | 4 +++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/http/lnm_http_route.c b/src/http/lnm_http_route.c index 231a89d..9901b79 100644 --- a/src/http/lnm_http_route.c +++ b/src/http/lnm_http_route.c @@ -12,6 +12,15 @@ 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)); diff --git a/src/http/lnm_http_router.c b/src/http/lnm_http_router.c index 8b071db..e735e02 100644 --- a/src/http/lnm_http_router.c +++ b/src/http/lnm_http_router.c @@ -15,13 +15,22 @@ lnm_err lnm_http_router_init(lnm_http_router **out) { return lnm_err_ok; } -void lnm_http_route_free(lnm_http_route *route) { - if (route == NULL) { +void lnm_http_router_free(lnm_http_router *router) { + if (router == NULL) { return; } - lnm_http_route_segment_trie_free(route->key_segments); - free(route); + for (size_t i = 0; i < 128; i++) { + 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) { diff --git a/test/routing.c b/test/routing.c index f21dbdb..e18b695 100644 --- a/test/routing.c +++ b/test/routing.c @@ -4,7 +4,7 @@ void test_routing_simple() { 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/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->start == 6); TEST_CHECK(segment->len == 8); + + lnm_http_router_free(router); } TEST_LIST = {