From 72960ddc9cc7844f1dc3419ead0842df61213ba8 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Sat, 30 Dec 2023 21:09:09 -0600 Subject: Just keep hacking --- README.md | 2 +- assets/entry.html | 13 +------- assets/form_begin.html | 11 ------- assets/form_end.html | 9 ------ assets/index_begin.html | 14 ++------- assets/index_end.html | 10 +------ src/clog.c | 80 ++++++++++++++++++++++++++++++++++++++++++------- src/queries.h | 5 ++++ 8 files changed, 80 insertions(+), 64 deletions(-) delete mode 100644 assets/form_begin.html delete mode 100644 assets/form_end.html diff --git a/README.md b/README.md index d1a07a6..1471b5b 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Use `psql` and load `db/db.sql` for models, 10. Get rid of `requests` dependency in `tests.py` 11. Fix `PATCH` redirects? -## But using C in 2023 is dangerous? +## But using C in 2024 is dangerous? * https://chromium.googlesource.com/chromium/src/+/master/docs/security/rule-of-2.md * https://verdagon.dev/blog/when-to-use-memory-safe-part-1 diff --git a/assets/entry.html b/assets/entry.html index 445d2ce..d25750d 100644 --- a/assets/entry.html +++ b/assets/entry.html @@ -1,12 +1 @@ - -
-
-
-

%s

-%s -
-
-%s -
-
-
+%s \ No newline at end of file diff --git a/assets/form_begin.html b/assets/form_begin.html deleted file mode 100644 index 4da1ce9..0000000 --- a/assets/form_begin.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - -bunkergate.org - - - - diff --git a/assets/form_end.html b/assets/form_end.html deleted file mode 100644 index 6131a40..0000000 --- a/assets/form_end.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/assets/index_begin.html b/assets/index_begin.html index bf13b45..0ff3660 100644 --- a/assets/index_begin.html +++ b/assets/index_begin.html @@ -3,20 +3,10 @@ - + bunkergate.org - + - - - - -
diff --git a/assets/index_end.html b/assets/index_end.html index ba83da5..6131a40 100644 --- a/assets/index_end.html +++ b/assets/index_end.html @@ -1,11 +1,3 @@ -
- - -HTML5 | -CSS3 - - -
- + \ No newline at end of file diff --git a/src/clog.c b/src/clog.c index 80906d3..4eebe6f 100644 --- a/src/clog.c +++ b/src/clog.c @@ -23,8 +23,6 @@ KORE_SECCOMP_FILTER("clog", KORE_SYSCALL_ALLOW(uname) ) -enum content_type { CONTENT_JSON, CONTENT_X_WWW_FORM_URLENCODED }; - enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND }; struct entry_query { @@ -77,10 +75,12 @@ int sql_select_entries(struct entry_query *eq); int sql_update_entry(const char *id, const char *title, const char *body); int sql_delete_entry(const char *id); int sql_insert_entry(const char *id, const char *title, const char *body); + int sql_render_entry(struct kore_pgsql *sql, struct entry_query *eq); +int sql_render_index(struct kore_pgsql *sql, struct entry_query *eq); -static void process_md_output(const MD_CHAR *, MD_SIZE size, void *); -static int render_md(const char *, struct kore_buf *); +static void process_md_output(const MD_CHAR *html, MD_SIZE size, void *buf); +static int render_md(const char *in, struct kore_buf *out); void entry_query_init(struct entry_query *eq, const char *id) { if (id != NULL) @@ -242,7 +242,7 @@ int post_entry_form(struct http_request *req) { http_response_header(req, "content-type", "text/html; charset=utf-8"); - kore_buf_append(resp_buf, asset_form_begin_html, asset_len_form_begin_html); + kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html); // Append rendered MD entry. size_t len = 0; @@ -254,7 +254,7 @@ int post_entry_form(struct http_request *req) { len ); - kore_buf_append(resp_buf, asset_form_end_html, asset_len_form_end_html); + kore_buf_append(resp_buf, asset_index_end_html, asset_len_index_end_html); http_response( req, @@ -482,10 +482,10 @@ int sql_select_entries(struct entry_query *eq) { KORE_PGSQL_PARAM_TEXT(eq->id) ); } else { - // Query all entries. + // Query for index. err = kore_pgsql_query( &sql, - q_select_entries + q_select_index ); } @@ -501,7 +501,11 @@ int sql_select_entries(struct entry_query *eq) { goto out; } - (void) sql_render_entry(&sql, eq); + if (eq->id) { + (void) sql_render_entry(&sql, eq); + } else { + (void) sql_render_index(&sql, eq); + } out: ; @@ -578,14 +582,70 @@ int sql_render_entry(struct kore_pgsql *sql, struct entry_query *eq) { kore_buf_appendf( eq->result, (const char *) asset_entry_html, + kore_buf_stringify(html_buf, NULL) + ); + + } + + kore_buf_free(html_buf); + + return KORE_RESULT_OK; +} + + +int sql_render_index(struct kore_pgsql *sql, struct entry_query *eq) { + + int row = 0, rows = 0; + + struct kore_buf *html_buf = NULL; + + const char *id = NULL; + const char *title = NULL; + const char *created_at = NULL; + const char *updated_at = NULL; + + // Allocate a buffer to render the markdown as HTML into. + html_buf = kore_buf_alloc(0); + + // Write table header. + kore_buf_append( + eq->result, + (const char *) "

clog.bunkergate.org

\r\n\r\n", + 94 + ); + + // Iterate over entries and render them. + rows = kore_pgsql_ntuples(sql); + for (row = 0; row < rows; row++) { + // Reset buffer. + kore_buf_cleanup(html_buf); + + // Fetch data. + id = kore_pgsql_getvalue(sql, row, 0); + title = kore_pgsql_getvalue(sql, row, 1); + created_at = kore_pgsql_getvalue(sql, row, 2); + updated_at = kore_pgsql_getvalue(sql, row, 3); + kore_log(LOG_DEBUG, "id: '%s'; title '%s'", id, title); + + // Append rendered MD entry. + kore_buf_appendf( + eq->result, + (const char *) "\r\n", id, title, created_at, - kore_buf_stringify(html_buf, NULL) + updated_at ); } + // Write table footer. + kore_buf_append( + eq->result, + (const char *) "
TitleCreatedUpdated
%s%s%s
\r\n", + 10 + ); + kore_buf_free(html_buf); return KORE_RESULT_OK; diff --git a/src/queries.h b/src/queries.h index 986f180..c0bd1dc 100644 --- a/src/queries.h +++ b/src/queries.h @@ -8,6 +8,11 @@ const char *q_select_entries = "FROM entries " "ORDER BY updated_at DESC;"; +const char *q_select_index = +"SELECT id, title, created_at::DATE, updated_at::DATE " +"FROM entries " +"ORDER BY updated_at DESC;"; + const char *q_insert_entry = "INSERT INTO entries " "(title, body) " -- cgit v1.2.3