Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "gmid.h"
23 struct etm { /* extension to mime */
24 const char *mime;
25 const char *ext;
26 };
28 struct mimes {
29 char *def;
30 struct etm *t;
31 size_t len;
32 size_t cap;
33 };
35 struct mimes mimes;
37 void
38 init_mime(void)
39 {
40 mimes.len = 0;
41 mimes.cap = 2;
43 if ((mimes.t = calloc(mimes.cap, sizeof(struct etm))) == NULL)
44 fatal("calloc: %s", strerror(errno));
46 mimes.def = strdup("application/octet-stream");
47 if (mimes.def == NULL)
48 fatal("strdup: %s", strerror(errno));
50 }
52 void
53 set_default_mime(const char *m)
54 {
55 free(mimes.def);
56 if ((mimes.def = strdup(m)) == NULL)
57 fatal("strdup: %s", strerror(errno));
58 }
60 /* register mime for the given extension */
61 void
62 add_mime(const char *mime, const char *ext)
63 {
64 if (mimes.len == mimes.cap) {
65 mimes.cap *= 1.5;
66 mimes.t = realloc(mimes.t, mimes.cap * sizeof(struct etm));
67 if (mimes.t == NULL)
68 fatal("realloc: %s", strerror(errno));
69 }
71 mimes.t[mimes.len].mime = mime;
72 mimes.t[mimes.len].ext = ext;
73 mimes.len++;
74 }
76 /* load a default set of common mime-extension associations */
77 void
78 load_default_mime()
79 {
80 struct etm *i, m[] = {
81 {"application/pdf", "pdf"},
82 {"image/gif", "gif"},
83 {"image/jpeg", "jpg"},
84 {"image/jpeg", "jpeg"},
85 {"image/png", "png"},
86 {"image/svg+xml", "svg"},
87 {"text/gemini", "gemini"},
88 {"text/gemini", "gmi"},
89 {"text/markdown", "markdown"},
90 {"text/markdown", "md"},
91 {"text/plain", "txt"},
92 {"text/xml", "xml"},
93 {NULL, NULL}
94 };
96 for (i = m; i->mime != NULL; ++i)
97 add_mime(i->mime, i->ext);
98 }
100 static const char *
101 path_ext(const char *path)
103 const char *end;
105 end = path + strlen(path)-1;
106 for (; end != path; --end) {
107 if (*end == '.')
108 return end+1;
109 if (*end == '/')
110 break;
113 return NULL;
116 const char *
117 mime(const char *path)
119 const char *ext;
120 struct etm *t;
122 if ((ext = path_ext(path)) == NULL)
123 return mimes.def;
125 for (t = mimes.t; t->mime != NULL; ++t)
126 if (!strcmp(ext, t->ext))
127 return t->mime;
129 return mimes.def;