aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-08 15:18:45 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-08 15:18:45 -0600
commit3ff9bdbcb2cae776579dcfe781276172f05f000c (patch)
tree52f7210fb43b645f053403027127bed271435d54 /src/clog.c
parent176668c4a1e7c949cbdb4ba387b7f187701e8db5 (diff)
Clean up
Diffstat (limited to 'src/clog.c')
-rw-r--r--src/clog.c44
1 files changed, 25 insertions, 19 deletions
diff --git a/src/clog.c b/src/clog.c
index 077fba4..bdaac38 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -29,7 +29,7 @@ enum content_type { JSON, HTML };
enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND };
struct post_query {
- const char *id;
+ char *id;
enum content_type type;
int status;
@@ -45,7 +45,7 @@ static const char * const error_msg[] = {
[HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.",
};
-void post_query_init(struct post_query *pq);
+void post_query_init(struct post_query *pq, enum content_type type, const char *id);
void post_query_cleanup(struct post_query *pq);
int validate_uuid(const char *uuid);
@@ -83,9 +83,13 @@ static void process_md_output(const MD_CHAR *, MD_SIZE size, void *);
static int render_md(const char *, struct kore_buf *);
void
-post_query_init(struct post_query *pq) {
- pq->id = NULL;
- pq->type = JSON;
+post_query_init(struct post_query *pq, enum content_type type, const char *id) {
+ if (id != NULL)
+ pq->id = kore_strdup(id);
+ else
+ pq->id = NULL;
+
+ pq->type = type;
pq->status = QUERY_STATUS_OK;
pq->result = kore_buf_alloc(0);
@@ -93,6 +97,10 @@ post_query_init(struct post_query *pq) {
void
post_query_cleanup(struct post_query *pq) {
+ if (pq->id != NULL)
+ kore_free((void *) pq->id);
+ pq->id = NULL;
+
if (pq->result != NULL)
kore_buf_free(pq->result);
pq->result = NULL;
@@ -228,8 +236,7 @@ int
get_posts(struct http_request *req) {
struct post_query pq;
- post_query_init(&pq);
- pq.type = get_content_type(req);
+ post_query_init(&pq, get_content_type(req), NULL);
(void) sql_select_posts(&pq);
if (pq.status != QUERY_STATUS_OK)
@@ -248,9 +255,7 @@ get_posts_resource(struct http_request *req) {
struct post_query pq;
- post_query_init(&pq);
- pq.id = req->path + strlen("/posts/");
- pq.type = get_content_type(req);
+ post_query_init(&pq, get_content_type(req), req->path + strlen("/posts/"));
// Check for valid resource UUID
kore_log(LOG_DEBUG, "Resource id /posts/%s.", pq.id);
@@ -292,6 +297,7 @@ post_posts(struct http_request *req) {
struct kore_json_item *item = NULL;
struct kore_json json;
+
kore_json_init(&json, req->http_body->data, req->http_body->length);
if (!kore_json_parse(&json)) {
@@ -347,11 +353,10 @@ post_posts(struct http_request *req) {
out: ;
+ // XXX Not sure about this hack to return the created resource.
if (status == HTTP_STATUS_CREATED) {
- post_query_init(&pq);
- pq.id = id;
- pq.type = type;
+ post_query_init(&pq, type, id);
(void) sql_select_posts(&pq);
if (pq.status == QUERY_STATUS_NOT_FOUND)
@@ -371,10 +376,9 @@ out: ;
return KORE_RESULT_OK;
}
-
int
sql_select_posts(struct post_query *pq) {
- int err = 0;
+ int err = KORE_RESULT_OK;
struct kore_pgsql sql;
@@ -432,11 +436,11 @@ out: ;
kore_pgsql_cleanup(&sql);
- return KORE_RESULT_OK;
+ return err;
}
int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) {
- int err = 0;
+ int err = KORE_RESULT_OK;
int row = 0, rows = 0;
@@ -456,10 +460,11 @@ int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) {
return KORE_RESULT_OK;
}
+ // pg->type == HTML
+
// Allocate a buffer to render the markdown as HTML into.
html_buf = kore_buf_alloc(0);
- // pg->type == HTML
// Iterate over posts and render them.
rows = kore_pgsql_ntuples(sql);
for (row = 0; row < rows; row++) {
@@ -476,7 +481,7 @@ int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) {
// Render MD.
err = render_md(body, html_buf);
if (err == KORE_RESULT_ERROR) {
- kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id);
+ kore_log(LOG_ERR, "Error rendering markdown for post %s.", id);
continue;
}
@@ -537,6 +542,7 @@ out: ;
static int
render_md(const char *in, struct kore_buf *out) {
+ // Not a kore err.
int err = 0;
static unsigned parser_flags = 0;