feat: add post redirect route

c-web-server
Jef Roosens 2023-05-29 23:55:27 +02:00
parent 1e42dc04dc
commit 94b07caeb3
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 29 additions and 1 deletions

View File

@ -4,7 +4,35 @@
bool lander_post_redirect(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx;
info("%.*s", ctx->req.body_len, ctx->req.body);
// Allocate a new buffer to pass to the trie
char *url = malloc(ctx->req.body_len + 1);
memcpy(url, ctx->req.body, ctx->req.body_len);
url[ctx->req.body_len] = '\0';
char *key;
Entry *new_entry = entry_new(Redirect, url);
TrieExitCode res = trie_add_random(ctx->g->trie, &key, new_entry, false);
if (res != Ok) {
error("trie_add_random failed with exit code %i", res);
ctx->res.status = http_internal_server_error;
conn->state = event_loop_conn_state_res;
return true;
}
// Add a slash to the key and add it as the location header
size_t key_len = strlen(key);
char *buf = malloc(key_len + 2);
memcpy(&buf[1], key, key_len);
buf[0] = '/';
buf[key_len + 1] = '\0';
free(key);
http_loop_res_add_header(ctx, http_header_location, buf, true);
conn->state = event_loop_conn_state_res;