commit 053c1e1
uint
·
2026-05-16 18:12:04 +0000 UTC
parent 1e3829e
collapse long if chain into MIME table
1 files changed,
+29,
-42
+29,
-42
1@@ -247,53 +247,41 @@ out:
2 */
3 static const char* mime_from_path(const char* path)
4 {
5+ static const struct {
6+ const char* ext;
7+ const char* type;
8+ } types[] = {
9+ /* audio */
10+ { "mp3", "audio/mpeg" }, { "m4a", "audio/mp4" },
11+ { "aac", "audio/aac" }, { "flac", "audio/flac" },
12+ { "wav", "audio/wav" }, { "ogg", "audio/ogg" },
13+ { "opus", "audio/opus" },
14+
15+ /* video */
16+ { "mp4", "video/mp4" }, { "mkv", "video/x-matroska" },
17+ { "webm", "video/webm" }, { "mov", "video/quicktime" },
18+
19+ /* images */
20+ { "jpg", "image/jpeg" }, { "jpeg", "image/jpeg" },
21+ { "png", "image/png" }, { "gif", "image/gif" },
22+ { "webp", "image/webp" },
23+
24+ /* subtitles */
25+ { "vtt", "text/vtt" }, { "srt", "application/x-subrip" },
26+
27+ /* misc */
28+ { "pdf", "application/pdf" },
29+ };
30 const char* dot = strrchr(path, '.');
31+
32 if (!dot || dot[1] == '\0')
33 return "application/octet-stream";
34
35 dot++;
36
37- /* audio */
38- if (strcasecmp(dot, "mp3") == 0)
39- return "audio/mpeg";
40- if (strcasecmp(dot, "m4a") == 0)
41- return "audio/mp4";
42- if (strcasecmp(dot, "aac") == 0)
43- return "audio/aac";
44- if (strcasecmp(dot, "flac") == 0)
45- return "audio/flac";
46- if (strcasecmp(dot, "wav") == 0)
47- return "audio/wav";
48- if (strcasecmp(dot, "ogg") == 0)
49- return "audio/ogg";
50- if (strcasecmp(dot, "opus") == 0)
51- return "audio/opus";
52-
53- /* video */
54- if (strcasecmp(dot, "mp4") == 0)
55- return "video/mp4";
56- if (strcasecmp(dot, "mkv") == 0)
57- return "video/x-matroska";
58- if (strcasecmp(dot, "webm") == 0)
59- return "video/webm";
60- if (strcasecmp(dot, "mov") == 0)
61- return "video/quicktime";
62-
63- /* images */
64- if (strcasecmp(dot, "jpg") == 0)
65- return "image/jpeg";
66- if (strcasecmp(dot, "jpeg") == 0)
67- return "image/jpeg";
68- if (strcasecmp(dot, "png") == 0)
69- return "image/png";
70- if (strcasecmp(dot, "gif") == 0)
71- return "image/gif";
72- if (strcasecmp(dot, "webp") == 0)
73- return "image/webp";
74-
75- /* misc */
76- if (strcasecmp(dot, "pdf") == 0)
77- return "application/pdf";
78+ for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); i++)
79+ if (strcasecmp(dot, types[i].ext) == 0)
80+ return types[i].type;
81
82 return "application/octet-stream";
83 }
84@@ -1040,4 +1028,3 @@ int http_handle(int c)
85
86 return route_dispatch(c, hdr, path, u, head_only);
87 }
88-