From 9206b6537b93db8b2d8e31f26de4da8a5c8ac633 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Mon, 2 Jan 2023 10:12:01 -0600 Subject: Reduce duplicate code --- src/clog.c | 77 ++++++++++++++++++++------------------------------------------ 1 file changed, 24 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/clog.c b/src/clog.c index 4a2d09a..8dd9b9f 100644 --- a/src/clog.c +++ b/src/clog.c @@ -30,23 +30,25 @@ struct post_request { struct kore_buf *resp_buf; }; -void post_request_init(struct post_request *p); +void post_request_init(struct post_request *post_req); void post_request_cleanup(struct post_request *post_req); -int redirect(struct http_request *); +int redirect(struct http_request *req); -int post(struct http_request *); -int posts(struct http_request *); +int post(struct http_request *req); +int posts(struct http_request *req); -int render_posts_html(struct post_request *); -int render_posts_json(struct post_request *); +int render_posts(struct http_request *req, const char *resource); +int render_posts_html(struct post_request *post_req); +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 *req_err = "There was an error processing the request."; +static const char *req_err_generic = "There was an error processing the request."; +static const char *req_err_no_resource = "There was an error processing the request."; void post_request_init(struct post_request *post_req) { @@ -79,54 +81,22 @@ redirect(struct http_request *req) int post(struct http_request *req) { - int err = 0; - - const char *accept = NULL; - - struct post_request post_req; - - post_request_init(&post_req); - - post_req.req = req; - - post_req.resource = req->path + strlen("/posts/"); - kore_log(LOG_DEBUG, "Resource /posts/%s", post_req.resource); - - err = http_request_header(req, "accept", &accept); - if (err == KORE_RESULT_OK) - { - kore_log(LOG_DEBUG, "Accept: %s", accept); - if (strncmp(accept, accept_json, sizeof(*accept_json)) == 0) - { - post_req.type = 0; - } - } - - if (post_req.type == 0) - { - http_response_header(post_req.req, "content-type", "application/json; charset=utf-8"); - (void) render_posts_json(&post_req); - } - else - { - http_response_header(post_req.req, "content-type", "text/html; charset=utf-8"); - (void) render_posts_html(&post_req); - } + const char *resource = NULL; - http_response( - post_req.req, - post_req.resp_status, - post_req.resp_buf->data, - post_req.resp_buf->offset - ); - - post_request_cleanup(&post_req); + resource = req->path + strlen("/posts/"); + kore_log(LOG_DEBUG, "Resource /posts/%s", resource); - return KORE_RESULT_OK; + return render_posts(req, resource); } int posts(struct http_request *req) +{ + return render_posts(req, NULL); +} + +int +render_posts(struct http_request *req, const char *resource) { int err = 0; @@ -137,6 +107,7 @@ posts(struct http_request *req) post_request_init(&post_req); post_req.req = req; + post_req.resource = resource; err = http_request_header(req, "accept", &accept); if (err == KORE_RESULT_OK) @@ -200,7 +171,7 @@ render_posts_html(struct post_request *post_req) kore_buf_appendf( post_req->resp_buf, (const char *) asset_error_html, - req_err + req_err_generic ); kore_pgsql_logerror(&sql); goto out; @@ -237,7 +208,7 @@ render_posts_html(struct post_request *post_req) kore_buf_appendf( post_req->resp_buf, (const char *) asset_error_html, - req_err + req_err_generic ); kore_pgsql_logerror(&sql); goto out; @@ -308,7 +279,7 @@ render_posts_json(struct post_request *post_req) kore_buf_appendf( post_req->resp_buf, (const char *) asset_error_json, - req_err + req_err_generic ); kore_pgsql_logerror(&sql); goto out; @@ -345,7 +316,7 @@ render_posts_json(struct post_request *post_req) kore_buf_appendf( post_req->resp_buf, (const char *) asset_error_json, - req_err + req_err_generic ); kore_pgsql_logerror(&sql); goto out; -- cgit v1.2.3