From 44ba4b053ed27c9b18a98269f2bb3f2ee4cffb65 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 2 Mar 2024 23:31:34 +0100 Subject: [PATCH] refactor(lander): partial migration to updated lnm framework --- .gitmodules | 3 ++ lnm | 1 + src/lander/lander_get.c | 9 +++-- src/lander/lander_post.c | 23 +++++------ src/main.c | 84 ++++++++++++++++++++++++---------------- 5 files changed, 71 insertions(+), 49 deletions(-) create mode 100644 .gitmodules create mode 160000 lnm diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2fe4b2f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lnm"] + path = lnm + url = https://git.rustybever.be/Chewing_Bever/lnm diff --git a/lnm b/lnm new file mode 160000 index 0000000..6eab5d6 --- /dev/null +++ b/lnm @@ -0,0 +1 @@ +Subproject commit 6eab5d616c2772a625b521e19d50020156c72ca2 diff --git a/src/lander/lander_get.c b/src/lander/lander_get.c index aaf35de..7d4a186 100644 --- a/src/lander/lander_get.c +++ b/src/lander/lander_get.c @@ -3,6 +3,7 @@ #include "lnm/http/consts.h" #include "lnm/http/loop.h" +#include "lnm/http/req.h" #include "lnm/log.h" #include "lnm/loop.h" #include "lsm/store.h" @@ -102,9 +103,8 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) { lnm_http_loop_gctx *gctx = ctx->g; lander_gctx *c_gctx = gctx->c; - const char *key_s = - &ctx->req.buf.s[ctx->req.path.o + ctx->req.path.groups[1].rm_so]; - int key_len = ctx->req.path.groups[1].rm_eo - ctx->req.path.groups[1].rm_so; + const char *key_s; + size_t key_len = lnm_http_req_route_segment(&key_s, &ctx->req, "key"); lsm_str *key; lsm_str_init_copy_n(&key, (char *)key_s, key_len); @@ -138,6 +138,9 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) { case lander_entry_type_file: res = lander_get_file(conn); break; + default: + ctx->res.status = lnm_http_status_internal_server_error; + res = lnm_http_step_err_res; } return res; diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index 8b14410..f5e877a 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -1,6 +1,7 @@ #include #include "lnm/loop.h" +#include "lnm/http/req.h" #include "lsm/store.h" #include "lander.h" @@ -20,28 +21,24 @@ static void randomize_key(char *key, int len) { * * @return true on success, false otherwise */ -bool lander_insert_entry(lnm_http_loop_ctx *ctx) { +bool lander_insert_entry(lnm_http_loop_ctx *ctx, bool secure) { lnm_http_loop_gctx *gctx = ctx->g; lander_gctx *c_gctx = gctx->c; lander_ctx *c_ctx = ctx->c; - lsm_str *key; - int key_len; + const char *key_s; + size_t key_len = lnm_http_req_route_segment(&key_s, &ctx->req, "key"); - if (ctx->req.path.groups[2].rm_eo == ctx->req.path.groups[2].rm_so) { + lsm_str *key; + + if (key_len == 0) { // Generate a random key to insert - bool secure = - (ctx->req.path.groups[1].rm_eo - ctx->req.path.groups[1].rm_so) == 1; key_len = secure ? 16 : 4; char *key_s = malloc((key_len + 1) * sizeof(char)); randomize_key(key_s, key_len); lsm_str_init(&key, key_s); } else { - const char *key_s = - &ctx->req.buf.s[ctx->req.path.o + ctx->req.path.groups[2].rm_so]; - key_len = ctx->req.path.groups[2].rm_eo - ctx->req.path.groups[2].rm_so; - lsm_str_init_copy_n(&key, key_s, key_len); } @@ -73,7 +70,7 @@ lnm_http_step_err lander_post_redirect(lnm_http_conn *conn) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - if (!lander_insert_entry(ctx)) { + if (!lander_insert_entry(ctx, false)) { return lnm_http_step_err_res; } @@ -98,7 +95,7 @@ lnm_http_step_err lander_post_paste(lnm_http_conn *conn) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - if (!lander_insert_entry(ctx)) { + if (!lander_insert_entry(ctx, false)) { return lnm_http_step_err_res; } @@ -113,7 +110,7 @@ lnm_http_step_err lander_post_file(lnm_http_conn *conn) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - if (!lander_insert_entry(ctx)) { + if (!lander_insert_entry(ctx, false)) { return lnm_http_step_err_res; } diff --git a/src/main.c b/src/main.c index 6cacf84..28d8ef4 100644 --- a/src/main.c +++ b/src/main.c @@ -12,49 +12,67 @@ const char *lander_server = "lander/" LANDER_VERSION; lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) { lnm_http_loop *hl; - lnm_http_step *step = NULL; - lnm_http_route *route; lnm_http_loop_init(&hl, gctx, lander_ctx_init, (lnm_http_ctx_reset_fn)lander_ctx_reset, (lnm_http_ctx_free_fn)lander_ctx_free); lnm_http_loop_set_api_key(hl, api_key); lnm_http_loop_set_server(hl, lander_server); - lnm_http_step_init(&step, lander_get_index); - lnm_http_route_init_literal(&route, lnm_http_method_get, "/", step); - lnm_http_loop_route_add(hl, route); + lnm_http_router *router; + lnm_http_router_init(&router); - lnm_http_step_init(&step, lander_get_entry); - lnm_http_route_init_regex(&route, lnm_http_method_get, "^/([^/]+)$", 1, step); - lnm_http_loop_route_add(hl, route); + lnm_http_route *route; + lnm_http_router_add(&route, router, lnm_http_method_get, "/"); + lnm_http_route_step_append(route, lander_get_index, false); - lnm_http_step_init(&step, lnm_http_loop_step_auth); - lnm_http_route_init_regex(&route, lnm_http_method_post, "^/s(l?)/([^/]*)$", 2, - step); - lnm_http_step_append(&step, step, lander_post_redirect); - lnm_http_step_append(&step, step, lnm_http_loop_step_body_to_buf); - lnm_http_step_append(&step, step, lander_post_redirect_body_to_attr); - lnm_http_loop_route_add(hl, route); + lnm_http_router_add(&route, router, lnm_http_method_get, "/:key"); + lnm_http_route_step_append(route, lander_get_entry, false); - lnm_http_step_init(&step, lnm_http_loop_step_auth); - lnm_http_route_init_regex(&route, lnm_http_method_post, "^/p(l?)/([^/]*)$", 2, - step); - lnm_http_step_append(&step, step, lander_post_paste); - lnm_http_step_append(&step, step, lander_stream_body_to_entry); - lnm_http_loop_route_add(hl, route); + lnm_http_router_add(&route, router, lnm_http_method_post, "/s/:key"); + lnm_http_route_step_append(route, lnm_http_loop_step_auth, false); + lnm_http_route_step_append(route, lander_post_redirect, false); + lnm_http_route_step_append(route, lnm_http_loop_step_body_to_buf, false); + lnm_http_route_step_append(route, lander_post_redirect_body_to_attr, false); - lnm_http_step_init(&step, lnm_http_loop_step_auth); - lnm_http_route_init_regex(&route, lnm_http_method_post, "^/f(l?)/([^/]*)$", 2, - step); - lnm_http_step_append(&step, step, lander_post_file); - lnm_http_step_append(&step, step, lander_stream_body_to_entry); - lnm_http_loop_route_add(hl, route); + lnm_http_router_add(&route, router, lnm_http_method_post, "/p/:key"); + lnm_http_route_step_append(route, lnm_http_loop_step_auth, false); + lnm_http_route_step_append(route, lander_post_paste, false); + lnm_http_route_step_append(route, lander_stream_body_to_entry, false); - lnm_http_step_init(&step, lnm_http_loop_step_auth); - lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1, - step); - lnm_http_step_append(&step, step, lander_remove_entry); - lnm_http_loop_route_add(hl, route); + lnm_http_router_add(&route, router, lnm_http_method_post, "/f/:key"); + lnm_http_route_step_append(route, lnm_http_loop_step_auth, false); + lnm_http_route_step_append(route, lander_post_file, false); + lnm_http_route_step_append(route, lander_stream_body_to_entry, false); + + /* lnm_http_step_init(&step, lnm_http_loop_step_auth); */ + /* lnm_http_route_init_regex(&route, lnm_http_method_post, "^/s(l?)/([^/]*)$", 2, */ + /* step); */ + /* lnm_http_step_append(&step, step, lander_post_redirect); */ + /* lnm_http_step_append(&step, step, lnm_http_loop_step_body_to_buf); */ + /* lnm_http_step_append(&step, step, lander_post_redirect_body_to_attr); */ + /* lnm_http_loop_route_add(hl, route); */ + + /* lnm_http_step_init(&step, lnm_http_loop_step_auth); */ + /* lnm_http_route_init_regex(&route, lnm_http_method_post, "^/p(l?)/([^/]*)$", 2, */ + /* step); */ + /* lnm_http_step_append(&step, step, lander_post_paste); */ + /* lnm_http_step_append(&step, step, lander_stream_body_to_entry); */ + /* lnm_http_loop_route_add(hl, route); */ + + /* lnm_http_step_init(&step, lnm_http_loop_step_auth); */ + /* lnm_http_route_init_regex(&route, lnm_http_method_post, "^/f(l?)/([^/]*)$", 2, */ + /* step); */ + /* lnm_http_step_append(&step, step, lander_post_file); */ + /* lnm_http_step_append(&step, step, lander_stream_body_to_entry); */ + /* lnm_http_loop_route_add(hl, route); */ + + /* lnm_http_step_init(&step, lnm_http_loop_step_auth); */ + /* lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1, */ + /* step); */ + /* lnm_http_step_append(&step, step, lander_remove_entry); */ + /* lnm_http_loop_route_add(hl, route); */ + + lnm_http_loop_router_set(hl, router); return hl; } @@ -104,7 +122,7 @@ int main() { lnm_linfo("main", "Store loaded containing %lu entries", lsm_store_size(c_gctx->store)); lnm_http_loop *hl = loop_init(c_gctx, api_key); - lnm_http_loop_run(hl, port, 1); + lnm_http_loop_run(hl, port, 1, 0); /* http_loop *hl = http_loop_init( */ /* lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]),