timekeeper

[Abandoned unfinished] CGI web application in C for time tracking. (My first, just a learning project)
Log | Files | Refs | README

commit 4ed7276d30b480f7015e0857142745b52a7ed0d0
parent 47cfc6579921ed85a04a3a3b35782733be9897e9
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Tue, 12 Mar 2024 08:38:03 -0700

Remove 404 page and update showpage()

Instead of defining a special page for the 404 error just call
errorpage() in showpage() if a page is not found.

Diffstat:
MMakefile | 2+-
Mpage.c | 40++++++++++++++++++++++++++++++++--------
Mpage.h | 1+
Dpages/404.c | 7-------
Mpages/pages.h | 2--
Mpages/util.c | 14--------------
Mpages/util.h | 1-
Mtimekeeper.c | 6++----
8 files changed, 36 insertions(+), 37 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,7 +4,7 @@ cflags = ${CFLAGS} -O0 -Wall -Wextra -I/usr/local/include lddflags = ${LDDFLAGS} -static -L/usr/local/lib -lkcgi -lkcgihtml -lz -lsqlbox -lsqlite3 -lm -lpthread prefix = /var/www/htdocs/${name}.primus.lan -pages = pages/index.c pages/404.c pages/login.c pages/logout.c \ +pages = pages/index.c pages/login.c pages/logout.c \ pages/account.c pages/main.c pages/export.c hdrsrc = page.c html.c user.c stmt.c key.c times.c src = ${hdrsrc} ${pages} pages/util.c diff --git a/page.c b/page.c @@ -40,20 +40,44 @@ freepagerequest(struct pagedata *pd) } enum kcgi_err +errorpage(struct pagedata *pd, enum khttp code) +{ + enum kcgi_err status; + + kutil_warn(&pd->req, pd->user ? pd->user->name : NULL, + "%s: %s", pd->req.fullpath, khttps[code]); + + if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS], + "%s", khttps[code])) != KCGI_OK || + (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_TYPE], + "%s", kmimetypes[KMIME_TEXT_HTML])) != KCGI_OK || + (status = khttp_body(&pd->req))) + return status; + return khttp_printf(&pd->req, + "<!DOCTYPE HTML><html><head><title>%1$s</title></head><h1>%1$s</h1></html>", + khttps[code]); +} + +enum kcgi_err showpage(struct pagedata *pd, char **names, enum kcgi_err (*functions[])(struct pagedata *), size_t len) { enum kcgi_err status; + char *ident; - if (pd->req.page == len) - status = functions[len - 1](pd); - else - status = functions[pd->req.page](pd); + ident = pd->user ? pd->user->name : NULL; - if (status != KCGI_OK) { - kutil_warn(&pd->req, NULL, "showpage: page %s returned %d", - names[(pd->req.page == len ? (len - 1) : pd->req.page)], - status); + if (pd->req.page == len) { + kutil_info(&pd->req, ident, + "%s: Invalid page requested", pd->req.fullpath); + status = errorpage(pd, KHTTP_404); + } else { + kutil_info(&pd->req, ident, + "%s: Serving CGI page", pd->pages[pd->req.page]); + status = functions[pd->req.page](pd); + if (status != KCGI_OK) + kutil_warn(&pd->req, ident, "%s: returned %d", + names[pd->req.page], status); } return status; diff --git a/page.h b/page.h @@ -14,3 +14,4 @@ enum kcgi_err loadpagerequest(struct kfcgi *fcgi, struct pagedata *pd); void freepagerequest(struct pagedata *pd); enum kcgi_err showpage(struct pagedata *pd, char **names, enum kcgi_err (*functions[])(struct pagedata *), size_t len); +enum kcgi_err errorpage(struct pagedata *pd, enum khttp code); diff --git a/pages/404.c b/pages/404.c @@ -1,7 +0,0 @@ -#include "common.h" - -enum kcgi_err -page404(struct pagedata *pd) -{ - return errorpage(pd, KHTTP_404); -} diff --git a/pages/pages.h b/pages/pages.h @@ -5,11 +5,9 @@ enum Page { PageAccount, PageMain, PageExport, - Page404, PageMax }; -enum kcgi_err page404(struct pagedata *pd); enum kcgi_err pageindex(struct pagedata *pd); enum kcgi_err pagelogin(struct pagedata *pd); enum kcgi_err pagelogout(struct pagedata *pd); diff --git a/pages/util.c b/pages/util.c @@ -11,20 +11,6 @@ #include "../key.h" enum kcgi_err -errorpage(struct pagedata *pd, enum khttp code) -{ - enum kcgi_err status; - - if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS], "%s", khttps[code])) != KCGI_OK || - (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML])) != KCGI_OK || - (status = khttp_body(&pd->req))) - return status; - return khttp_printf(&pd->req, - "<!doctype HTML><html><head><title>%1$s</title></head><h1>%1$s</h1></html>", - khttps[code]); -} - -enum kcgi_err starthtmldoc(struct pagedata *pd, enum khttp code, char *title) { enum kcgi_err status; diff --git a/pages/util.h b/pages/util.h @@ -1,3 +1,2 @@ -enum kcgi_err errorpage(struct pagedata *pd, enum khttp code); enum kcgi_err starthtmldoc(struct pagedata *pd, enum khttp code, char *title); enum kcgi_err redirect(struct pagedata *pd, char *to, char *msg); diff --git a/timekeeper.c b/timekeeper.c @@ -34,8 +34,7 @@ static char *pages[] = { [PageLogout] = "logout", [PageAccount] = "account", [PageMain] = "main", - [PageExport] = "export", - [Page404] = "404" + [PageExport] = "export" }; int @@ -68,8 +67,7 @@ main(void) [PageLogout] = pagelogout, [PageAccount] = pageaccount, [PageMain] = pagemain, - [PageExport] = pageexport, - [Page404] = page404 + [PageExport] = pageexport }; memset(&cfg, 0, sizeof(cfg));