Mercurial > sci
comparison scid/pageutil.c @ 29:695637f1d8a7
scid: first index page in javascript
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 04 Aug 2022 14:13:58 +0200 |
parents | scid/page.c@7e10cace67a3 |
children | 43333d18e4b8 |
comparison
equal
deleted
inserted
replaced
28:4c16bb25e4f1 | 29:695637f1d8a7 |
---|---|
1 /* | |
2 * pageutil.c -- page utilities | |
3 * | |
4 * Copyright (c) 2021 David Demelier <markand@malikania.fr> | |
5 * | |
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 | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
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 | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <assert.h> | |
20 | |
21 #include "pageutil.h" | |
22 #include "theme.h" | |
23 #include "scid.h" | |
24 | |
25 void | |
26 pageutil_render(struct kreq *req, | |
27 enum khttp status, | |
28 enum kmime mime, | |
29 const char *content) | |
30 { | |
31 assert(req); | |
32 | |
33 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[mime]); | |
34 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status]); | |
35 | |
36 if (content) { | |
37 khttp_body(req); | |
38 khttp_printf(req, "%s", content); | |
39 } | |
40 | |
41 khttp_free(req); | |
42 } | |
43 | |
44 void | |
45 pageutil_status(struct kreq *req, enum khttp status) | |
46 { | |
47 assert(req); | |
48 | |
49 char *body; | |
50 | |
51 body = theme_page_status(scid.theme, status); | |
52 pageutil_render(req, status, KMIME_TEXT_HTML, body); | |
53 free(body); | |
54 } | |
55 | |
56 void | |
57 pageutil_json(struct kreq *req, enum khttp status, json_t *doc) | |
58 { | |
59 assert(req); | |
60 | |
61 char *body; | |
62 | |
63 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]); | |
64 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status]); | |
65 | |
66 if (doc) { | |
67 if ((body = json_dumps(doc, JSON_COMPACT))) { | |
68 khttp_body(req); | |
69 khttp_printf(req, "%s", body); | |
70 } | |
71 | |
72 json_decref(doc); | |
73 } | |
74 | |
75 khttp_free(req); | |
76 } |