2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
23 struct etm { /* extension to mime */
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));
53 set_default_mime(const char *m)
56 if ((mimes.def = strdup(m)) == NULL)
57 fatal("strdup: %s", strerror(errno));
60 /* register mime for the given extension */
62 add_mime(const char *mime, const char *ext)
64 if (mimes.len == mimes.cap) {
66 mimes.t = realloc(mimes.t, mimes.cap * sizeof(struct etm));
68 fatal("realloc: %s", strerror(errno));
71 mimes.t[mimes.len].mime = mime;
72 mimes.t[mimes.len].ext = ext;
76 /* load a default set of common mime-extension associations */
80 struct etm *i, m[] = {
81 {"application/pdf", "pdf"},
83 {"image/jpeg", "jpg"},
84 {"image/jpeg", "jpeg"},
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"},
96 for (i = m; i->mime != NULL; ++i)
97 add_mime(i->mime, i->ext);
101 path_ext(const char *path)
105 end = path + strlen(path)-1;
106 for (; end != path; --end) {
117 mime(const char *path)
122 if ((ext = path_ext(path)) == NULL)
125 for (t = mimes.t; t->mime != NULL; ++t)
126 if (!strcmp(ext, t->ext))