From 197c6663712ce20d693c693d54cafdbded8c2f3e Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Wed, 25 May 2022 17:22:08 -0500 Subject: Add catch-all HTTP redirect --- src/clog.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/clog.c b/src/clog.c index 10a453c..0e9bd0e 100644 --- a/src/clog.c +++ b/src/clog.c @@ -19,6 +19,8 @@ KORE_SECCOMP_FILTER("clog", KORE_SYSCALL_ALLOW(uname) ) +int redirect(struct http_request *); + int post(struct http_request *); int posts(struct http_request *); int render_posts_html(struct http_request *, const char *); @@ -28,6 +30,15 @@ static int render_md(char *, struct kore_buf *); static const char *database = "db"; +int +redirect(struct http_request *req) +{ + http_response_header(req, "Location", "/"); + http_response(req, HTTP_STATUS_MOVED_PERMANENTLY, NULL, 0); + + return KORE_RESULT_OK; +} + int post(struct http_request *req) { @@ -75,26 +86,20 @@ posts(struct http_request *req) int render_posts_html(struct http_request *req, const char *resource) { - // Errors int err; - // Buffer for response struct kore_buf *resp_buf = kore_buf_alloc(0); - // SQL vars struct kore_pgsql sql; int row, rows; - // Post attributes/query results. char *id, *title, *created_at, *body; - // Start writing HTML. kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html); - // Setup SQL kore_pgsql_init(&sql); - // Initialise our kore_pgsql data structure with the database name + // Initialize our kore_pgsql data structure with the database name // we want to connect to (note that we registered this earlier with // kore_pgsql_register()). We also say we will perform a synchronous // query (KORE_PGSQL_SYNC). @@ -181,12 +186,10 @@ render_posts_html(struct http_request *req, const char *resource) out: ; - // Finish building response. kore_buf_append(resp_buf, asset_index_end_html, asset_len_index_end_html); http_response_header(req, "content-type", "text/html; charset=utf-8"); http_response(req, HTTP_STATUS_OK, resp_buf->data, resp_buf->offset); - // Cleanup. kore_pgsql_cleanup(&sql); kore_buf_free(resp_buf); @@ -196,26 +199,17 @@ out: ; int render_posts_json(struct http_request *req, const char *resource) { - // Errors int err; - // Buffer for response struct kore_buf *resp_buf = kore_buf_alloc(0); - // SQL vars struct kore_pgsql sql; int rows; - // JSON results form PostgreSQL. char *json; - // Setup SQL kore_pgsql_init(&sql); - // Initialise our kore_pgsql data structure with the database name - // we want to connect to (note that we registered this earlier with - // kore_pgsql_register()). We also say we will perform a synchronous - // query (KORE_PGSQL_SYNC). err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC); if (err == KORE_RESULT_ERROR) { @@ -276,11 +270,9 @@ render_posts_json(struct http_request *req, const char *resource) out: ; - // Finish building response. http_response_header(req, "content-type", "application/json; charset=utf-8"); http_response(req, HTTP_STATUS_OK, resp_buf->data, resp_buf->offset); - // Cleanup. kore_pgsql_cleanup(&sql); kore_buf_free(resp_buf); -- cgit v1.2.3