feat(lander): server content-type header for file entries

lsm
Jef Roosens 2023-11-12 14:29:46 +01:00
parent 70f622d9f3
commit c026e13c44
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 41 additions and 6 deletions

View File

@ -68,4 +68,6 @@ bool lander_post_file_lsm(event_loop_conn *conn);
*/ */
bool lander_headers_to_attrs(event_loop_conn *conn); bool lander_headers_to_attrs(event_loop_conn *conn);
bool lander_attrs_to_headers(event_loop_conn *conn);
#endif #endif

View File

@ -44,6 +44,7 @@ elif [ "$1" = f ]; then
-w "${URL}%header{location}" \ -w "${URL}%header{location}" \
-XPOST \ -XPOST \
-H "X-Api-Key: $API_KEY" \ -H "X-Api-Key: $API_KEY" \
-H "X-Lander-Content-Type: $(file --mime-type --brief $2)" \
--data-binary @"$2" \ --data-binary @"$2" \
"$URL/f/$3" "$URL/f/$3"
fi fi

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include "http/types.h"
#include "http_loop.h" #include "http_loop.h"
#include "lander.h" #include "lander.h"
#include "lsm/store.h" #include "lsm/store.h"
@ -15,7 +16,7 @@ http_route lander_routes[] = {
.type = http_route_regex, .type = http_route_regex,
.method = http_get, .method = http_get,
.path = "^/([^/]+)$", .path = "^/([^/]+)$",
.steps = {lander_get_entry_lsm, NULL}, .steps = {lander_get_entry_lsm, lander_attrs_to_headers, NULL},
.steps_res = {http_loop_step_write_header, lander_stream_body_to_client, .steps_res = {http_loop_step_write_header, lander_stream_body_to_client,
NULL}, NULL},
}, },
@ -48,7 +49,8 @@ http_route lander_routes[] = {
.method = http_post, .method = http_post,
.path = "^/f(l?)/([^/]*)$", .path = "^/f(l?)/([^/]*)$",
.steps = {http_loop_step_auth, http_loop_step_parse_content_length, .steps = {http_loop_step_auth, http_loop_step_parse_content_length,
lander_post_file_lsm, lander_headers_to_attrs, 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, .steps_res = {http_loop_step_write_header, http_loop_step_write_body,
NULL}}, NULL}},
}; };
@ -56,9 +58,11 @@ http_route lander_routes[] = {
struct { struct {
char *header; char *header;
lander_attr_type attr_type; lander_attr_type attr_type;
http_header header_type;
} header_to_attr_type[] = { } header_to_attr_type[] = {
{ "X-Lander-Content-Type", lander_attr_type_content_type }, {"X-Lander-Content-Type", lander_attr_type_content_type,
{ NULL, 0 }, http_header_content_type},
{NULL, 0},
}; };
void *lander_gctx_init() { return calloc(1, sizeof(lander_gctx)); } void *lander_gctx_init() { return calloc(1, sizeof(lander_gctx)); }
@ -87,11 +91,13 @@ bool lander_headers_to_attrs(event_loop_conn *conn) {
int j = 0; int j = 0;
while (header_to_attr_type[j].header != NULL) { while (header_to_attr_type[j].header != NULL) {
if (strncmp(header->name, header_to_attr_type[j].header, header->name_len) == 0) { if (strncmp(header->name, header_to_attr_type[j].header,
header->name_len) == 0) {
lsm_str *value; lsm_str *value;
lsm_str_init_copy_n(&value, (char *)header->value, header->value_len); 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); lsm_entry_attr_insert(c_ctx->entry, header_to_attr_type[j].attr_type,
value);
break; break;
} }
@ -102,3 +108,27 @@ bool lander_headers_to_attrs(event_loop_conn *conn) {
return true; return true;
} }
bool lander_attrs_to_headers(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx;
lander_ctx *c_ctx = ctx->c;
int j = 0;
lsm_str *value;
while (header_to_attr_type[j].header != NULL) {
if (lsm_entry_attr_get(&value, c_ctx->entry,
header_to_attr_type[j].attr_type) == lsm_error_ok) {
char *buf = malloc(lsm_str_len(value) + 1);
memcpy(buf, lsm_str_ptr(value), lsm_str_len(value));
buf[lsm_str_len(value)] = '\0';
http_res_add_header(&ctx->res, header_to_attr_type[j].header_type, buf,
true);
}
j++;
}
return true;
}

View File

@ -92,9 +92,11 @@ bool lander_get_entry_lsm(event_loop_conn *conn) {
break; break;
case lsm_error_not_found: case lsm_error_not_found:
ctx->res.status = http_not_found; ctx->res.status = http_not_found;
conn->state = event_loop_conn_state_res;
return true; return true;
default: default:
ctx->res.status = http_internal_server_error; ctx->res.status = http_internal_server_error;
conn->state = event_loop_conn_state_res;
return true; return true;
} }