Mercurial > sci
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scid/pageutil.c Thu Aug 04 14:13:58 2022 +0200 @@ -0,0 +1,76 @@ +/* + * pageutil.c -- page utilities + * + * Copyright (c) 2021 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> + +#include "pageutil.h" +#include "theme.h" +#include "scid.h" + +void +pageutil_render(struct kreq *req, + enum khttp status, + enum kmime mime, + const char *content) +{ + assert(req); + + khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[mime]); + khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status]); + + if (content) { + khttp_body(req); + khttp_printf(req, "%s", content); + } + + khttp_free(req); +} + +void +pageutil_status(struct kreq *req, enum khttp status) +{ + assert(req); + + char *body; + + body = theme_page_status(scid.theme, status); + pageutil_render(req, status, KMIME_TEXT_HTML, body); + free(body); +} + +void +pageutil_json(struct kreq *req, enum khttp status, json_t *doc) +{ + assert(req); + + char *body; + + khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]); + khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status]); + + if (doc) { + if ((body = json_dumps(doc, JSON_COMPACT))) { + khttp_body(req); + khttp_printf(req, "%s", body); + } + + json_decref(doc); + } + + khttp_free(req); +}