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.
24 init_mime(struct mime *mime)
29 mime->t = calloc(mime->cap, sizeof(struct etm));
31 fatal("calloc: %s", strerror(errno));
34 /* register mime for the given extension */
36 add_mime(struct mime *mime, const char *mt, const char *ext)
38 if (mime->len == mime->cap) {
40 mime->t = realloc(mime->t, mime->cap * sizeof(struct etm));
42 fatal("realloc: %s", strerror(errno));
45 mime->t[mime->len].mime = mt;
46 mime->t[mime->len].ext = ext;
50 /* load a default set of common mime-extension associations */
52 load_default_mime(struct mime *mime)
54 struct etm *i, m[] = {
55 {"application/pdf", "pdf"},
57 {"image/jpeg", "jpg"},
58 {"image/jpeg", "jpeg"},
60 {"image/svg+xml", "svg"},
61 {"text/gemini", "gemini"},
62 {"text/gemini", "gmi"},
63 {"text/markdown", "markdown"},
64 {"text/markdown", "md"},
65 {"text/plain", "txt"},
70 for (i = m; i->mime != NULL; ++i)
71 add_mime(mime, i->mime, i->ext);
75 path_ext(const char *path)
79 end = path + strlen(path)-1;
80 for (; end != path; --end) {
91 mime(struct vhost *host, const char *path)
93 const char *def, *ext;
96 if ((def = host->default_mime) == NULL)
97 def = "application/octet-stream";
99 if ((ext = path_ext(path)) == NULL)
102 for (t = conf.mime.t; t->mime != NULL; ++t)
103 if (!strcmp(ext, t->ext))