: nply/minci

commit fb66de5df1da0ba361816b7d1e70722c4f1be9e3
parent a0c7d6a5a3270ef427619ddea45217c33396eae0
Author: kristapsdz <kristaps@bsd.lv>
Date: Thu, 12 Mar 2020 20:19:53 +0000

Move if-modified-since check to the beginning to avoid hitting the database.
Also output charset.
1 file changed, 21 insertions(+), 22 deletions(-)
M
main.c
|
43
+++++++++++++++++++++----------------------
diff --git a/main.c b/main.c
@@ -95,6 +95,9 @@ html_open(struct khtmlreq *req, const char *title)
 		KATTR_NAME, "viewport",
 		KATTR_CONTENT, "width=device-width, initial-scale=1",
 		KATTR__MAX);
+	khtml_attr(req, KELEM_META, 
+		KATTR_CHARSET, "utf-8",
+		KATTR__MAX);
 	khtml_attrx(req, KELEM_LINK, 
 		KATTR_REL, KATTRX_STRING, "stylesheet",
 		KATTR_HREF, KATTRX_STRING, "/minci.css",

@@ -490,28 +493,6 @@ get_last(struct kreq *r, time_t mtime)
 {
 	struct req	 req;
 	struct kpair	*kpn, *kpd;
-	char		*cp;
-	struct tm	 tm;
-	time_t		 cmptime;
-
-	/*
-	 * If the client is coming in with a If-Modified-Since, see if the
-	 * timestamp on our database file has changed.
-	 * For our application, this works: it's an append-only record.
-	 */
-
-	if (r->reqmap[KREQU_IF_MODIFIED_SINCE] != NULL) {
-		memset(&tm, 0, sizeof(struct tm));
-		cp = strptime
-			(r->reqmap[KREQU_IF_MODIFIED_SINCE]->val,
-			 "%a, %d %b %Y %T GMT", &tm);
-		if (cp != NULL && 
-		    (cmptime = mktime(&tm)) != -1 &&
-		    mtime <= cmptime) {
-			http_open(r, KHTTP_304, r->mime, 0);
-			return;
-		}
-	}
 
 	/* Both of these are optional. */
 

@@ -826,6 +807,9 @@ main(void)
 	struct kreq	 r;
 	enum kcgi_err	 er;
 	struct stat	 st;
+	struct tm	 tm;
+	char		*cp;
+	time_t		 t;
 
 	/* Basic checks: parse and valid page. */
 

@@ -856,6 +840,21 @@ main(void)
 		return EXIT_FAILURE;
 	}
 
+	if (r.method == KMETHOD_GET &&
+	    r.reqmap[KREQU_IF_MODIFIED_SINCE] != NULL) {
+		memset(&tm, 0, sizeof(struct tm));
+		cp = strptime
+			(r.reqmap[KREQU_IF_MODIFIED_SINCE]->val,
+			 "%a, %d %b %Y %T GMT", &tm);
+		if (cp != NULL && 
+		    (t = mktime(&tm)) != -1 &&
+		    st.st_mtime <= t) {
+			http_open(&r, KHTTP_304, r.mime, 0);
+			khttp_free(&r);
+			return EXIT_SUCCESS;
+		}
+	}
+
 	/* Open the database. */
 
 	if ((r.arg = db_open_logging