You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.1 KiB

#pragma once
#include <string.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
#else
#include <stdlib.h>
#endif
#include "log.h"
int hasTriedIndex;
int getMime(char** mime, char* filename) {
char* dot = strrchr(filename, '.');
if (!dot || dot == filename) {
if (!hasTriedIndex) {
hasTriedIndex++;
strcat(filename, "/index.html");
info("trying to upgrade directory to index.html");
return getMime(mime, filename);
} else {
warn("file extension does not exist or is the same as request's path.\n");
hasTriedIndex = 1;
*mime = "text/plain";
return 0;
}
} else {
if (!strcmp(dot, ".html")) {
*mime = "text/html";
} else if (!strcmp(dot, ".css")) {
*mime = "text/css";
} else if (!strcmp(dot, ".js")) {
*mime = "text/javascript";
} else if (!strcmp(dot, ".woff2")) {
*mime = "font/woff2";
return 1;
} else if (!strcmp(dot, ".ico")) {
*mime = "image/vnd.microsoft.icon";
return 1;
} else {
warn("mimetype for %s not found\n", filename);
*mime = "text/plain";
}
if (hasTriedIndex == 1) {
hasTriedIndex = 2;
} else {
hasTriedIndex = 0;
}
}
return 0;
}