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.
29 conf.mimes.t = calloc(conf.mimes.cap, sizeof(struct etm));
30 if (conf.mimes.t == NULL)
31 fatal("calloc: %s", strerror(errno));
34 /* register mime for the given extension */
36 add_mime(const char *mime, const char *ext)
38 if (conf.mimes.len == conf.mimes.cap) {
39 conf.mimes.cap *= 1.5;
40 conf.mimes.t = realloc(conf.mimes.t,
41 conf.mimes.cap * sizeof(struct etm));
42 if (conf.mimes.t == NULL)
43 fatal("realloc: %s", strerror(errno));
46 conf.mimes.t[conf.mimes.len].mime = mime;
47 conf.mimes.t[conf.mimes.len].ext = ext;
51 /* load a default set of common mime-extension associations */
55 struct etm *i, m[] = {
56 {"application/pdf", "pdf"},
58 {"image/jpeg", "jpg"},
59 {"image/jpeg", "jpeg"},
61 {"image/svg+xml", "svg"},
62 {"text/gemini", "gemini"},
63 {"text/gemini", "gmi"},
64 {"text/markdown", "markdown"},
65 {"text/markdown", "md"},
66 {"text/plain", "txt"},
71 for (i = m; i->mime != NULL; ++i)
72 add_mime(i->mime, i->ext);
76 path_ext(const char *path)
80 end = path + strlen(path)-1;
81 for (; end != path; --end) {
92 mime(struct vhost *host, const char *path)
94 const char *def, *ext;
97 if ((def = host->default_mime) == NULL)
98 def = "application/octet-stream";
100 if ((ext = path_ext(path)) == NULL)
103 for (t = conf.mimes.t; t->mime != NULL; ++t)
104 if (!strcmp(ext, t->ext))