diff scid/page-static.c @ 26:7e10cace67a3

scid: add basic mustache support
author David Demelier <markand@malikania.fr>
date Tue, 02 Aug 2022 13:24:13 +0200
parents
children 695637f1d8a7
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scid/page-static.c	Tue Aug 02 13:24:13 2022 +0200
@@ -0,0 +1,65 @@
+/*
+ * page-static.c -- page /static
+ *
+ * Copyright (c) 2020-2022 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 <sys/types.h>
+#include <sys/stat.h>
+#include <assert.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include "config.h"
+#include "page.h"
+#include "scid.h"
+
+static void
+get(struct kreq *req)
+{
+	struct stat st;
+	char path[PATH_MAX];
+
+	snprintf(path, sizeof (path), "%s%s", scid.themedir, req->fullpath);
+
+	if (stat(path, &st) < 0)
+		page(req, KHTTP_404, KMIME_TEXT_HTML, "404.html", NULL);
+	else {
+		khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
+		khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[req->mime]);
+		khttp_head(req, kresps[KRESP_CONTENT_LENGTH],
+		    "%llu", (unsigned long long)(st.st_size));
+		khttp_body(req);
+		khttp_template(req, NULL, path);
+		khttp_free(req);
+	}
+}
+
+void
+page_static(struct kreq *r)
+{
+	assert(r);
+
+	switch (r->method) {
+	case KMETHOD_GET:
+		get(r);
+		break;
+	default:
+		page(r, KHTTP_400, KMIME_TEXT_HTML, "400.html", NULL);
+		break;
+	}
+}