diff options
author | Aaron Ball <nullspoon@oper.io> | 2023-05-25 11:27:26 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2023-05-25 11:27:26 -0600 |
commit | 432a282e3cb849ee8a5b29c8883f683bc6113229 (patch) | |
tree | e48774096acec1f48cb99f414c447c008dd06b90 | |
parent | 0096d75f76690d50010cd77a4d8416079ca38247 (diff) | |
parent | 4207e2029557fc1090e62ab2be765e48a070997e (diff) | |
download | oper.io-432a282e3cb849ee8a5b29c8883f683bc6113229.tar.gz oper.io-432a282e3cb849ee8a5b29c8883f683bc6113229.tar.xz |
Merge branch 'better-organize'
-rw-r--r-- | src/main.c | 94 |
1 files changed, 55 insertions, 39 deletions
@@ -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; } |