aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-02 11:46:39 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-03 10:54:19 -0600
commit832ab13c9dd74fe30533f345d7e9209dff8c89d1 (patch)
tree456e70ca3834e3f6e49ed7d0d335f1bc805c4caf /src
parent2137b1e4b3fd147fc53a614cb1358bd3d50341bf (diff)
Use enum for response content type
Diffstat (limited to 'src')
-rw-r--r--src/clog.c30
1 files changed, 16 insertions, 14 deletions
diff --git a/src/clog.c b/src/clog.c
index 5c0037f..3ec3ee3 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -20,16 +20,25 @@ KORE_SECCOMP_FILTER("clog",
KORE_SYSCALL_ALLOW(uname)
)
+enum request_type { JSON = 0, HTML = 1 };
+
struct post_request {
struct http_request *req;
const char *resource;
- // FIXME: use enum
- unsigned int type; // 0 JSON; 1 HTML
+ enum request_type type;
int resp_status;
struct kore_buf *resp_buf;
};
+static const char *accept_json = "application/json";
+static const char *database = "db";
+
+static const char * const error_msg[] = {
+ [HTTP_STATUS_NOT_FOUND] = "Resource not found.",
+ [HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.",
+};
+
void post_request_init(struct post_request *post_req);
void post_request_cleanup(struct post_request *post_req);
@@ -45,19 +54,11 @@ int render_posts_json(struct post_request *post_req);
static void process_md_output(const MD_CHAR *, MD_SIZE size, void *);
static int render_md(const char *, struct kore_buf *);
-static const char *accept_json = "application/json";
-static const char *database = "db";
-
-static const char * const error_msg[] = {
- [HTTP_STATUS_NOT_FOUND] = "Resource not found.",
- [HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.",
-};
-
void post_request_init(struct post_request *post_req)
{
post_req->req = NULL;
post_req->resource = NULL;
- post_req->type = 1;
+ post_req->type = HTML;
post_req->resp_status = HTTP_STATUS_OK;
post_req->resp_buf = kore_buf_alloc(0);
@@ -118,15 +119,15 @@ render_posts(struct http_request *req, const char *resource)
kore_log(LOG_DEBUG, "Accept: %s", accept);
if (strncmp(accept, accept_json, sizeof(*accept_json)) == 0)
{
- post_req.type = 0;
+ post_req.type = JSON;
}
else
{
- post_req.type = 1;
+ post_req.type = HTML;
}
}
- if (post_req.type == 0)
+ if (post_req.type == JSON)
{
http_response_header(post_req.req, "content-type", "application/json; charset=utf-8");
(void) render_posts_json(&post_req);
@@ -246,6 +247,7 @@ render_posts_html(struct post_request *post_req)
// Allocate a buffer to render the markdown as HTML into.
struct kore_buf *html_buf = kore_buf_alloc(0);
+
// Render MD.
err = render_md(body, html_buf);
if (err == KORE_RESULT_ERROR)