factor out mimetypes, probably something else
parent
cda88d2f76
commit
4d323d55cd
Binary file not shown.
@ -0,0 +1,53 @@
|
|||||||
|
#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;
|
||||||
|
}
|
Loading…
Reference in new issue