From 96658d75dfb8a5106366e211ed5fa70d77a0c740 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Mon, 2 Jan 2023 20:24:50 -0600 Subject: Formatting --- src/clog.c | 116 +++++++++++++++++++++++-------------------------------------- 1 file changed, 43 insertions(+), 73 deletions(-) (limited to 'src/clog.c') diff --git a/src/clog.c b/src/clog.c index 78ca73b..2ce9b88 100644 --- a/src/clog.c +++ b/src/clog.c @@ -23,7 +23,7 @@ KORE_SECCOMP_FILTER("clog", KORE_SYSCALL_ALLOW(uname) ) -enum request_type { JSON = 0, HTML = 1 }; +enum request_type { JSON, HTML }; struct post_request { struct http_request *req; @@ -56,28 +56,24 @@ int render_posts_query(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 *); -void post_request_init(struct post_request *post_req) -{ +void post_request_init(struct post_request *post_req) { post_req->req = NULL; post_req->resource = NULL; - post_req->type = HTML; + post_req->type = JSON; post_req->resp_status = HTTP_STATUS_OK; post_req->resp_buf = kore_buf_alloc(0); } -void post_request_cleanup(struct post_request *post_req) -{ - if (post_req->resp_buf != NULL) - { +void post_request_cleanup(struct post_request *post_req) { + if (post_req->resp_buf != NULL) { kore_buf_free(post_req->resp_buf); post_req->resp_buf = NULL; } } int -redirect(struct http_request *req) -{ +redirect(struct http_request *req) { http_response_header(req, "Location", "/"); http_response(req, HTTP_STATUS_MOVED_PERMANENTLY, NULL, 0); @@ -85,8 +81,7 @@ redirect(struct http_request *req) } int -post(struct http_request *req) -{ +post(struct http_request *req) { const char *resource = NULL; resource = req->path + strlen("/posts/"); @@ -96,14 +91,12 @@ post(struct http_request *req) } int -posts(struct http_request *req) -{ +posts(struct http_request *req) { return render_posts(req, NULL); } int -render_posts(struct http_request *req, const char *resource) -{ +render_posts(struct http_request *req, const char *resource) { int err = 0; const char *accept = NULL; @@ -116,45 +109,39 @@ render_posts(struct http_request *req, const char *resource) post_req.resource = resource; err = http_request_header(req, "accept", &accept); - if (err == KORE_RESULT_OK) - { + if (err == KORE_RESULT_OK) { kore_log(LOG_DEBUG, "Accept: %s", accept); if (strncmp(accept, accept_json, sizeof(*accept_json)) == 0) - { post_req.type = JSON; - } else - { post_req.type = HTML; - } } - if (post_req.type == JSON) - { + if (post_req.type == JSON) { http_response_header(post_req.req, "content-type", "application/json; charset=utf-8"); + (void) render_posts_query(&post_req); - if (post_req.resp_status != HTTP_STATUS_OK) - { + if (post_req.resp_status != HTTP_STATUS_OK) { kore_buf_appendf( post_req.resp_buf, (const char *) asset_error_json, error_msg[post_req.resp_status] ); } - } - else - { + + } else { http_response_header(post_req.req, "content-type", "text/html; charset=utf-8"); kore_buf_append(post_req.resp_buf, asset_index_begin_html, asset_len_index_begin_html); + (void) render_posts_query(&post_req); - if (post_req.resp_status != HTTP_STATUS_OK) - { + if (post_req.resp_status != HTTP_STATUS_OK) { kore_buf_appendf( post_req.resp_buf, (const char *) asset_error_html, error_msg[post_req.resp_status] ); } + kore_buf_append(post_req.resp_buf, asset_index_end_html, asset_len_index_end_html); } @@ -172,8 +159,7 @@ render_posts(struct http_request *req, const char *resource) int -render_posts_query(struct post_request *post_req) -{ +render_posts_query(struct post_request *post_req) { int err = 0; int row = 0, rows = 0; @@ -194,16 +180,14 @@ render_posts_query(struct post_request *post_req) // 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) - { + if (err == KORE_RESULT_ERROR) { post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR; kore_pgsql_logerror(&sql); goto out; } // Query for posts, check for error. - if (post_req->resource) - { + if (post_req->resource) { // Query a post. err = kore_pgsql_query_params( &sql, @@ -212,9 +196,7 @@ render_posts_query(struct post_request *post_req) 1, // param count KORE_PGSQL_PARAM_TEXT(post_req->resource) ); - } - else - { + } else { // Query all posts. err = kore_pgsql_query( &sql, @@ -222,26 +204,34 @@ render_posts_query(struct post_request *post_req) ); } - if (err == KORE_RESULT_ERROR) - { + if (err == KORE_RESULT_ERROR) { post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR; kore_pgsql_logerror(&sql); goto out; } - if (post_req->type == HTML) - { - // Iterate over posts and render them. - rows = kore_pgsql_ntuples(&sql); + if (post_req->type == JSON) { + // XXX Always tuples from the above Postgres queries, need to check the length for results. + if (kore_pgsql_getlength(&sql, 0, 0) == 0) { + post_req->resp_status = HTTP_STATUS_NOT_FOUND; + goto out; + } - if (rows == 0) - { + json = kore_pgsql_getvalue(&sql, 0, 0); + kore_buf_append( + post_req->resp_buf, + json, + strlen(json) + ); + } else { // post_req->type == HTML + rows = kore_pgsql_ntuples(&sql); + if (rows == 0) { post_req->resp_status = HTTP_STATUS_NOT_FOUND; goto out; } - for (row = 0; row < rows; row++) - { + // Iterate over posts and render them. + for (row = 0; row < rows; row++) { id = kore_pgsql_getvalue(&sql, row, 0); title = kore_pgsql_getvalue(&sql, row, 1); created_at = kore_pgsql_getvalue(&sql, row, 2); @@ -253,8 +243,7 @@ render_posts_query(struct post_request *post_req) // Render MD. err = render_md(body, html_buf); - if (err == KORE_RESULT_ERROR) - { + if (err == KORE_RESULT_ERROR) { kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id); kore_buf_free(html_buf); continue; @@ -272,23 +261,6 @@ render_posts_query(struct post_request *post_req) kore_buf_free(html_buf); } } - else if (post_req->type == JSON) - { - // XXX Always tuples from the above Postgres queries, need to check the length for results. - if (kore_pgsql_getlength(&sql, 0, 0) == 0) - { - post_req->resp_status = HTTP_STATUS_NOT_FOUND; - goto out; - } - - json = kore_pgsql_getvalue(&sql, 0, 0); - kore_buf_append( - post_req->resp_buf, - json, - strlen(json) - ); - } // else { ... - out: ; kore_pgsql_cleanup(&sql); @@ -297,8 +269,7 @@ out: ; } static int -render_md(const char *in, struct kore_buf *out) -{ +render_md(const char *in, struct kore_buf *out) { int err = 0; static unsigned parser_flags = 0; @@ -322,8 +293,7 @@ render_md(const char *in, struct kore_buf *out) } static void -process_md_output(const MD_CHAR *html, MD_SIZE size, void *buf) -{ +process_md_output(const MD_CHAR *html, MD_SIZE size, void *buf) { kore_buf_append((struct kore_buf *) buf, (const void *) html, (size_t) size); } -- cgit v1.2.3