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 #ifdef NO_GC #include #define malloc(n) GC_malloc(n) #else #include #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)