summaryrefslogtreecommitdiff
path: root/xs_mime.h
diff options
context:
space:
mode:
Diffstat (limited to 'xs_mime.h')
-rw-r--r--xs_mime.h90
1 files changed, 50 insertions, 40 deletions
diff --git a/xs_mime.h b/xs_mime.h
index ef7affe..2c8eaa9 100644
--- a/xs_mime.h
+++ b/xs_mime.h
@@ -6,57 +6,67 @@
6 6
7const char *xs_mime_by_ext(const char *file); 7const char *xs_mime_by_ext(const char *file);
8 8
9extern const char *xs_mime_types[];
10
9#ifdef XS_IMPLEMENTATION 11#ifdef XS_IMPLEMENTATION
10 12
11/* intentionally brain-dead simple */ 13/* intentionally brain-dead simple */
12struct _mime_info { 14/* CAUTION: sorted */
13 const char *type; 15
14 const char *ext; 16const char *xs_mime_types[] = {
15} mime_info[] = { 17 "3gp", "video/3gpp",
16 { "application/json", ".json" }, 18 "aac", "audio/aac",
17 { "image/gif", ".gif" }, 19 "css", "text/css",
18 { "image/jpeg", ".jpeg" }, 20 "flac", "audio/flac",
19 { "image/jpeg", ".jpg" }, 21 "flv", "video/flv",
20 { "image/png", ".png" }, 22 "gif", "image/gif",
21 { "image/webp", ".webp" }, 23 "gmi", "text/gemini",
22 { "video/mp4", ".mp4" }, 24 "html", "text/html",
23 { "video/mp4", ".mpg4" }, 25 "jpeg", "image/jpeg",
24 { "video/mp4", ".m4v" }, 26 "jpg", "image/jpeg",
25 { "video/webm", ".webm" }, 27 "json", "application/json",
26 { "video/quicktime", ".mov" }, 28 "m4a", "audio/aac",
27 { "video/3gpp", ".3gp" }, 29 "m4v", "video/mp4",
28 { "video/ogg", ".ogv" }, 30 "md", "text/markdown",
29 { "video/flv", ".flv" }, 31 "mov", "video/quicktime",
30 { "audio/mp3", ".mp3" }, 32 "mp3", "audio/mp3",
31 { "audio/ogg", ".ogg" }, 33 "mp4", "video/mp4",
32 { "audio/ogg", ".oga" }, 34 "mpg4", "video/mp4",
33 { "audio/ogg", ".opus" }, 35 "oga", "audio/ogg",
34 { "audio/flac", ".flac" }, 36 "ogg", "audio/ogg",
35 { "audio/wav", ".wav" }, 37 "ogv", "video/ogg",
36 { "audio/wma", ".wma" }, 38 "opus", "audio/ogg",
37 { "audio/aac", ".aac" }, 39 "png", "image/png",
38 { "audio/aac", ".m4a" }, 40 "txt", "text/plain",
39 { "text/css", ".css" }, 41 "wav", "audio/wav",
40 { "text/html", ".html" }, 42 "webm", "video/webm",
41 { "text/plain", ".txt" }, 43 "webp", "image/webp",
42 { "text/xml", ".xml" }, 44 "wma", "audio/wma",
43 { "text/markdown", ".md" }, 45 "xml", "text/xml",
44 { "text/gemini", ".gmi" }, 46 NULL, NULL,
45 { NULL, NULL }
46}; 47};
47 48
48 49
49const char *xs_mime_by_ext(const char *file) 50const char *xs_mime_by_ext(const char *file)
50/* returns the MIME type by file extension */ 51/* returns the MIME type by file extension */
51{ 52{
52 struct _mime_info *mi = mime_info; 53 const char *ext = strrchr(file, '.');
53 xs *lfile = xs_tolower_i(xs_dup(file)); 54
55 if (ext) {
56 const char **p = xs_mime_types;
57 xs *uext = xs_tolower_i(xs_dup(ext + 1));
58
59 while (**p) {
60 int c;
54 61
55 while (mi->type != NULL) { 62 if ((c = strcmp(*p, uext)) == 0)
56 if (xs_endswith(lfile, mi->ext)) 63 return p[1];
57 return mi->type; 64 else
65 if (c > 0)
66 break;
58 67
59 mi++; 68 p += 2;
69 }
60 } 70 }
61 71
62 return "application/octet-stream"; 72 return "application/octet-stream";