about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-10 09:38:46 -0400
committerJune McEnroe <june@causal.agency>2020-07-10 09:38:46 -0400
commitbc8b57c001c05f20579b0fdd6c75e26ee6255ffc (patch)
tree7eba86206c297425e55d8fdf5b7299600b6a9a76
parentAdd default stylesheet (diff)
downloadscooper-bc8b57c001c05f20579b0fdd6c75e26ee6255ffc.tar.gz
scooper-bc8b57c001c05f20579b0fdd6c75e26ee6255ffc.zip
Serve stylesheet separately, with caching
-rw-r--r--Makefile2
-rw-r--r--default.css5
-rw-r--r--html.c29
-rw-r--r--server.c14
-rw-r--r--server.h3
5 files changed, 28 insertions, 25 deletions
diff --git a/Makefile b/Makefile
index 4894309..56f5f15 100644
--- a/Makefile
+++ b/Makefile
@@ -24,7 +24,7 @@ scooper: ${OBJS}
 
 ${OBJS}: server.h
 
-html.o: default.css.h
+server.o: default.css.h
 
 default.css.h: default.css
 	file2c < default.css > $@
diff --git a/default.css b/default.css
index 7465c01..edd2af3 100644
--- a/default.css
+++ b/default.css
@@ -1,3 +1,8 @@
 html {
 	font-family: monospace;
 }
+
+body {
+	max-width: 130ch;
+	margin: auto;
+}
diff --git a/html.c b/html.c
index c6b4a42..dbc6631 100644
--- a/html.c
+++ b/html.c
@@ -21,27 +21,7 @@
 
 #include "server.h"
 
-static const char DefaultStyle[] = {
-#include "default.css.h"
-	,'\0'
-};
-
-const char *htmlStylesheet;
-
-static enum kcgi_err stylesheet(struct khtmlreq *html) {
-	if (htmlStylesheet) {
-		return khtml_attr(
-			html, KELEM_LINK,
-			KATTR_REL, "stylesheet",
-			KATTR_HREF, htmlStylesheet,
-			KATTR__MAX
-		);
-	}
-	return 0
-		|| khtml_elem(html, KELEM_STYLE)
-		|| khtml_puts(html, DefaultStyle)
-		|| khtml_closeelem(html, 1);
-}
+const char *htmlStylesheet = "stylesheet.css";
 
 enum kcgi_err htmlHead(struct khtmlreq *html, const char *title) {
 	return khtml_elem(html, KELEM_DOCTYPE)
@@ -49,7 +29,12 @@ enum kcgi_err htmlHead(struct khtmlreq *html, const char *title) {
 		|| khtml_elem(html, KELEM_TITLE)
 		|| khtml_puts(html, title)
 		|| khtml_closeelem(html, 1)
-		|| stylesheet(html)
+		|| khtml_attr(
+			html, KELEM_LINK,
+			KATTR_REL, "stylesheet",
+			KATTR_HREF, htmlStylesheet,
+			KATTR__MAX
+		)
 		|| khtml_elem(html, KELEM_H1)
 		|| khtml_puts(html, title)
 		|| khtml_closeelem(html, 1);
diff --git a/server.c b/server.c
index 751d414..ac8fc4f 100644
--- a/server.c
+++ b/server.c
@@ -48,6 +48,11 @@ const struct kvalid Keys[KeysLen] = {
 bool pagePublic;
 int pageLimit = 100;
 
+static const char CSS[] = {
+#include "default.css.h"
+};
+static const char *Cache = "public, max-age=86400, immutable";
+
 static enum kcgi_err request(struct kreq *req) {
 	if (req->method != KMETHOD_HEAD && req->method != KMETHOD_GET) {
 		return httpFail(req, KHTTP_405);
@@ -57,7 +62,14 @@ static enum kcgi_err request(struct kreq *req) {
 		case Contexts: return pageContexts(req);
 		case Events:   return pageEvents(req);
 		case Search:   return pageSearch(req);
-		default:       return httpFail(req, KHTTP_404);
+		case Stylesheet: {
+			if (req->mime != KMIME_TEXT_CSS) return httpFail(req, KHTTP_404);
+			return httpHead(req, KHTTP_200, KMIME_TEXT_CSS)
+				|| khttp_head(req, kresps[KRESP_CACHE_CONTROL], "%s", Cache)
+				|| khttp_body(req)
+				|| khttp_write(req, CSS, sizeof(CSS));
+		}
+		default: return httpFail(req, KHTTP_404);
 	}
 }
 
diff --git a/server.h b/server.h
index b1a22ee..f863b75 100644
--- a/server.h
+++ b/server.h
@@ -80,7 +80,8 @@ dbBindText(sqlite3_stmt *stmt, const char *param, const char *value) {
 	X(Networks, "networks") \
 	X(Contexts, "contexts") \
 	X(Events, "events") \
-	X(Search, "search")
+	X(Search, "search") \
+	X(Stylesheet, "stylesheet")
 
 enum {
 #define X(page, path) page,