diff options
Diffstat (limited to 'src/clog.c')
-rw-r--r-- | src/clog.c | 96 |
1 files changed, 64 insertions, 32 deletions
@@ -26,11 +26,12 @@ struct post_request { // FIXME: use enum unsigned int type; // 0 JSON; 1 HTML - // TODO + int resp_status; struct kore_buf *resp_buf; }; void post_request_init(struct post_request *p); +void post_request_cleanup(struct post_request *post_req); int redirect(struct http_request *); @@ -44,6 +45,8 @@ static void process_md_output(const MD_CHAR *, MD_SIZE size, void *); static int render_md(const char *, struct kore_buf *); static const char *database = "db"; +// FIXME: quotes for JSON error output; extra quotes in HTML +static const char *req_err = "\"There was an error processing the request.\""; static const char *accept_json = "application/json"; void post_request_init(struct post_request *post_req) @@ -51,7 +54,18 @@ void post_request_init(struct post_request *post_req) post_req->req = NULL; post_req->resource = NULL; post_req->type = 1; - post_req->resp_buf = NULL; + + 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) + { + kore_buf_free(post_req->resp_buf); + post_req->resp_buf = NULL; + } } int @@ -76,6 +90,9 @@ post(struct http_request *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) { @@ -86,18 +103,26 @@ post(struct http_request *req) } } - post_req.resource = req->path + strlen("/posts/"); - kore_log(LOG_DEBUG, "Resource /posts/%s", post_req.resource); - 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); } + http_response( + post_req.req, + post_req.resp_status, + post_req.resp_buf->data, + post_req.resp_buf->offset + ); + + post_request_cleanup(&post_req); + return KORE_RESULT_OK; } @@ -126,13 +151,24 @@ posts(struct http_request *req) if (post_req.type == 0) { + http_response_header(post_req.req, "content-type", "application/json; charset=utf-8"); err = render_posts_json(&post_req); } else { + http_response_header(post_req.req, "content-type", "text/html; charset=utf-8"); err = render_posts_html(&post_req); } + http_response( + post_req.req, + post_req.resp_status, + post_req.resp_buf->data, + post_req.resp_buf->offset + ); + + post_request_cleanup(&post_req); + return err; } @@ -148,13 +184,11 @@ render_posts_html(struct post_request *post_req) const char *created_at = NULL; const char *body = NULL; - struct kore_buf *resp_buf = kore_buf_alloc(0); - struct kore_pgsql sql; kore_pgsql_init(&sql); - kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html); + kore_buf_append(post_req->resp_buf, asset_index_begin_html, asset_len_index_begin_html); // Initialize our kore_pgsql data structure with the database name // we want to connect to (note that we registered this earlier with @@ -163,10 +197,11 @@ render_posts_html(struct post_request *post_req) err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC); if (err == KORE_RESULT_ERROR) { + post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR; kore_buf_appendf( - resp_buf, + post_req->resp_buf, (const char *) asset_error_html, - "There was an error processing the request." + req_err ); kore_pgsql_logerror(&sql); goto out; @@ -199,10 +234,11 @@ render_posts_html(struct post_request *post_req) if (err == KORE_RESULT_ERROR) { + post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR; kore_buf_appendf( - resp_buf, + post_req->resp_buf, (const char *) asset_error_html, - "There was an error processing the request." + req_err ); kore_pgsql_logerror(&sql); goto out; @@ -210,6 +246,9 @@ render_posts_html(struct post_request *post_req) // Iterate over posts and render them. rows = kore_pgsql_ntuples(&sql); + + // if (rows == 0) ... + for (row = 0; row < rows; row++) { id = kore_pgsql_getvalue(&sql, row, 0); @@ -231,7 +270,7 @@ render_posts_html(struct post_request *post_req) // Append rendered MD post. kore_buf_appendf( - resp_buf, + post_req->resp_buf, (const char *) asset_post_html, title, created_at, @@ -243,12 +282,9 @@ render_posts_html(struct post_request *post_req) out: ; - kore_buf_append(resp_buf, asset_index_end_html, asset_len_index_end_html); - http_response_header(post_req->req, "content-type", "text/html; charset=utf-8"); - http_response(post_req->req, HTTP_STATUS_OK, resp_buf->data, resp_buf->offset); + kore_buf_append(post_req->resp_buf, asset_index_end_html, asset_len_index_end_html); kore_pgsql_cleanup(&sql); - kore_buf_free(resp_buf); return KORE_RESULT_OK; } @@ -262,8 +298,6 @@ render_posts_json(struct post_request *post_req) const char *json = NULL; - struct kore_buf *resp_buf = kore_buf_alloc(0); - struct kore_pgsql sql; kore_pgsql_init(&sql); @@ -271,10 +305,11 @@ render_posts_json(struct post_request *post_req) err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC); if (err == KORE_RESULT_ERROR) { - kore_buf_appendf( - resp_buf, - (const char *) asset_error_html, - "There was an error processing the request." + post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR; + kore_buf_append( + post_req->resp_buf, + req_err, + strlen(req_err) ); kore_pgsql_logerror(&sql); goto out; @@ -307,10 +342,11 @@ render_posts_json(struct post_request *post_req) } if (err == KORE_RESULT_ERROR) { - kore_buf_appendf( - resp_buf, - (const char *) asset_error_html, - "There was an error processing the request." + post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR; + kore_buf_append( + post_req->resp_buf, + req_err, + strlen(req_err) ); kore_pgsql_logerror(&sql); goto out; @@ -320,7 +356,7 @@ render_posts_json(struct post_request *post_req) if (rows == 1) { json = kore_pgsql_getvalue(&sql, 0, 0); kore_buf_append( - resp_buf, + post_req->resp_buf, json, strlen(json) ); @@ -328,11 +364,7 @@ render_posts_json(struct post_request *post_req) out: ; - http_response_header(post_req->req, "content-type", "application/json; charset=utf-8"); - http_response(post_req->req, HTTP_STATUS_OK, resp_buf->data, resp_buf->offset); - kore_pgsql_cleanup(&sql); - kore_buf_free(resp_buf); return KORE_RESULT_OK; } |