comparison page.c @ 74:67b3d13a5035

pasterd: make own HTML code for good
author David Demelier <markand@malikania.fr>
date Wed, 15 Mar 2023 19:34:00 +0100
parents 6792975da9a0
children b12491ceabfd
comparison
equal deleted inserted replaced
73:6792975da9a0 74:67b3d13a5035
1 /* 1 /*
2 * page.c -- page renderer 2 * page.c -- page renderer
3 * 3 *
4 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr> 4 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
5 * 5 *
6 * Permission to use, copy, modify, and/or distribute this software for any 6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies. 8 * copyright notice and this permission notice appear in all copies.
9 * 9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <sys/types.h>
20 #include <assert.h> 19 #include <assert.h>
21 #include <stdarg.h> 20 #include <string.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 21
25 #include <kcgi.h> 22 #include <mustach-jansson.h>
26 #include <kcgihtml.h>
27 23
28 #include "fmt.h"
29 #include "page.h" 24 #include "page.h"
30 #include "util.h" 25 #include "util.h"
31 26
27 #include "html/footer.h"
32 #include "html/header.h" 28 #include "html/header.h"
33 #include "html/footer.h" 29 #include "html/status.h"
30
31 static const int statustab[] = {
32 [KHTTP_200] = 200,
33 [KHTTP_400] = 400,
34 [KHTTP_404] = 404,
35 [KHTTP_500] = 500
36 };
37
38 static const char * const statusmsg[] = {
39 [KHTTP_200] = "OK",
40 [KHTTP_400] = "Bad Request",
41 [KHTTP_404] = "Not Found",
42 [KHTTP_500] = "Internal Server Error"
43 };
44
45 static int
46 writer(void *data, const char *buffer, size_t size)
47 {
48 struct kreq *req = data;
49
50 khttp_write(req, buffer, size);
51
52 return MUSTACH_OK;
53 }
34 54
35 static void 55 static void
36 print_title(struct kreq *req, struct khtmlreq *html, const void *data) 56 format(struct kreq *req, const char *html, json_t *doc)
37 { 57 {
38 (void)req; 58 if (!doc)
39 59 khttp_template_buf(req, NULL, html, strlen(html));
40 khtml_printf(html, "%s", (const char *)data); 60 else
61 mustach_jansson_write(html, strlen(html), doc, 0, writer, req);
41 } 62 }
42 63
43 void 64 void
44 page(struct kreq *req, const struct ktemplate *tmpl, enum khttp status, const char *file, const char *title) 65 page(struct kreq *req, enum khttp status, const unsigned char *html, json_t *doc)
45 { 66 {
67 assert(req);
68 assert(html);
69
46 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); 70 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]);
47 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status]); 71 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status]);
48 khttp_body(req); 72 khttp_body(req);
73 format(req, (const char *)html_header, doc);
74 format(req, (const char *)html, doc);
75 format(req, (const char *)html_footer, doc);
76 khttp_free(req);
49 77
50 fmt(req, html_header, title, (const struct fmt_printer []) { 78 if (doc)
51 { "title", print_title }, 79 json_decref(doc);
52 { NULL, NULL }
53 });
54 fmt(req, html_footer, NULL, NULL);
55 khttp_free(req);
56 } 80 }
57 81
58 void 82 void
59 page2(struct kreq *req, 83 page_status(struct kreq *req, enum khttp status)
60 enum khttp status,
61 const char *title,
62 const unsigned char *html,
63 const void *data,
64 const struct fmt_printer *printers)
65 { 84 {
66 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); 85 assert(req);
67 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status]);
68 khttp_body(req);
69 86
70 fmt(req, html_header, title, (const struct fmt_printer []) { 87 page(req, status, html_status, json_pack("{si ss}",
71 { "title", print_title }, 88 "code", statustab[status],
72 { NULL, NULL } 89 "status", statusmsg[status]
73 }); 90 ));
74 fmt(req, html, data, printers);
75 fmt(req, html_footer, NULL, NULL);
76 khttp_free(req);
77 } 91 }