From 70f622d9f3329f565a7b229ab992c8c52265883f Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 12 Nov 2023 14:12:13 +0100 Subject: [PATCH] feat(lander): support sendind extra attributes as custom headers --- include/lander.h | 6 ++++++ src/lander/lander.c | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/lander.h b/include/lander.h index 2d8f8e5..1bb38d3 100644 --- a/include/lander.h +++ b/include/lander.h @@ -62,4 +62,10 @@ bool lander_remove_entry(event_loop_conn *conn); bool lander_post_file_lsm(event_loop_conn *conn); +/** + * Parse any custom headers and add them as attributes to the context's LSM + * entry + */ +bool lander_headers_to_attrs(event_loop_conn *conn); + #endif diff --git a/src/lander/lander.c b/src/lander/lander.c index ba34ea1..79518b0 100644 --- a/src/lander/lander.c +++ b/src/lander/lander.c @@ -48,11 +48,19 @@ http_route lander_routes[] = { .method = http_post, .path = "^/f(l?)/([^/]*)$", .steps = {http_loop_step_auth, http_loop_step_parse_content_length, - lander_post_file_lsm, lander_stream_body_to_entry, NULL}, + lander_post_file_lsm, lander_headers_to_attrs, lander_stream_body_to_entry, NULL}, .steps_res = {http_loop_step_write_header, http_loop_step_write_body, NULL}}, }; +struct { + char *header; + lander_attr_type attr_type; +} header_to_attr_type[] = { + { "X-Lander-Content-Type", lander_attr_type_content_type }, + { NULL, 0 }, +}; + void *lander_gctx_init() { return calloc(1, sizeof(lander_gctx)); } void *lander_ctx_init() { return calloc(1, sizeof(lander_ctx)); } @@ -68,3 +76,29 @@ void lander_ctx_reset(lander_ctx *ctx) { } void lander_ctx_free(lander_ctx *ctx) { free(ctx); } + +bool lander_headers_to_attrs(event_loop_conn *conn) { + http_loop_ctx *ctx = conn->ctx; + lander_ctx *c_ctx = ctx->c; + + for (size_t i = 0; i < ctx->req.num_headers; i++) { + struct phr_header *header = &ctx->req.headers[i]; + + int j = 0; + + while (header_to_attr_type[j].header != NULL) { + if (strncmp(header->name, header_to_attr_type[j].header, header->name_len) == 0) { + lsm_str *value; + lsm_str_init_copy_n(&value, (char *)header->value, header->value_len); + + lsm_entry_attr_insert(c_ctx->entry, header_to_attr_type[j].attr_type, value); + + break; + } + + j++; + } + } + + return true; +}