diff options
Diffstat (limited to 'cgi.c')
-rw-r--r-- | cgi.c | 72 |
1 files changed, 42 insertions, 30 deletions
@@ -1,6 +1,6 @@ /* $Id$ */ /* - * Copyright (c) 2014-2019, 2021 Ingo Schwarze <schwarze@usta.de> + * Copyright (c) 2014-2019, 2021, 2022 Ingo Schwarze <schwarze@usta.de> * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2022 Anna Vyalkova <cyber@sysrq.in> * @@ -87,10 +87,10 @@ static void pg_search(const struct req *); static void pg_searchres(const struct req *, struct manpage *, size_t); static void pg_show(struct req *, const char *); -static void resp_begin_html(int, const char *, const char *); +static int resp_begin_html(int, const char *, const char *); static void resp_begin_http(int, const char *); static void resp_catman(const struct req *, const char *); -static void resp_copy(const char *); +static int resp_copy(const char *, const char *); static void resp_end_html(void); static void resp_format(const struct req *, const char *); static void resp_searchform(const struct req *, enum focus); @@ -353,22 +353,26 @@ resp_begin_http(int code, const char *msg) fflush(stdout); } -static void -resp_copy(const char *filename) +static int +resp_copy(const char *element, const char *filename) { char buf[4096]; ssize_t sz; int fd; - if ((fd = open(filename, O_RDONLY)) != -1) { - fflush(stdout); - while ((sz = read(fd, buf, sizeof(buf))) > 0) - write(STDOUT_FILENO, buf, sz); - close(fd); - } + if ((fd = open(filename, O_RDONLY)) == -1) + return 0; + + if (element != NULL) + printf("<%s>\n", element); + fflush(stdout); + while ((sz = read(fd, buf, sizeof(buf))) > 0) + write(STDOUT_FILENO, buf, sz); + close(fd); + return 1; } -static void +static int resp_begin_html(int code, const char *msg, const char *file) { const char *name, *sec, *cp; @@ -414,14 +418,14 @@ resp_begin_html(int code, const char *msg, const char *file) "<body>\n", CUSTOMIZE_TITLE); - resp_copy(MAN_DIR "/header.html"); + return resp_copy("header", MAN_DIR "/header.html"); } static void resp_end_html(void) { - - resp_copy(MAN_DIR "/footer.html"); + if (resp_copy("footer", MAN_DIR "/footer.html")) + puts("</footer>"); puts("</body>\n" "</html>"); @@ -432,8 +436,7 @@ resp_searchform(const struct req *req, enum focus focus) { int i; - printf("<header>\n" - "<form role=\"search\" action=\"/%s\" method=\"get\" " + printf("<form role=\"search\" action=\"/%s\" method=\"get\" " "autocomplete=\"off\" autocapitalize=\"none\">\n" " <fieldset>\n" " <legend>Manual Page Search Parameters</legend>\n", @@ -501,8 +504,7 @@ resp_searchform(const struct req *req, enum focus focus) } puts(" </fieldset>\n" - "</form>\n" - "</header>"); + "</form>"); } static int @@ -557,10 +559,11 @@ validate_filename(const char *file) static void pg_index(const struct req *req) { - - resp_begin_html(200, NULL, NULL); + if (resp_begin_html(200, NULL, NULL) == 0) + puts("<header>"); resp_searchform(req, FOCUS_QUERY); - printf("<main>\n" + printf("</header>\n" + "<main>\n" "<p role=\"doc-notice\" aria-label=\"usage\">\n" "This web interface is documented in the\n" "<a class=\"Xr\" href=\"/%s%sman.cgi.8\"" @@ -580,8 +583,10 @@ static void pg_noresult(const struct req *req, int code, const char *http_msg, const char *user_msg) { - resp_begin_html(code, http_msg, NULL); + if (resp_begin_html(code, http_msg, NULL) == 0) + puts("<header>"); resp_searchform(req, FOCUS_QUERY); + puts("</header>"); puts("<main>"); puts("<p role=\"doc-notice\" aria-label=\"no result\">"); puts(user_msg); @@ -593,8 +598,8 @@ pg_noresult(const struct req *req, int code, const char *http_msg, static void pg_error_badrequest(const char *msg) { - - resp_begin_html(400, "Bad Request", NULL); + if (resp_begin_html(400, "Bad Request", NULL)) + puts("</header>"); puts("<main>\n" "<h1>Bad Request</h1>\n" "<p role=\"doc-notice\" aria-label=\"Bad Request\">"); @@ -602,14 +607,15 @@ pg_error_badrequest(const char *msg) printf("Try again from the\n" "<a href=\"/%s\">main page</a>.\n" "</p>\n" - "</main>", scriptname); + "</main>\n", scriptname); resp_end_html(); } static void pg_error_internal(void) { - resp_begin_html(500, "Internal Server Error", NULL); + if (resp_begin_html(500, "Internal Server Error", NULL)) + puts("</header>"); puts("<main><p role=\"doc-notice\">Internal Server Error</p></main>"); resp_end_html(); } @@ -641,6 +647,7 @@ pg_searchres(const struct req *req, struct manpage *r, size_t sz) size_t i, iuse; int archprio, archpriouse; int prio, priouse; + int have_header; for (i = 0; i < sz; i++) { if (validate_filename(r[i].file)) @@ -707,12 +714,15 @@ pg_searchres(const struct req *req, struct manpage *r, size_t sz) priouse = prio; iuse = i; } - resp_begin_html(200, NULL, r[iuse].file); + have_header = resp_begin_html(200, NULL, r[iuse].file); } else - resp_begin_html(200, NULL, NULL); + have_header = resp_begin_html(200, NULL, NULL); + if (have_header == 0) + puts("<header>"); resp_searchform(req, req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY); + puts("</header>"); if (sz > 1) { puts("<nav>"); @@ -983,8 +993,10 @@ pg_show(struct req *req, const char *fullpath) return; } - resp_begin_html(200, NULL, file); + if (resp_begin_html(200, NULL, file) == 0) + puts("<header>"); resp_searchform(req, FOCUS_NONE); + puts("</header>"); resp_show(req, file); resp_end_html(); } |