From 7a5a4bbc1742cbd2492f6e9fcdb5c4aa65396371 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 24 Nov 2019 00:19:52 +0100 Subject: static: Some filename based preset mimes, because 'file' doesn't have those --- plugins/static/mime.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/plugins/static/mime.c b/plugins/static/mime.c index 6be5078..e00588a 100644 --- a/plugins/static/mime.c +++ b/plugins/static/mime.c @@ -178,7 +178,31 @@ static char* mime_detect_perform(const char *path) { return NULL; } +static const char* mime_detect_filename(const char *path) { + size_t len = strlen(path); + if (len == 0) return NULL; + + size_t dotidx = len - 1; + while (dotidx > 0 && path[dotidx] != '.' && path[dotidx] != '/') dotidx--; + if (path[dotidx] == '/') return NULL; + + const char *extension = path + dotidx + 1; + + if (strcmp(extension, "html") == 0) return "text/html"; + if (strcmp(extension, "htm") == 0) return "text/html"; + if (strcmp(extension, "txt") == 0) return "text/plain"; + if (strcmp(extension, "js") == 0) return "text/javascript"; + if (strcmp(extension, "css") == 0) return "text/css"; + + return NULL; +} + const char* mime_detect(const char *path) { + { + const char *typ_from_name = mime_detect_filename(path); + if (typ_from_name) return typ_from_name; + } + struct timespec now_mtime = get_file_mtime(path); if (now_mtime.tv_sec < 0) return NULL; -- cgit v1.2.3-70-g09d2