aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2022-05-25 17:22:08 -0500
committerMichael McVady <femtonaut@gmail.com>2022-05-25 17:22:08 -0500
commit197c6663712ce20d693c693d54cafdbded8c2f3e (patch)
tree8bef1f19ce63c40163a9a6b9acef6a7d002fbb2c /src
parent9e22f355a9e811fba2503133591099a35f9b1fe3 (diff)
Add catch-all HTTP redirect
Diffstat (limited to 'src')
-rw-r--r--src/clog.c32
1 files changed, 12 insertions, 20 deletions
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 *);
@@ -29,6 +31,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)
{
int err = 0;
@@ -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);