diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index 33aa720..45f39df 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -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;