diff options
author | Michael McVady <femtonaut@gmail.com> | 2023-01-02 08:29:23 -0600 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2023-01-02 08:29:23 -0600 |
commit | 5964c509a536a694e0f2b3d5daf6f74abcc291f2 (patch) | |
tree | dbdbb8db19c4f419ff84bb8c7479e89d1e0dad30 /src/clog.c | |
parent | 34ec321f7c3e54a43905003b9dee8a41ac11aa18 (diff) |
Hack in post_rq struct
Diffstat (limited to 'src/clog.c')
-rw-r--r-- | src/clog.c | 75 |
1 files changed, 41 insertions, 34 deletions
@@ -20,22 +20,25 @@ KORE_SECCOMP_FILTER("clog", KORE_SYSCALL_ALLOW(uname) ) -struct post_req { +struct post_request { struct http_request *req; const char *resource; // FIXME: use enum unsigned int type; // 0 JSON; 1 HTML + + // TODO + struct kore_buf *resp_buf; }; -void post_req_init(struct post_req *p); +void post_request_init(struct post_request *p); int redirect(struct http_request *); int post(struct http_request *); int posts(struct http_request *); -int render_posts_html(struct http_request *, const char *); -int render_posts_json(struct http_request *, const char *); +int render_posts_html(struct post_request *); +int render_posts_json(struct post_request *); static void process_md_output(const MD_CHAR *, MD_SIZE size, void *); static int render_md(const char *, struct kore_buf *); @@ -43,11 +46,12 @@ static int render_md(const char *, struct kore_buf *); static const char *database = "db"; static const char *accept_json = "application/json"; -void post_req_init(struct post_req *p) +void post_request_init(struct post_request *post_req) { - p->req = NULL; - p->resource = NULL; - p->type = 1; + post_req->req = NULL; + post_req->resource = NULL; + post_req->type = 1; + post_req->resp_buf = NULL; } int @@ -66,11 +70,11 @@ post(struct http_request *req) const char *accept = NULL; - struct post_req request; + struct post_request post_req; - post_req_init(&request); + post_request_init(&post_req); - request.req = req; + post_req.req = req; err = http_request_header(req, "accept", &accept); if (err == KORE_RESULT_OK) @@ -78,22 +82,23 @@ post(struct http_request *req) kore_log(LOG_DEBUG, "Accept: %s", accept); if (strncmp(accept, accept_json, sizeof(*accept_json)) == 0) { - request.type = 0; + post_req.type = 0; } } - request.resource = req->path + strlen("/posts/"); - kore_log(LOG_DEBUG, "Resource /posts/%s", request.resource); + post_req.resource = req->path + strlen("/posts/"); + kore_log(LOG_DEBUG, "Resource /posts/%s", post_req.resource); - if (request.type == 0) + if (post_req.type == 0) { - return render_posts_json(request.req, request.resource); + (void) render_posts_json(&post_req); } else { - return render_posts_html(request.req, request.resource); + (void) render_posts_html(&post_req); } + return KORE_RESULT_OK; } int @@ -103,11 +108,11 @@ posts(struct http_request *req) const char *accept = NULL; - struct post_req request; + struct post_request post_req; - post_req_init(&request); + post_request_init(&post_req); - request.req = req; + post_req.req = req; err = http_request_header(req, "accept", &accept); if (err == KORE_RESULT_OK) @@ -115,22 +120,24 @@ posts(struct http_request *req) kore_log(LOG_DEBUG, "Accept: %s", accept); if (strncmp(accept, accept_json, sizeof(*accept_json)) == 0) { - request.type = 0; + post_req.type = 0; } } - if (request.type == 0) + if (post_req.type == 0) { - return render_posts_json(request.req, request.resource); + err = render_posts_json(&post_req); } else { - return render_posts_html(request.req, request.resource); + err = render_posts_html(&post_req); } + + return err; } int -render_posts_html(struct http_request *req, const char *resource) +render_posts_html(struct post_request *post_req) { int err = 0; @@ -166,7 +173,7 @@ render_posts_html(struct http_request *req, const char *resource) } // Query for posts, check for error. - if (resource) + if (post_req->resource) { // Query a post. err = kore_pgsql_query_params( @@ -176,7 +183,7 @@ render_posts_html(struct http_request *req, const char *resource) "WHERE id = $1;", 0, // return string data 1, // param count - KORE_PGSQL_PARAM_TEXT(resource) + KORE_PGSQL_PARAM_TEXT(post_req->resource) ); } else @@ -237,8 +244,8 @@ render_posts_html(struct http_request *req, const char *resource) out: ; kore_buf_append(resp_buf, asset_index_end_html, asset_len_index_end_html); - http_response_header(req, "content-type", "text/html; charset=utf-8"); - http_response(req, HTTP_STATUS_OK, resp_buf->data, resp_buf->offset); + 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_pgsql_cleanup(&sql); kore_buf_free(resp_buf); @@ -247,7 +254,7 @@ out: ; } int -render_posts_json(struct http_request *req, const char *resource) +render_posts_json(struct post_request *post_req) { int err = 0; @@ -273,7 +280,7 @@ render_posts_json(struct http_request *req, const char *resource) goto out; } - if (resource) + if (post_req->resource) { err = kore_pgsql_query_params( &sql, @@ -284,7 +291,7 @@ render_posts_json(struct http_request *req, const char *resource) ") row;", 0, // string data 1, // param count - KORE_PGSQL_PARAM_TEXT(resource) + KORE_PGSQL_PARAM_TEXT(post_req->resource) ); } else @@ -321,8 +328,8 @@ render_posts_json(struct http_request *req, const char *resource) out: ; - http_response_header(req, "content-type", "application/json; charset=utf-8"); - http_response(req, HTTP_STATUS_OK, resp_buf->data, resp_buf->offset); + 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); |