From 94da7584aa655ab9b0c95620572e2257e1ec3930 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 3 Mar 2024 09:41:56 +0100 Subject: [PATCH] refactor(lander): fully integrate new lnm framework router --- include/lander.h | 6 ++++ src/lander/lander_delete.c | 6 ++-- src/lander/lander_post.c | 39 ++++++++++++++++++----- src/main.c | 64 +++++++++++++++++++++----------------- 4 files changed, 77 insertions(+), 38 deletions(-) diff --git a/include/lander.h b/include/lander.h index ae2f714..7b418bc 100644 --- a/include/lander.h +++ b/include/lander.h @@ -43,8 +43,12 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn); lnm_http_step_err lander_post_redirect(lnm_http_conn *conn); +lnm_http_step_err lander_post_redirect_secure(lnm_http_conn *conn); + lnm_http_step_err lander_post_paste(lnm_http_conn *conn); +lnm_http_step_err lander_post_paste_secure(lnm_http_conn *conn); + lnm_http_step_err lander_stream_body_to_entry(lnm_http_conn *conn); lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn); @@ -53,6 +57,8 @@ lnm_http_step_err lander_remove_entry(lnm_http_conn *conn); lnm_http_step_err lander_post_file(lnm_http_conn *conn); +lnm_http_step_err lander_post_file_secure(lnm_http_conn *conn); + /** * Store the requested header as an attribute, if it's present. */ diff --git a/src/lander/lander_delete.c b/src/lander/lander_delete.c index f773ab5..f13326b 100644 --- a/src/lander/lander_delete.c +++ b/src/lander/lander_delete.c @@ -1,3 +1,4 @@ +#include "lnm/http/req.h" #include "lnm/loop.h" #include "lander.h" @@ -8,9 +9,8 @@ lnm_http_step_err lander_remove_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); diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index f5e877a..a5beaaf 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -1,7 +1,7 @@ #include -#include "lnm/loop.h" #include "lnm/http/req.h" +#include "lnm/loop.h" #include "lsm/store.h" #include "lander.h" @@ -66,11 +66,12 @@ bool lander_insert_entry(lnm_http_loop_ctx *ctx, bool secure) { return true; } -lnm_http_step_err lander_post_redirect(lnm_http_conn *conn) { +static lnm_http_step_err __lander_post_redirect(lnm_http_conn *conn, + bool secure) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - if (!lander_insert_entry(ctx, false)) { + if (!lander_insert_entry(ctx, secure)) { return lnm_http_step_err_res; } @@ -80,6 +81,14 @@ lnm_http_step_err lander_post_redirect(lnm_http_conn *conn) { return lnm_http_step_err_done; } +lnm_http_step_err lander_post_redirect(lnm_http_conn *conn) { + return __lander_post_redirect(conn, false); +} + +lnm_http_step_err lander_post_redirect_secure(lnm_http_conn *conn) { + return __lander_post_redirect(conn, true); +} + lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; @@ -91,11 +100,11 @@ lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn) { return lnm_http_step_err_done; } -lnm_http_step_err lander_post_paste(lnm_http_conn *conn) { +static lnm_http_step_err __lander_post_paste(lnm_http_conn *conn, bool secure) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - if (!lander_insert_entry(ctx, false)) { + if (!lander_insert_entry(ctx, secure)) { return lnm_http_step_err_res; } @@ -106,11 +115,19 @@ lnm_http_step_err lander_post_paste(lnm_http_conn *conn) { return lnm_http_step_err_done; } -lnm_http_step_err lander_post_file(lnm_http_conn *conn) { +lnm_http_step_err lander_post_paste(lnm_http_conn *conn) { + return __lander_post_paste(conn, false); +} + +lnm_http_step_err lander_post_paste_secure(lnm_http_conn *conn) { + return __lander_post_paste(conn, true); +} + +static lnm_http_step_err __lander_post_file(lnm_http_conn *conn, bool secure) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - if (!lander_insert_entry(ctx, false)) { + if (!lander_insert_entry(ctx, secure)) { return lnm_http_step_err_res; } @@ -122,3 +139,11 @@ lnm_http_step_err lander_post_file(lnm_http_conn *conn) { return lnm_http_step_err_done; } + +lnm_http_step_err lander_post_file(lnm_http_conn *conn) { + return __lander_post_file(conn, false); +} + +lnm_http_step_err lander_post_file_secure(lnm_http_conn *conn) { + return __lander_post_file(conn, true); +} diff --git a/src/main.c b/src/main.c index 28d8ef4..4ac8911 100644 --- a/src/main.c +++ b/src/main.c @@ -28,50 +28,58 @@ lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) { lnm_http_router_add(&route, router, lnm_http_method_get, "/:key"); lnm_http_route_step_append(route, lander_get_entry, false); + lnm_http_router_add(&route, router, lnm_http_method_delete, "/:key"); + lnm_http_route_step_append(route, lnm_http_loop_step_auth, false); + lnm_http_route_step_append(route, lander_remove_entry, false); + + lnm_http_router_add(&route, router, lnm_http_method_post, "/s/"); + 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_router_add(&route, router, lnm_http_method_post, "/sl/"); + lnm_http_route_step_append(route, lnm_http_loop_step_auth, false); + lnm_http_route_step_append(route, lander_post_redirect_secure, 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_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_router_add(&route, router, lnm_http_method_post, "/p/"); + 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_router_add(&route, router, lnm_http_method_post, "/pl/"); + lnm_http_route_step_append(route, lnm_http_loop_step_auth, false); + lnm_http_route_step_append(route, lander_post_paste_secure, false); + lnm_http_route_step_append(route, lander_stream_body_to_entry, false); + 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_router_add(&route, router, lnm_http_method_post, "/f/"); + 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_router_add(&route, router, lnm_http_method_post, "/fl/"); + lnm_http_route_step_append(route, lnm_http_loop_step_auth, false); + lnm_http_route_step_append(route, lander_post_file_secure, false); + lnm_http_route_step_append(route, lander_stream_body_to_entry, false); + 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;