commit 347ca498726cd4b495130349d066c3b038b2afec
parent 56cbca7f9369198fd850b7cc53d45acdbcfe31cc
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Tue, 26 Mar 2024 19:49:07 -0700
Have caller pass error page function to showpage
This way error pages can be custom to the site.
Diffstat:
5 files changed, 33 insertions(+), 17 deletions(-)
diff --git a/page.c b/page.c
@@ -116,7 +116,8 @@ errorpage(struct pagedata *pd, enum khttp code)
enum kcgi_err
showpage(struct pagedata *pd, char **names,
- enum kcgi_err (*functions[])(struct pagedata *), size_t len)
+ enum kcgi_err (*functions[])(struct pagedata *), size_t len,
+ enum kcgi_err (*errorpage)(struct pagedata *, enum khttp))
{
enum kcgi_err status;
char *ident;
diff --git a/page.h b/page.h
@@ -23,5 +23,6 @@ void freepagerequest(struct pagedata *pd);
enum kcgi_err startpage(struct pagedata *pd, struct pagetemplate *t, enum khttp status);
enum kcgi_err endpage(struct pagedata *pd, struct pagetemplate *t);
enum kcgi_err showpage(struct pagedata *pd, char **names,
- enum kcgi_err (*functions[])(struct pagedata *), size_t len);
+ enum kcgi_err (*functions[])(struct pagedata *), size_t len,
+ enum kcgi_err (*errorpage)(struct pagedata *, enum khttp));
enum kcgi_err errorpage(struct pagedata *pd, enum khttp code);
diff --git a/pages/common.c b/pages/common.c
@@ -105,3 +105,25 @@ tk_htmlerror(struct pagedata *pd, char *fmt, ...)
return status;
return khtml_closeelem(&pd->html, 1);
}
+
+enum kcgi_err
+tk_errorpage(struct pagedata *pd, enum khttp code)
+{
+ static char *css[] = { "css/main.css", NULL };
+ static struct pagetemplate template = {
+ "Unsuccessful",
+ .css = css
+ };
+
+ enum kcgi_err status;
+
+ kutil_warn(&pd->req, pd->user ? pd->user->name : NULL,
+ "%s: %s", pd->req.fullpath, khttps[code]);
+
+ if ((status = tk_startpage(pd, &template, code)) != KCGI_OK ||
+ (status = khtml_elem(&pd->html, KELEM_H1)) != KCGI_OK ||
+ (status = khtml_printf(&pd->html, "%s", khttps[code])) != KCGI_OK ||
+ (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
+ return status;
+ return tk_endpage(pd, &template);
+}
diff --git a/pages/common.h b/pages/common.h
@@ -15,3 +15,4 @@ enum kcgi_err htmlheader(struct pagedata *pd);
enum kcgi_err tk_startpage(struct pagedata *pd, struct pagetemplate *t,
enum khttp code);
enum kcgi_err tk_htmlerror(struct pagedata *pd, char *fmt, ...);
+enum kcgi_err tk_errorpage(struct pagedata *pd, enum khttp code);
diff --git a/timekeeper.c b/timekeeper.c
@@ -2,30 +2,20 @@
#define const
-#include <sys/types.h>
#include <err.h>
#include <limits.h>
-#include <stdarg.h>
-#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
-#include <kcgi.h>
-#include <kcgihtml.h>
-
#include <sqlbox.h>
-#include "page.h"
-#include "html.h"
-#include "user.h"
-#include "stmt.h"
-#include "key.h"
-#include "pages/util.h"
-#include "pages/pages.h"
+#include "config.h"
+#include "pages/common.h"
+#include "pages/pages.h"
+#include "stmt.h"
#include "util.h"
-#include "config.h"
#define Promises "stdio rpath wpath cpath proc recvfd unix sendfd"
@@ -100,7 +90,8 @@ main(void)
status = KCGI_OK;
while ((status = loadpagerequest(fcgi, &pd)) == KCGI_OK) {
/* Ignore return value */
- showpage(&pd, pages, pagefunctions, Len(pages));
+ showpage(&pd, pages, pagefunctions, Len(pages),
+ tk_errorpage);
freepagerequest(&pd);
}