aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-02 10:12:01 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-02 10:12:01 -0600
commit9206b6537b93db8b2d8e31f26de4da8a5c8ac633 (patch)
treeba4aa97a27dd466aef6c5d2b1967b5e06af3a8c6 /src/clog.c
parent0050e1d3960d4ee6cdeca4e20bea5ecb05c35af2 (diff)
Reduce duplicate code
Diffstat (limited to 'src/clog.c')
-rw-r--r--src/clog.c77
1 files changed, 24 insertions, 53 deletions
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,55 +81,23 @@ 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;
const char *accept = NULL;
@@ -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;