From 4207e2029557fc1090e62ab2be765e48a070997e Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Thu, 25 May 2023 11:21:50 -0600 Subject: Better organize main qs handling and improve 404 This moves page handling logic into a new function, `handle_page`, and special qs handling logic to `handle_special`. This keeps code better organized. This also better handles 404 pages. If a custom 404.html is available, it is treated as a normal page and parsed correctly. If none exists, a generic 404 is sent in all scenarios where no logic handled the query string. --- src/main.c | 94 ++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/src/main.c b/src/main.c index 4578318..ac5b14c 100644 --- a/src/main.c +++ b/src/main.c @@ -24,60 +24,51 @@ #include "j2.h" -int main(int argc, char* argv[], char* envp[]) { - char buf[256]; - char path[512]; - int status = 200; - - // Space for title assembly - char titlebuf[TITLE_MAX] = {'\0'}; - char title[TITLE_MAX + 2] = {'\0'}; - - // Required environment variables - char* header = getenv("INDEX_HEADER"); - char* footer = getenv("INDEX_FOOTER"); - char* posts = getenv("INDEX_POSTS"); - - // List of supported query string key names - char* keys[] = {"q", "p", "title", "\0"}; - // Parse the query string (if present) - struct querystring qs; - qs.qs = getenv("QUERY_STRING"); - qs.pos = 0; - - // If no title specified (return non-zero), default to "index" - if(cgi_qs_get_val(&qs, keys, buf) != 0) - strcpy(buf, "index"); - - // If the IP address was requested - if(strncmp(buf, "ip", 2) == 0) { +int handle_special(char* querystring) { + if(strncmp(querystring, "ip", 2) == 0) { + // If the IP address was requested cgi_print_header("text/plain", 200); - printf("%s\n", cgi_remote_ip()); + puts(cgi_remote_ip()); return 0; - } - // If the remote agent was requested - if(strncmp(buf, "agent", 5) == 0) { + } else if(strncmp(querystring, "agent", 5) == 0) { + // If the remote agent was requested cgi_print_header("text/plain", 200); - printf("%s\n", cgi_user_agent()); + puts(cgi_user_agent()); return 0; - } - // If client headers requested - if(strncmp(buf, "headers", 5) == 0) { + } else if(strncmp(querystring, "headers", 5) == 0) { + // If client headers requested cgi_print_header("text/plain", 200); cgi_print_headers(); return 0; } + return -1; +} + +int handle_page(char* querystring) { + char path[512] = {0}; + int status = 200; + // Space for title assembly + char titlebuf[TITLE_MAX] = {'\0'}; + char title[TITLE_MAX + 2] = {'\0'}; + // Required environment variables + char* header = getenv("INDEX_HEADER"); + char* footer = getenv("INDEX_FOOTER"); + char* posts = getenv("INDEX_POSTS"); // Assemble the page path - sprintf(path, "%s/%s.html", posts, buf); + sprintf(path, "%s/%s.html", posts, querystring); - // If the file does not exist, set status to 404 and set the page to be - // printed to the 404 page. + // If the file does not exist, attempt to read the 404 page instead + // Return -1 if 404 page does not exist if(access(path, F_OK) != 0) { + sprintf(path, "%s/404.html", posts); + + if(access(path, F_OK) != 0) + return -1; + status = 404; - sprintf(path, "%s/%s.html", posts, "404"); strcpy(title, ": 404"); } @@ -94,6 +85,31 @@ int main(int argc, char* argv[], char* envp[]) { j2_cat(path); j2_cat(footer); fflush(stdout); + return 0; +} + +int main(int argc, char* argv[], char* envp[]) { + char buf[256] = {0}; + char* keys[] = {"q", "p", "title", "\0"}; // Supported query string key names + struct querystring qs; + // Parse the query string (if present) + qs.qs = getenv("QUERY_STRING"); + qs.pos = 0; + + // If no title specified (return non-zero), default to "index" + if(cgi_qs_get_val(&qs, keys, buf) != 0) + strcpy(buf, "index"); + + if(handle_special(buf) == 0) + return 0; + if(handle_page(buf) == 0) + return 0; + + // All attempts to recognize the request have failed. Return generic 404. + cgi_print_header("text/html", 404); + setenv("PAGE_TITLE", "404 Not Found", 1); + puts("404 Not Found"); + fflush(stdout); return 0; } -- cgit v1.2.3