Compare commits

..

4 Commits

8 changed files with 2069 additions and 1 deletions

View File

@ -73,7 +73,7 @@ $(BINS_TEST): %: %.c.o $(LIB)
$(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c $(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
mkdir -p $(dir $@) mkdir -p $(dir $@)
$(CC) $(_CFLAGS) -I$(TEST_DIR) \ $(CC) $(_CFLAGS) -I$(TEST_DIR) \
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \ -I$(SRC_DIR)/_include \
-c $< -o $@ -c $< -o $@
# =====EXAMPLES===== # =====EXAMPLES=====

View File

@ -32,6 +32,8 @@ typedef enum lnm_err {
lnm_err_not_setup, lnm_err_not_setup,
lnm_err_bad_regex, lnm_err_bad_regex,
lnm_err_not_found, lnm_err_not_found,
lnm_err_invalid_route,
lnm_err_overlapping_route,
} lnm_err; } lnm_err;
typedef struct lnm_loop lnm_http_loop; typedef struct lnm_loop lnm_http_loop;

View File

@ -13,6 +13,7 @@ typedef enum lnm_http_method {
lnm_http_method_patch, lnm_http_method_patch,
lnm_http_method_delete, lnm_http_method_delete,
lnm_http_method_head, lnm_http_method_head,
lnm_http_method_total,
} lnm_http_method; } lnm_http_method;
extern const char *lnm_http_status_names[][32]; extern const char *lnm_http_status_names[][32];

View File

@ -0,0 +1,39 @@
#ifndef LNM_HTTP_ROUTER
#define LNM_HTTP_ROUTER
#include "lnm/common.h"
#include "lnm/http/consts.h"
typedef struct lnm_http_route lnm_http_route;
typedef struct lnm_http_router lnm_http_router;
typedef enum lnm_http_route_err {
lnm_http_route_err_match = 0,
lnm_http_route_err_unknown_route = 1,
lnm_http_route_err_unknown_method = 2,
} lnm_http_route_err;
/**
* Allocate and initialize a new http_router.
*/
lnm_err lnm_http_router_init(lnm_http_router **out);
void lnm_http_router_free(lnm_http_router *router);
lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
lnm_http_method method, const char *path);
/**
* Add all of the child router's routes to the parent router, under the given
* route prefix.
*/
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,
const lnm_http_router *router,
lnm_http_method method,
const char *path);
#endif

View File

@ -0,0 +1,21 @@
#ifndef LNM_HTTP_ROUTER_INTERNAL
#define LNM_HTTP_ROUTER_INTERNAL
#include "lnm/common.h"
#include "lnm/http/consts.h"
#include "lnm/http/router.h"
struct lnm_http_route {};
struct lnm_http_router {
struct lnm_http_router *exact_children[128];
struct lnm_http_router *single_segment_child;
lnm_http_route *routes[lnm_http_method_total];
bool represents_route;
};
lnm_err lnm_http_route_init(lnm_http_route **out);
void lnm_http_route_free(lnm_http_route *route);
#endif

View File

@ -0,0 +1,145 @@
#include <stdio.h>
#include <string.h>
#include "lnm/common.h"
#include "lnm/http/router.h"
#include "lnm/http/router_internal.h"
lnm_err lnm_http_router_init(lnm_http_router **out) {
lnm_http_router *router = calloc(1, sizeof(lnm_http_router));
if (router == NULL) {
return lnm_err_failed_alloc;
}
*out = router;
return lnm_err_ok;
}
lnm_err lnm_http_route_init(lnm_http_route **out) {
lnm_http_route *route = calloc(1, sizeof(lnm_http_route));
if (route == NULL) {
return lnm_err_failed_alloc;
}
*out = route;
return lnm_err_ok;
}
static bool is_ascii(const char *s) {
while (*s != '0') {
if (*s > 127) {
return false;
}
s++;
}
return true;
}
lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
lnm_http_method method, const char *path) {
if (path[0] != '/' || !is_ascii(path)) {
return lnm_err_invalid_route;
}
while (*path != '\0') {
unsigned char c = *path;
switch (c) {
case ':':
if (http_router->single_segment_child == NULL) {
LNM_RES(lnm_http_router_init(&http_router->single_segment_child));
}
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;
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]));
}
http_router = http_router->exact_children[c];
path++;
break;
}
}
if (http_router->routes[method] != NULL) {
return lnm_err_overlapping_route;
}
LNM_RES(lnm_http_route_init(&http_router->routes[method]));
http_router->represents_route = true;
if (out != NULL) {
*out = http_router->routes[method];
}
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') {
if (!router->represents_route) {
return lnm_http_route_err_unknown_route;
}
if (out != NULL) {
*out = router->routes[method];
}
return router->routes[method] == NULL ? lnm_http_route_err_unknown_method
: lnm_http_route_err_match;
}
lnm_http_route_err res = lnm_http_route_err_unknown_route;
lnm_http_router *exact_router = router->exact_children[(unsigned char)*path];
if (exact_router != NULL) {
lnm_http_route_err sub_res =
lnm_http_router_route(out, exact_router, method, path + 1);
if (sub_res == lnm_http_route_err_match) {
return lnm_http_route_err_match;
}
res = LNM_MAX(res, sub_res);
}
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;
lnm_http_route_err sub_res =
lnm_http_router_route(out, exact_router, method, path);
if (sub_res == lnm_http_route_err_match) {
return lnm_http_route_err_match;
}
res = LNM_MAX(res, sub_res);
}
return res;
}

21
test/routing.c 100644
View File

@ -0,0 +1,21 @@
#include "test.h"
#include "lnm/http/router.h"
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_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_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);
}
TEST_LIST = {
{ "routing simple", test_routing_simple },
{ NULL, NULL }
};

1839
test/test.h 100644

File diff suppressed because it is too large Load Diff