refactor(routing): simply key segment api

routing
Jef Roosens 2024-02-23 11:38:52 +01:00
parent 9dbdb0b089
commit d739157fb1
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 15 additions and 19 deletions

View File

@ -49,7 +49,7 @@ lnm_http_route_err lnm_http_router_route(lnm_http_route_match *out,
lnm_http_method method, lnm_http_method method,
const char *path); const char *path);
lnm_err lnm_http_route_match_get(lnm_http_route_match_segment **out, const lnm_http_route_match_segment *
lnm_http_route_match *match, const char *key); lnm_http_route_match_get(lnm_http_route_match *match, const char *key);
#endif #endif

View File

@ -56,9 +56,7 @@ void lnm_http_route_segment_trie_free(lnm_http_route_segment_trie *trie) {
} }
for (size_t i = 0; i < 128; i++) { for (size_t i = 0; i < 128; i++) {
if (trie->children[i] != NULL) { lnm_http_route_segment_trie_free(trie->children[i]);
lnm_http_route_segment_trie_free(trie->children[i]);
}
} }
free(trie); free(trie);
@ -94,8 +92,8 @@ lnm_err lnm_http_route_key_segment_insert(lnm_http_route *route,
} }
static bool is_ascii(const char *s) { static bool is_ascii(const char *s) {
while (*s != '0') { while (*s != '\0') {
if (*s > 127) { if (*s < 0) {
return false; return false;
} }
@ -269,10 +267,10 @@ lnm_http_route_err lnm_http_router_route(lnm_http_route_match *out,
return __lnm_http_router_route(out, router, method, path, 0, 0); return __lnm_http_router_route(out, router, method, path, 0, 0);
} }
lnm_err lnm_http_route_match_get(lnm_http_route_match_segment **out, const lnm_http_route_match_segment *
lnm_http_route_match *match, const char *key) { lnm_http_route_match_get(lnm_http_route_match *match, const char *key) {
if (match->route->key_segments == NULL) { if (match->route->key_segments == NULL) {
return lnm_err_not_found; return NULL;
} }
lnm_http_route_segment_trie *trie = match->route->key_segments; lnm_http_route_segment_trie *trie = match->route->key_segments;
@ -281,17 +279,15 @@ lnm_err lnm_http_route_match_get(lnm_http_route_match_segment **out,
trie = trie->children[(unsigned char)*key]; trie = trie->children[(unsigned char)*key];
if (trie == NULL) { if (trie == NULL) {
return lnm_err_not_found; return NULL;
} }
key++; key++;
} }
if (!trie->represents_segment) { if (!trie->represents_segment) {
return lnm_err_not_found; return NULL;
} }
*out = &match->key_segments[trie->index]; return &match->key_segments[trie->index];
return lnm_err_ok;
} }

View File

@ -29,12 +29,12 @@ void test_routing_simple() {
TEST_CHECK(match.key_segments[1].start == 15); TEST_CHECK(match.key_segments[1].start == 15);
TEST_CHECK(match.key_segments[1].len == 9); TEST_CHECK(match.key_segments[1].len == 9);
lnm_http_route_match_segment *segment; const lnm_http_route_match_segment *segment;
TEST_CHECK(lnm_http_route_match_get(&segment, &match, "second") == lnm_err_ok); TEST_CHECK((segment = lnm_http_route_match_get(&match, "second")) != NULL);
TEST_CHECK(segment->start == 15); TEST_CHECK(segment->start == 15);
TEST_CHECK(segment->len == 9); TEST_CHECK(segment->len == 9);
TEST_CHECK(lnm_http_route_match_get(&segment, &match, "yuhh") == lnm_err_not_found); TEST_CHECK((segment = lnm_http_route_match_get(&match, "yuhh")) == NULL);
TEST_CHECK(lnm_http_route_match_get(&segment, &match, "hello") == lnm_err_ok); 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);
} }