: nply/minci

commit 87e5b0d77a92ccb7b96afd8b9c04cea0ad8cd5e2
parent fa3f7317b651ae428f3d0b671af49a37a02f5ae1
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 12 Sep 2026 21:47:38 +0530

Support FastCGI serving
2 files changed, 75 insertions(+), 42 deletions(-)
M
Makefile
|
8
++++++--
M
main.c
|
109
++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
diff --git a/Makefile b/Makefile
@@ -13,7 +13,7 @@ LDADD		+= $(LIBS_PKG)
 
 OBJS 		 = db.o main.o
 
-all: minci.cgi
+all: minci.cgi minci.fcgi
 
 installcgi: updatecgi
 	mkdir -p $(WWWPREFIX)/data

@@ -24,6 +24,7 @@ updatecgi: all
 	mkdir -p $(WWWPREFIX)/htdocs
 	install -o www -m 0444 minci.css $(WWWPREFIX)/htdocs
 	install -o www -m 0500 minci.cgi $(WWWPREFIX)/cgi-bin
+	install -o www -m 0500 minci.fcgi $(WWWPREFIX)/cgi-bin
 
 testupdatedb:
 	ort-sqldiff $(WWWPREFIX)/data/minci.ort db.ort || true

@@ -38,8 +39,11 @@ updatedb:
 minci.cgi: $(OBJS) minci.db
 	$(CC) -o $@ -static $(OBJS) $(LDFLAGS) $(LDADD)
 
+minci.fcgi: minci.cgi
+	cp minci.cgi $@
+
 clean:
-	rm -f $(OBJS) minci.cgi db.c extern.h minci.db db.sql
+	rm -f $(OBJS) minci.cgi minci.fcgi db.c extern.h minci.db db.sql
 
 $(OBJS): extern.h
 
diff --git a/main.c b/main.c
@@ -2071,28 +2071,16 @@ out:
 	free(buf);
 }
 
-int
-main(void)
+static int
+serve(struct kreq *r)
 {
-	struct kreq	 r;
-	enum kcgi_err	 er;
 	struct stat	 st;
 	struct tm	 tm;
 	char		*cp;
 	time_t		 t;
 
-	/* Basic checks: parse and valid page. */
-
-	er = khttp_parse(&r, valid_keys,
-		VALID__MAX, pages, PAGE__MAX, PAGE_INDEX);
-
-	if (er != KCGI_OK)
-		kutil_errx(&r, NULL, 
-			"khttp_parse: %s", kcgi_strerror(er));
-
-	if (r.page == PAGE__MAX) {
-		http_open(&r, KHTTP_404, KMIME__MAX, 0);
-		khttp_free(&r);
+	if (r->page == PAGE__MAX) {
+		http_open(r, KHTTP_404, KMIME__MAX, 0);
 		return EXIT_SUCCESS;
 	}
 

@@ -2105,56 +2093,97 @@ main(void)
 	 */
 
 	if (stat(DATADIR "/minci.db", &st) == -1) {
-		kutil_err(&r, NULL, DATADIR "/minci.db");
-		khttp_free(&r);
+		kutil_err(r, NULL, DATADIR "/minci.db");
 		return EXIT_FAILURE;
 	}
 
-	if (r.method == KMETHOD_GET &&
-	    r.reqmap[KREQU_IF_MODIFIED_SINCE] != NULL) {
+	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,
+			(r->reqmap[KREQU_IF_MODIFIED_SINCE]->val,
 			 "%a, %d %b %Y %T GMT", &tm);
-		if (cp != NULL && 
+		if (cp != NULL &&
 		    (t = mktime(&tm)) != -1 &&
 		    st.st_mtime <= t) {
-			http_open(&r, KHTTP_304, r.mime, 0);
-			khttp_free(&r);
+			http_open(r, KHTTP_304, r->mime, 0);
 			return EXIT_SUCCESS;
 		}
 	}
 
 	/* Open the database. */
 
-	if ((r.arg = db_open_logging
-	    (DATADIR "/minci.db", NULL, warnx, NULL)) == NULL) {
-		kutil_errx(&r, NULL, "db_open: %s", 
-			DATADIR "/minci.db");
-		khttp_free(&r);
-		return EXIT_FAILURE;
-	}
+	if ((r->arg = db_open_logging
+	    (DATADIR "/minci.db", NULL, warnx, NULL)) == NULL)
+		kutil_errx(r, NULL, "db_open: %s", DATADIR "/minci.db");
 
 #ifdef __OpenBSD__
 	if (pledge("stdio", NULL) == -1) {
 		kutil_warn(NULL, NULL, "pledge");
-		db_close(r.arg);
-		khttp_free(&r);
+		db_close(r->arg);
 		return EXIT_FAILURE;
 	}
 #endif
 
 	/* Switch on method, not resource. */
 
-	if (r.method == KMETHOD_POST) {
-		db_role(r.arg, ROLE_producer);
-		post(&r);
+	if (r->method == KMETHOD_POST) {
+		db_role(r->arg, ROLE_producer);
+		post(r);
 	} else {
-		db_role(r.arg, ROLE_consumer);
-		get(&r, st.st_mtime);
+		db_role(r->arg, ROLE_consumer);
+		get(r, st.st_mtime);
 	}
 
-	db_close(r.arg);
-	khttp_free(&r);
+	db_close(r->arg);
 	return EXIT_SUCCESS;
 }
+
+static int
+main_cgi(void)
+{
+	struct kreq	 r;
+	enum kcgi_err	 er;
+	int		 rc;
+
+	er = khttp_parse(&r, valid_keys,
+		VALID__MAX, pages, PAGE__MAX, PAGE_INDEX);
+	if (er != KCGI_OK)
+		kutil_errx(&r, NULL,
+			"khttp_parse: %s", kcgi_strerror(er));
+	rc = serve(&r);
+	khttp_free(&r);
+	return rc;
+}
+
+static int
+main_fcgi(void)
+{
+	struct kfcgi	*fcgi;
+	struct kreq	 r;
+	enum kcgi_err	 er;
+	int		 rc = EXIT_SUCCESS;
+
+	er = khttp_fcgi_init(&fcgi, valid_keys,
+		VALID__MAX, pages, PAGE__MAX, PAGE_INDEX);
+	if (er != KCGI_OK)
+		errx(EXIT_FAILURE, "khttp_fcgi_init: %s", kcgi_strerror(er));
+	while ((er = khttp_fcgi_parse(fcgi, &r)) == KCGI_OK) {
+		if (serve(&r) != EXIT_SUCCESS)
+			rc = EXIT_FAILURE;
+		khttp_free(&r);
+	}
+	if (er != KCGI_EXIT && er != KCGI_HUP) {
+		warnx("khttp_fcgi_parse: %s", kcgi_strerror(er));
+		rc = EXIT_FAILURE;
+	}
+	khttp_fcgi_free(fcgi);
+	return rc;
+}
+
+int
+main(void)
+{
+
+	return khttp_fcgi_test() ? main_fcgi() : main_cgi();
+}