From e24dfc3412648c8b07a32a59774ed21f5058f7e0 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Sun, 9 Jul 2023 11:23:15 -0500 Subject: Deconflate `accept_type` & `content_type` --- src/clog.c | 74 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/clog.c b/src/clog.c index 71c3c3d..5be4a51 100644 --- a/src/clog.c +++ b/src/clog.c @@ -23,15 +23,15 @@ KORE_SECCOMP_FILTER("clog", KORE_SYSCALL_ALLOW(uname) ) -enum accept_type { JSON, HTML }; +enum accept_type { ACCEPT_JSON, ACCEPT_HTML }; -// enum content_type { JSON, X_WWW_FORM_URLENCODED }; +enum content_type { CONTENT_JSON, CONTENT_X_WWW_FORM_URLENCODED }; enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND }; struct post_query { char *id; - enum accept_type type; + enum accept_type accept_type; int status; struct kore_buf *result; @@ -48,7 +48,7 @@ static const char * const error_msg[] = { [HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.", // 500 }; -void post_query_init(struct post_query *pq, enum accept_type type, const char *id); +void post_query_init(struct post_query *pq, enum accept_type accept_type, const char *id); void post_query_cleanup(struct post_query *pq); int validate_uuid(const char *uuid); @@ -57,14 +57,14 @@ enum accept_type get_accept_type(struct http_request *req); int http_ok_resp( struct http_request *req, - enum accept_type type, + enum accept_type accept_type, enum http_status_code status, struct kore_buf *result ); int http_err_resp( struct http_request *req, - enum accept_type type, + enum accept_type accept_type, enum http_status_code status ); @@ -90,13 +90,13 @@ int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq); 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, enum accept_type type, const char *id) { +void post_query_init(struct post_query *pq, enum accept_type accept_type, const char *id) { if (id != NULL) pq->id = kore_strdup(id); else pq->id = NULL; - pq->type = type; + pq->accept_type = accept_type; pq->status = QUERY_STATUS_OK; pq->result = kore_buf_alloc(0); } @@ -144,22 +144,22 @@ enum accept_type get_accept_type(struct http_request *req) { if (err == KORE_RESULT_OK) { kore_log(LOG_DEBUG, "Accept: %s", accept); if (strncmp(accept, accept_json, sizeof(*accept_json)) == 0) - return JSON; + return ACCEPT_JSON; } - return HTML; + return ACCEPT_HTML; } int http_ok_resp( struct http_request *req, - enum accept_type type, + enum accept_type accept_type, enum http_status_code status, struct kore_buf *result ) { struct kore_buf *resp_buf = kore_buf_alloc(0); const char *body = kore_buf_stringify(result, NULL); - if (type == JSON) { + if (accept_type == ACCEPT_JSON) { http_response_header(req, "content-type", "application/json; charset=utf-8"); kore_buf_append( resp_buf, @@ -191,12 +191,12 @@ int http_ok_resp( int http_err_resp( struct http_request *req, - enum accept_type type, + enum accept_type accept_type, enum http_status_code status ) { struct kore_buf *resp_buf = kore_buf_alloc(0); - if (type == JSON) { + if (accept_type == ACCEPT_JSON) { http_response_header(req, "content-type", "application/json; charset=utf-8"); kore_buf_appendf( resp_buf, @@ -246,7 +246,7 @@ int get_form(struct http_request *req) { asset_len_post_edit_html ); - http_ok_resp(req, HTML, HTTP_STATUS_OK, resp_buf); + http_ok_resp(req, ACCEPT_HTML, HTTP_STATUS_OK, resp_buf); kore_buf_free(resp_buf); @@ -294,7 +294,7 @@ int post_form(struct http_request *req) { kore_buf_stringify(html_buf, NULL) ); - http_ok_resp(req, HTML, HTTP_STATUS_OK, resp_buf); + http_ok_resp(req, ACCEPT_HTML, HTTP_STATUS_OK, resp_buf); kore_buf_free(html_buf); kore_buf_free(resp_buf); @@ -310,9 +310,9 @@ int get_posts(struct http_request *req) { (void) sql_select_posts(&pq); if (pq.status != QUERY_STATUS_OK) - http_err_resp(req, pq.type, HTTP_STATUS_INTERNAL_ERROR); + http_err_resp(req, pq.accept_type, HTTP_STATUS_INTERNAL_ERROR); else - http_ok_resp(req, pq.type, HTTP_STATUS_OK, pq.result); + http_ok_resp(req, pq.accept_type, HTTP_STATUS_OK, pq.result); post_query_cleanup(&pq); @@ -331,17 +331,17 @@ int get_posts_resource(struct http_request *req) { err = validate_uuid(pq.id); if (err == KORE_RESULT_ERROR) { kore_log(LOG_ERR, "Invalid post id %s.", pq.id); - http_err_resp(req, pq.type, HTTP_STATUS_NOT_FOUND); + http_err_resp(req, pq.accept_type, HTTP_STATUS_NOT_FOUND); goto out; } (void) sql_select_posts(&pq); if (pq.status == QUERY_STATUS_NOT_FOUND) - http_err_resp(req, pq.type, HTTP_STATUS_NOT_FOUND); + http_err_resp(req, pq.accept_type, HTTP_STATUS_NOT_FOUND); else if (pq.status == QUERY_STATUS_ERROR) - http_err_resp(req, pq.type, HTTP_STATUS_INTERNAL_ERROR); + http_err_resp(req, pq.accept_type, HTTP_STATUS_INTERNAL_ERROR); else - http_ok_resp(req, pq.type, HTTP_STATUS_OK, pq.result); + http_ok_resp(req, pq.accept_type, HTTP_STATUS_OK, pq.result); out: ; @@ -355,7 +355,7 @@ int post_posts(struct http_request *req) { int status = HTTP_STATUS_CREATED; - enum accept_type type = get_accept_type(req); + enum accept_type accept_type = get_accept_type(req); const char *id = NULL; const char *title = NULL; @@ -414,7 +414,7 @@ int post_posts(struct http_request *req) { out: ; - http_err_resp(req, type, status); + http_err_resp(req, accept_type, status); kore_json_cleanup(&json); @@ -426,7 +426,7 @@ int put_posts(struct http_request *req) { int status = HTTP_STATUS_OK; - enum accept_type type = get_accept_type(req); + enum accept_type accept_type = get_accept_type(req); const char *id = NULL; const char *title = NULL; @@ -444,7 +444,7 @@ int put_posts(struct http_request *req) { err = validate_uuid(id); if (err == KORE_RESULT_ERROR) { kore_log(LOG_ERR, "Invalid post id %s.", id); - http_err_resp(req, type, HTTP_STATUS_NOT_FOUND); + http_err_resp(req, accept_type, HTTP_STATUS_NOT_FOUND); goto out; } @@ -483,7 +483,7 @@ int put_posts(struct http_request *req) { out: ; - http_err_resp(req, type, status); + http_err_resp(req, accept_type, status); kore_json_cleanup(&json); @@ -512,7 +512,7 @@ int sql_select_posts(struct post_query *pq) { // Query a post. err = kore_pgsql_query_params( &sql, - pq->type == HTML ? q_select_html_post : q_select_json_post, + pq->accept_type == ACCEPT_HTML ? q_select_html_post : q_select_json_post, 0, // return string data 1, // param count KORE_PGSQL_PARAM_TEXT(pq->id) @@ -521,7 +521,7 @@ int sql_select_posts(struct post_query *pq) { // Query all posts. err = kore_pgsql_query( &sql, - pq->type == HTML ? q_select_html_posts : q_select_json_posts + pq->accept_type == ACCEPT_HTML ? q_select_html_posts : q_select_json_posts ); } @@ -532,7 +532,7 @@ int sql_select_posts(struct post_query *pq) { } // XXX Always tuples from the above Postgres queries, need to check the length for results. - if (pq->type == JSON && kore_pgsql_getlength(&sql, 0, 0) == 0) { + if (pq->accept_type == ACCEPT_JSON && kore_pgsql_getlength(&sql, 0, 0) == 0) { pq->status = QUERY_STATUS_NOT_FOUND; goto out; } @@ -555,7 +555,7 @@ out: ; int delete_posts(struct http_request *req) { int err = 0; - enum accept_type type = get_accept_type(req); + enum accept_type accept_type = get_accept_type(req); const char *id = req->path + strlen("/posts/"); // Check for valid resource UUID @@ -563,19 +563,19 @@ int delete_posts(struct http_request *req) { err = validate_uuid(id); if (err == KORE_RESULT_ERROR) { kore_log(LOG_ERR, "Invalid post id %s.", id); - http_err_resp(req, type, HTTP_STATUS_NOT_FOUND); + http_err_resp(req, accept_type, HTTP_STATUS_NOT_FOUND); goto out; } err = sql_delete_posts(id); // FIXME: should 404 if id doesn't exist. // if (pq.status == QUERY_STATUS_NOT_FOUND) - // http_err_resp(req, pq.type, HTTP_STATUS_NOT_FOUND); + // http_err_resp(req, pq.accept_type, HTTP_STATUS_NOT_FOUND); if (err == KORE_RESULT_ERROR) - http_err_resp(req, type, HTTP_STATUS_INTERNAL_ERROR); + http_err_resp(req, accept_type, HTTP_STATUS_INTERNAL_ERROR); else // TODO: test this, it should explode. - http_err_resp(req, type, HTTP_STATUS_OK); + http_err_resp(req, accept_type, HTTP_STATUS_OK); out: ; @@ -594,7 +594,7 @@ int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) { const char *created_at = NULL; const char *body = NULL; - if (pq->type == JSON) { + if (pq->accept_type == ACCEPT_JSON) { kore_buf_append( pq->result, kore_pgsql_getvalue(sql, 0, 0), @@ -603,7 +603,7 @@ int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) { return KORE_RESULT_OK; } - // pg->type == HTML + // pg->accept_type == ACCEPT_HTML // Allocate a buffer to render the markdown as HTML into. html_buf = kore_buf_alloc(0); -- cgit v1.2.3