Compare commits
No commits in common. "de4e509c9ce82d2d1376f7b15fe0a08dfb736703" and "1f63f06e0c733d069457a30ad3852c11811f1612" have entirely different histories.
de4e509c9c
...
1f63f06e0c
2
Makefile
2
Makefile
|
@ -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$(SRC_DIR)/_include \
|
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \
|
||||||
-c $< -o $@
|
-c $< -o $@
|
||||||
|
|
||||||
# =====EXAMPLES=====
|
# =====EXAMPLES=====
|
||||||
|
|
|
@ -32,8 +32,6 @@ 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;
|
||||||
|
|
|
@ -13,7 +13,6 @@ 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];
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
#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
|
|
|
@ -1,21 +0,0 @@
|
||||||
#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
|
|
|
@ -1,145 +0,0 @@
|
||||||
#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;
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
#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
1839
test/test.h
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue