commit 1a3017b4c2f07c7546e12661920ae8e86d320f51
parent ec4d9dcdef6e1be313614e40af9f38947dea07c7
Author: quantumish <freifeld.david@gmail.com>
Date: Fri, 14 Apr 2023 09:37:19 -0700
Support path redirects + logging to file, clean up code
Diffstat:
6 files changed, 31 insertions(+), 18 deletions(-)
diff --git a/http.c b/http.c
@@ -35,6 +35,7 @@ response_t serve_error(enum StatusCode c) {
}
shitvec_t paths;
+hashmap_t path_redirs;
char* get_file_ext(char* filename) {
char* ext = strtok(filename, ".");
@@ -113,7 +114,6 @@ response_t serve_file(request_t req) {
}
// TODO actually respect requests
-// TODO handle gzip
// TODO list dirs
response_t make_response (request_t req) {
@@ -125,6 +125,12 @@ response_t make_response (request_t req) {
log_info("Got request %s %s", method_name(req.method), req.path);
+ char* mapped_path = hashmap_get(&path_redirs, req.path);
+ if (mapped_path != NULL) {
+ log_debug("test");
+ strcpy(req.path, mapped_path);
+ }
+
if (!shitvec_check(&paths, req.path, (sv_cmp_t)strcmp)) {
return serve_error(NotFound);
}
@@ -200,6 +206,10 @@ int main() {
}
paths = shitvec_new(MAX_PATH_LEN);
+ path_redirs = hashmap_new(MAX_PATH_LEN, MAX_PATH_LEN);
+ path_redirs.vark = true;
+ hashmap_set(&path_redirs, "/", "/index.html");
+
list_files_sv(&paths, "public");
// TODO handle special non-file paths
diff --git a/http/request.c b/http/request.c
@@ -29,6 +29,7 @@ int cmp_r_mimetype(const void* a, const void* b) {
if (aa->q > bb->q) return -1;
else if (aa->q == bb->q) return 0;
else if (aa->q < bb->q) return 1;
+ return 0; // unreachable?
}
// TODO handle parse errors
diff --git a/http/response.c b/http/response.c
@@ -46,12 +46,8 @@ const char* exts[] = {"html", "css", "js", "png", "gif", "jpeg", "svg", "ttf", "
const char* mtypes[] = {"text/html", "text/css", "text/javascript", "image/png",
"image/gif", "image/jpeg", "image/svg+xml", "font/ttf", "font/woff", "font/woff2"};
-// TODO find cleaner way of doing this
-// two lists and loop
const char* ext_to_mtype(char* ext) {
- if (ext == NULL) {
- return"text/plain";
- }
+ if (ext == NULL) return"text/plain";
for (size_t i = 0; exts[i] != NULL; i++) {
if (strcmp(ext, exts[i]) == 0) return mtypes[i];
diff --git a/utils/hashmap.c b/utils/hashmap.c
@@ -50,7 +50,6 @@ hashmap_t hashmap_new(size_t ksize, size_t vsize) {
}
int hashmap_kcmp(hashmap_t* h, void* a, void* b) {
- /* log_debug("%s (%p) vs. %s", a, a, b); */
if (h->vark) return strcmp(a, b);
return memcmp(a, b, h->k_sz);
}
@@ -136,6 +135,7 @@ void* hashmap_get(hashmap_t* h, void* k) {
looped_once = 1;
}
}
+ return 0x0; // unreachable?
}
int hashmap_del(hashmap_t* h, void* k) {
diff --git a/utils/llist.c b/utils/llist.c
@@ -1,4 +0,0 @@
-
-struct node {
- struct node* next;
-};
diff --git a/utils/log.c b/utils/log.c
@@ -1,7 +1,4 @@
-// TODO: fix wackiness with time functions
-// TODO: allow for writing to files
-
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
@@ -27,6 +24,8 @@ const char* lvl_colors[] =
size_t log_min_level = 0;
+FILE* log_file = NULL;
+
void log_msg(enum log_level lvl, char* lvl_name, char* fmt, ...) {
if (lvl < log_min_level) return;
@@ -40,17 +39,28 @@ void log_msg(enum log_level lvl, char* lvl_name, char* fmt, ...) {
struct tm* local = localtime(&now);
const char* lvl_color = lvl_colors[lvl];
+
+ if (log_file == NULL) {
+ printf(ANSI_GREY "%02d/%02d/%04d %02d:%02d:%02d %s%s" ANSI_RESET ": %s\n",
+ local->tm_mon+1, local->tm_mday, 1900+local->tm_year, local->tm_hour,
+ local->tm_min, local->tm_sec, lvl_color, lvl_name, msg);
+ } else {
+ fprintf(log_file, "%02d/%02d/%04d %02d:%02d:%02d %s: %s\n",
+ local->tm_mon+1, local->tm_mday, 1900+local->tm_year, local->tm_hour,
+ local->tm_min, local->tm_sec, lvl_name, msg);
+ }
- printf(ANSI_GREY "%02d/%02d/%04d %02d:%02d:%02d %s%s" ANSI_RESET ": %s\n",
- local->tm_mon+1, local->tm_mday, 1900+local->tm_year,
- local->tm_hour+1, local->tm_min+1, local->tm_sec,
- lvl_color, lvl_name, msg);
+ va_end(args);
}
void log_set_min_lvl(enum log_level lvl) {
log_min_level = lvl;
}
+void log_set_log_file(char* path) {
+ log_file = fopen(path, "w");
+}
+
void die(char* p) {
log_fatal("(%s) %s", p, strerror(errno));
exit(1);