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 */
42 if ((mimes.t = calloc(mimes.cap, sizeof(struct etm))) == NULL)
43 fatal("calloc: %s", strerror(errno));
46 /* register mime for the given extension */
48 add_mime(const char *mime, const char *ext)
50 if (mimes.len == mimes.cap) {
52 mimes.t = realloc(mimes.t, mimes.cap * sizeof(struct etm));
54 fatal("realloc: %s", strerror(errno));
57 mimes.t[mimes.len].mime = mime;
58 mimes.t[mimes.len].ext = ext;
62 /* load a default set of common mime-extension associations */
66 struct etm *i, m[] = {
67 {"application/pdf", "pdf"},
69 {"image/jpeg", "jpg"},
70 {"image/jpeg", "jpeg"},
72 {"image/svg+xml", "svg"},
73 {"text/gemini", "gemini"},
74 {"text/gemini", "gmi"},
75 {"text/markdown", "markdown"},
76 {"text/markdown", "md"},
77 {"text/plain", "txt"},
82 for (i = m; i->mime != NULL; ++i)
83 add_mime(i->mime, i->ext);
87 path_ext(const char *path)
91 end = path + strlen(path)-1;
92 for (; end != path; --end) {
103 mime(const char *path)
105 const char *ext, *def = "application/octet-stream";
108 if ((ext = path_ext(path)) == NULL)
111 for (t = mimes.t; t->mime != NULL; ++t)
112 if (!strcmp(ext, t->ext))