1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
#include <ctype.h>
#include <kore/kore.h>
#include <kore/http.h>
#include <kore/pgsql.h>
#include <kore/seccomp.h>
#include "../lib/md4c/src/entity.h"
#include "../lib/md4c/src/entity.c"
#include "../lib/md4c/src/md4c.h"
#include "../lib/md4c/src/md4c.c"
#include "../lib/md4c/src/md4c-html.h"
#include "../lib/md4c/src/md4c-html.c"
#include "assets.h"
#include "queries.h" // FIXME: Compilation fail if this is a .c file.
KORE_SECCOMP_FILTER("clog",
KORE_SYSCALL_ALLOW(bind),
KORE_SYSCALL_ALLOW(getdents64),
KORE_SYSCALL_ALLOW(newfstatat),
KORE_SYSCALL_ALLOW(sendmmsg),
KORE_SYSCALL_ALLOW(uname)
)
enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND };
struct entry {
char *id;
char *created_at;
char *updated_at;
char *title;
char *body;
};
struct entry_query {
char *id;
struct entry **entries;
int num_entries;
int status;
};
static const char *database = "db";
static const char * const error_msg[] = {
[HTTP_STATUS_OK] = "OK", // 200
[HTTP_STATUS_CREATED] = "Resource created successfully.", // 201
[HTTP_STATUS_BAD_REQUEST] = "There was an error processing the request data.", // 400
[HTTP_STATUS_NOT_FOUND] = "Resource not found.", // 404
[HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.", // 500
};
void entry_query_init(struct entry_query *eq, const char *id);
void entry_query_cleanup(struct entry_query *eq);
int validate_uuid(const char *uuid);
int validate_text(struct http_request *req, char *data);
int http_ok_resp(struct http_request *req, enum http_status_code status, struct kore_buf *content);
int http_err_resp(struct http_request *req, enum http_status_code status);
int redirect(struct http_request *req);
int get_index(struct http_request *req);
int get_entry(struct http_request *req);
int edit_entry(struct http_request *req);
int update_entry(struct http_request *req);
int sql_select(struct entry_query *eq);
int sql_update(const char *id, const char *title, const char *body);
// int sql_delete(const char *id);
// int sql_insert(const char *id, const char *title, const char *body);
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)
eq->id = kore_strdup(id);
else
eq->id = NULL;
eq->entries = kore_malloc(sizeof(struct entry *) * 100);
eq->num_entries = 0;
eq->status = QUERY_STATUS_OK;
}
void entry_query_cleanup(struct entry_query *eq) {
if (eq->id != NULL)
kore_free((void *) eq->id);
eq->id = NULL;
for (int i = 0; i < eq->num_entries; i++) {
if (eq->entries[i]->id != NULL)
kore_free((void *) eq->entries[i]->id);
if (eq->entries[i]->title != NULL)
kore_free((void *) eq->entries[i]->title);
if (eq->entries[i]->created_at != NULL)
kore_free((void *) eq->entries[i]->created_at);
if (eq->entries[i]->updated_at != NULL)
kore_free((void *) eq->entries[i]->updated_at);
if (eq->entries[i]->body != NULL)
kore_free((void *) eq->entries[i]->body);
kore_free((void *) eq->entries[i]);
}
kore_free((void *) eq->entries);
}
int validate_uuid(const char *uuid) {
int i = 0;
if (strlen(uuid) != 36)
return KORE_RESULT_ERROR;
for (i = 0; i <= 36; i++) {
if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
if (uuid[i] != '-')
return KORE_RESULT_ERROR;
continue;
}
if (i == 36) {
if (uuid[i] != '\0')
return KORE_RESULT_ERROR;
continue;
}
if (!isxdigit(uuid[i]))
return KORE_RESULT_ERROR;
}
return KORE_RESULT_OK;
}
int validate_text(struct http_request *req, char *data) {
kore_log(LOG_NOTICE, "v_example_func called %s", data);
return KORE_RESULT_OK;
}
int http_ok_resp(
struct http_request *req,
enum http_status_code status,
struct kore_buf *content
) {
struct kore_buf *resp_buf = kore_buf_alloc(0);
const char *body = kore_buf_stringify(content, NULL);
http_response_header(req, "content-type", "text/html; charset=utf-8");
kore_buf_append(resp_buf, asset_header_html, asset_len_header_html);
kore_buf_append(resp_buf, body, strlen(body));
kore_buf_append(resp_buf, asset_footer_html, asset_len_footer_html);
http_response(req, status, resp_buf->data, resp_buf->offset);
kore_buf_free(resp_buf);
return KORE_RESULT_OK;
}
int http_err_resp(
struct http_request *req,
enum http_status_code status
) {
struct kore_buf *resp_buf = kore_buf_alloc(0);
http_response_header(req, "content-type", "text/html; charset=utf-8");
kore_buf_append(resp_buf, asset_header_html, asset_len_header_html);
kore_buf_appendf(resp_buf, (const char *) asset_error_html, error_msg[status]);
kore_buf_append(resp_buf, asset_footer_html, asset_len_footer_html);
http_response(req, status, resp_buf->data, resp_buf->offset);
kore_buf_free(resp_buf);
return KORE_RESULT_OK;
}
int redirect(struct http_request *req) {
http_response_header(req, "Location", "/");
http_response(req, HTTP_STATUS_TEMPORARY_REDIRECT, NULL, 0);
return KORE_RESULT_OK;
}
int get_index(struct http_request *req) {
struct entry_query eq;
int row = 0;
struct kore_buf *content = NULL;
content = kore_buf_alloc(0);
entry_query_init(&eq, NULL);
(void) sql_select(&eq);
if (eq.status != QUERY_STATUS_OK) {
http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR);
goto out;
}
// Write table header.
kore_buf_append(content, asset_index_header_html, asset_len_index_header_html);
// Iterate over entries and render them.
for (row = 0; row < eq.num_entries; row++) {
// Append rendered MD entry.
kore_buf_appendf(
content,
(const char *) asset_index_row_html,
eq.entries[row]->id, eq.entries[row]->title, eq.entries[row]->created_at, eq.entries[row]->updated_at
);
}
// Write table footer.
kore_buf_append(content, asset_index_footer_html, asset_len_index_footer_html);
http_ok_resp(req, HTTP_STATUS_OK, content);
out: ;
kore_free(content);
entry_query_cleanup(&eq);
return KORE_RESULT_OK;
}
int get_entry(struct http_request *req) {
int err = 0;
struct entry_query eq;
struct kore_buf *content = NULL;
struct kore_buf *rendered_body = NULL;
content = kore_buf_alloc(0);
rendered_body = kore_buf_alloc(0);
entry_query_init(&eq, req->path + strlen("/entries/"));
// Check for valid resource UUID
kore_log(LOG_DEBUG, "Resource id /entries/%s.", eq.id);
err = validate_uuid(eq.id);
if (err == KORE_RESULT_ERROR) {
kore_log(LOG_ERR, "Invalid entry id %s.", eq.id);
http_err_resp(req, HTTP_STATUS_NOT_FOUND);
goto out;
}
(void) sql_select(&eq);
if (eq.status == QUERY_STATUS_NOT_FOUND) {
http_err_resp(req, HTTP_STATUS_NOT_FOUND);
goto out;
}
else if (eq.status == QUERY_STATUS_ERROR) {
http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR);
goto out;
}
// Render MD.
err = render_md(eq.entries[0]->body, rendered_body);
if (err == KORE_RESULT_ERROR) {
kore_log(LOG_ERR, "Error rendering markdown for entry %s.", eq.id);
goto out;
}
// Append rendered MD entry.
kore_buf_appendf(
content,
(const char *) asset_entry_html,
kore_buf_stringify(rendered_body, NULL)
);
http_ok_resp(req, HTTP_STATUS_OK, content);
out: ;
entry_query_cleanup(&eq);
kore_free(rendered_body);
kore_free(content);
return KORE_RESULT_OK;
}
int edit_entry(struct http_request *req) {
int err = 0;
struct entry_query eq;
struct kore_buf *content = NULL;
struct kore_buf *rendered_body = NULL;
char *id = NULL;
content = kore_buf_alloc(0);
rendered_body = kore_buf_alloc(0);
id = kore_strdup(req->path + strlen("/entries/"));
id[strlen(id) - strlen("/edit")] = '\0';
entry_query_init(&eq, (const char*) id);
// Check for valid resource UUID
kore_log(LOG_DEBUG, "Resource id /entries/%s.", eq.id);
err = validate_uuid(eq.id);
if (err == KORE_RESULT_ERROR) {
kore_log(LOG_ERR, "Invalid entry id %s XXX.", eq.id);
http_err_resp(req, HTTP_STATUS_NOT_FOUND);
goto out;
}
(void) sql_select(&eq);
if (eq.status == QUERY_STATUS_NOT_FOUND) {
http_err_resp(req, HTTP_STATUS_NOT_FOUND);
goto out;
}
else if (eq.status == QUERY_STATUS_ERROR) {
http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR);
goto out;
}
kore_buf_appendf(
content, (const char *) asset_entry_edit_html,
eq.entries[0]->id, eq.entries[0]->id, eq.entries[0]->title, eq.entries[0]->body
);
// Render MD.
err = render_md(eq.entries[0]->body, rendered_body);
if (err == KORE_RESULT_ERROR) {
kore_log(LOG_ERR, "Error rendering markdown for entry %s.", eq.id);
goto out;
}
// Append rendered MD entry.
kore_buf_appendf(
content,
(const char *) asset_entry_html,
kore_buf_stringify(rendered_body, NULL)
);
http_ok_resp(req, HTTP_STATUS_OK, content);
out: ;
entry_query_cleanup(&eq);
kore_free(id);
kore_free(rendered_body);
kore_free(content);
return KORE_RESULT_OK;
}
int update_entry(struct http_request *req) {
int err = 0;
char *id = NULL;
char *title = NULL;
char *body = NULL;
http_populate_post(req);
if (http_argument_get_string(req, "title", &title)) {
kore_log(LOG_INFO, "form title %s.", title);
}
else {
kore_log(LOG_ERR, "Error no title");
http_err_resp(req, HTTP_STATUS_BAD_REQUEST);
goto out;
}
if (http_argument_get_string(req, "body", &body)) {
kore_log(LOG_INFO, "form body %s.", body);
}
else {
kore_log(LOG_ERR, "Error no body");
http_err_resp(req, HTTP_STATUS_BAD_REQUEST);
goto out;
}
id = kore_strdup(req->path + strlen("/entries/"));
kore_log(LOG_DEBUG, "updating entry %s.", id);
err = validate_uuid(id);
if (err == KORE_RESULT_ERROR) {
kore_log(LOG_ERR, "Invalid entry id %s.", id);
http_err_resp(req, HTTP_STATUS_NOT_FOUND);
goto out;
}
err = sql_update(id, title, body);
if (err == KORE_RESULT_ERROR) {
kore_log(LOG_ERR, "Error updating entry id %s.", id);
http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR);
goto out;
}
http_err_resp(req, HTTP_STATUS_CREATED);
out: ;
if (id != NULL) {
kore_free(id);
}
return KORE_RESULT_OK;
}
int sql_select(struct entry_query *eq) {
int err = KORE_RESULT_OK;
int row = 0;
int rows = 0;
struct entry *entry = NULL;
struct kore_pgsql sql;
kore_pgsql_init(&sql);
eq->status = QUERY_STATUS_OK;
// Set up synchronous database handle.
err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
if (err == KORE_RESULT_ERROR) {
eq->status = QUERY_STATUS_ERROR;
kore_pgsql_logerror(&sql);
goto out;
}
// Query for entries, check for error.
if (eq->id != NULL) {
// Query an entry.
err = kore_pgsql_query_params(&sql, q_select_entry,
0, // return string data
1, // param count
KORE_PGSQL_PARAM_TEXT(eq->id)
);
} else {
// Query for index.
err = kore_pgsql_query(&sql, q_select_entries);
}
if (err == KORE_RESULT_ERROR) {
eq->status = QUERY_STATUS_ERROR;
kore_pgsql_logerror(&sql);
goto out;
}
// TODO: Add test for this; When database is empty, don't return 404 for base request.
if (eq->id != NULL && kore_pgsql_ntuples(&sql) == 0) {
eq->status = QUERY_STATUS_NOT_FOUND;
goto out;
}
// Iterate over entries and render them.
rows = kore_pgsql_ntuples(&sql);
eq->num_entries = rows;
for (row = 0; row < rows; row++) {
// Fetch & copy data to entry
entry = kore_malloc(sizeof(struct entry));
entry->id = kore_strdup(kore_pgsql_getvalue(&sql, row, 0));
entry->title = kore_strdup(kore_pgsql_getvalue(&sql, row, 1));
entry->created_at = kore_strdup(kore_pgsql_getvalue(&sql, row, 2));
entry->updated_at = kore_strdup(kore_pgsql_getvalue(&sql, row, 3));
entry->body = kore_strdup(kore_pgsql_getvalue(&sql, row, 4));
eq->entries[row] = entry;
kore_log(
LOG_DEBUG, "id: %s; title %s; created_at %s, updated_at %s",
entry->id, entry->title, entry->created_at, entry->updated_at
);
}
out: ;
kore_pgsql_cleanup(&sql);
return err;
}
int sql_update(const char *id, const char *title, const char *body) {
int err = KORE_RESULT_OK;
struct kore_pgsql sql;
kore_pgsql_init(&sql);
err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
if (err == KORE_RESULT_ERROR) {
kore_pgsql_logerror(&sql);
goto out;
}
err = kore_pgsql_query_params(
&sql, q_update_entry, 0, 3, KORE_PGSQL_PARAM_TEXT(title), KORE_PGSQL_PARAM_TEXT(body),
KORE_PGSQL_PARAM_TEXT(id)
);
if (err == KORE_RESULT_ERROR) {
kore_pgsql_logerror(&sql);
goto out;
}
out: ;
kore_pgsql_cleanup(&sql);
return err;
}
// int sql_delete_entry(const char *id) {
// int err = KORE_RESULT_OK;
// struct kore_pgsql sql;
// kore_pgsql_init(&sql);
// err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
// if (err == KORE_RESULT_ERROR) {
// kore_pgsql_logerror(&sql);
// goto out;
// }
// err = kore_pgsql_query_params(
// &sql,
// q_delete_entry,
// 0,
// 1,
// KORE_PGSQL_PARAM_TEXT(id)
// );
// if (err == KORE_RESULT_ERROR) {
// kore_pgsql_logerror(&sql);
// goto out;
// }
// out: ;
// kore_pgsql_cleanup(&sql);
// return err;
// }
static int render_md(const char *in, struct kore_buf *out) {
// Not a kore err.
int err = 0;
static unsigned parser_flags = 0;
static unsigned renderer_flags = 0;
parser_flags |= MD_FLAG_TABLES;
parser_flags |= MD_FLAG_STRIKETHROUGH;
// parser_flags |= MD_FLAG_TASKLISTS;
// render_flags |= MD_HTML_FLAG_DEBUG;
err = md_html(
in, (MD_SIZE) strlen(in), process_md_output, (void*) out, parser_flags, renderer_flags
);
if(err != 0) {
kore_log(LOG_ERR, "Parsing Markdown failed.");
return KORE_RESULT_ERROR;
}
return KORE_RESULT_OK;
}
static void process_md_output(const MD_CHAR *html, MD_SIZE size, void *buf) {
kore_buf_append((struct kore_buf *) buf, (const void *) html, (size_t) size);
}
|