feat(lander): support sendind extra attributes as custom headers

lsm
Jef Roosens 2023-11-12 14:12:13 +01:00
parent 7fac278ead
commit 70f622d9f3
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 41 additions and 1 deletions

View File

@ -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

View File

@ -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;
}