Compare commits
2 Commits
e29e02ff85
...
fbd41f7e4e
Author | SHA1 | Date |
---|---|---|
Jef Roosens | fbd41f7e4e | |
Jef Roosens | 9fa009ccf4 |
|
@ -0,0 +1,44 @@
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "lnm/log.h"
|
||||||
|
#include "lnm/loop.h"
|
||||||
|
#include "lnm/http/loop.h"
|
||||||
|
|
||||||
|
lnm_err ctx_init(void **c_ctx, void *gctx) {
|
||||||
|
*c_ctx = NULL;
|
||||||
|
|
||||||
|
return lnm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ctx_reset(void *c_ctx) {}
|
||||||
|
void ctx_free(void *c_ctx) {}
|
||||||
|
|
||||||
|
lnm_http_step_err print_step(lnm_http_conn *conn) {
|
||||||
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
|
|
||||||
|
const lnm_http_route_match_segment *seg = lnm_http_route_match_get(&ctx->match, "key");
|
||||||
|
lnm_linfo("main", "key: %.*s", seg->len, ctx->req.buf.s + ctx->req.path.o + seg->start);
|
||||||
|
return lnm_http_step_err_done;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
lnm_http_loop *hl;
|
||||||
|
|
||||||
|
lnm_http_loop_init(&hl, NULL, ctx_init,
|
||||||
|
ctx_reset,
|
||||||
|
ctx_free);
|
||||||
|
|
||||||
|
lnm_http_router *router;
|
||||||
|
lnm_http_router_init(&router);
|
||||||
|
|
||||||
|
lnm_http_route *route;
|
||||||
|
lnm_http_router_add(&route, router, lnm_http_method_get, "/:key");
|
||||||
|
lnm_http_route_step_append(route, print_step, false);
|
||||||
|
|
||||||
|
lnm_http_loop_router_set(hl, router);
|
||||||
|
|
||||||
|
lnm_log_init_global();
|
||||||
|
lnm_log_register_stdout(lnm_log_level_debug);
|
||||||
|
|
||||||
|
printf("res = %i\n", lnm_http_loop_run(hl, 8080, 1, 0));
|
||||||
|
}
|
|
@ -130,7 +130,7 @@ typedef struct lnm_http_loop_ctx {
|
||||||
lnm_http_loop_state state;
|
lnm_http_loop_state state;
|
||||||
lnm_http_req req;
|
lnm_http_req req;
|
||||||
lnm_http_res res;
|
lnm_http_res res;
|
||||||
const lnm_http_route *route;
|
lnm_http_route_match match;
|
||||||
lnm_http_step *cur_step;
|
lnm_http_step *cur_step;
|
||||||
lnm_http_loop_gctx *g;
|
lnm_http_loop_gctx *g;
|
||||||
void *c;
|
void *c;
|
||||||
|
|
|
@ -42,7 +42,6 @@ void lnm_http_loop_ctx_reset(lnm_http_loop_ctx *ctx) {
|
||||||
lnm_http_req_reset(&ctx->req);
|
lnm_http_req_reset(&ctx->req);
|
||||||
lnm_http_res_reset(&ctx->res);
|
lnm_http_res_reset(&ctx->res);
|
||||||
|
|
||||||
ctx->route = NULL;
|
|
||||||
ctx->cur_step = NULL;
|
ctx->cur_step = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,13 +58,10 @@ void lnm_http_loop_process_route(lnm_http_conn *conn) {
|
||||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
const lnm_http_loop_gctx *gctx = ctx->g;
|
const lnm_http_loop_gctx *gctx = ctx->g;
|
||||||
|
|
||||||
lnm_http_route_match match;
|
switch (lnm_http_router_route(&ctx->match, gctx->router, ctx->req.method,
|
||||||
|
|
||||||
switch (lnm_http_router_route(&match, gctx->router, ctx->req.method,
|
|
||||||
ctx->req.buf.s + ctx->req.path.o)) {
|
ctx->req.buf.s + ctx->req.path.o)) {
|
||||||
case lnm_http_route_err_match:
|
case lnm_http_route_err_match:
|
||||||
ctx->route = match.route;
|
ctx->cur_step = ctx->match.route->step;
|
||||||
ctx->cur_step = match.route->step;
|
|
||||||
ctx->state = lnm_http_loop_state_parse_headers;
|
ctx->state = lnm_http_loop_state_parse_headers;
|
||||||
break;
|
break;
|
||||||
case lnm_http_route_err_unknown_method:
|
case lnm_http_route_err_unknown_method:
|
||||||
|
|
|
@ -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));
|
||||||
|
|
|
@ -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) {
|
||||||
|
@ -53,10 +62,10 @@ lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case ':': {
|
case ':': {
|
||||||
const char *next_slash_ptr = strchr(path, '/');
|
const char *next_slash_ptr = strchr(path + 1, '/');
|
||||||
const char *new_path =
|
const char *new_path =
|
||||||
next_slash_ptr == NULL ? strchr(path, '\0') : next_slash_ptr;
|
next_slash_ptr == NULL ? strchr(path + 1, '\0') : next_slash_ptr;
|
||||||
size_t key_len = new_path - path - 1;
|
size_t key_len = new_path - (path + 1);
|
||||||
|
|
||||||
if (key_len == 0) {
|
if (key_len == 0) {
|
||||||
res = lnm_err_invalid_route;
|
res = lnm_err_invalid_route;
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
#include "lnm/http/router.h"
|
#include "lnm/http/loop.h"
|
||||||
|
|
||||||
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 = {
|
||||||
|
|
Loading…
Reference in New Issue