generate mimetypes with script

master
Drake 2 years ago
parent c282094e9e
commit 4047aba890

Binary file not shown.

File diff suppressed because it is too large Load Diff

@ -0,0 +1,71 @@
CUSTOM_TYPES = {
"example": "text/example"
}
import mimetypes
mimetypes.init()
types = mimetypes.types_map
for k,v in CUSTOM_TYPES.items():
types[k] = v
genString = "if (0) {} "
for k,v in types.items():
if "text" in v or "application/json" in v or "application/x-sh" in v or "application/xhtml+xml" in v or "application/xml" in v or "application/javascript" in v:
genString += f"""
else if (!strcmp(dot, "{k}")) {{
*mime = "{v}";
}}"""
else:
genString += f"""
else if (!strcmp(dot, "{k}")) {{
*mime = "{v}";
return 1;
}}"""
fullText = f"""#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 {{ {genString} else {{
warn("mimetype for %s not found\\n", filename);
*mime = "text/plain";
}}
if (hasTriedIndex == 1) {{
hasTriedIndex = 2;
}} else {{
hasTriedIndex = 0;
}}
}}
return 0;
}}
"""
with open("lib/mime.h", "w") as f:
f.write(fullText)
Loading…
Cancel
Save