2 a555e0d6 2022-07-04 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 0fbe79b3 2021-01-18 op * Permission to use, copy, modify, and distribute this software for any
5 0fbe79b3 2021-01-18 op * purpose with or without fee is hereby granted, provided that the above
6 0fbe79b3 2021-01-18 op * copyright notice and this permission notice appear in all copies.
8 0fbe79b3 2021-01-18 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0fbe79b3 2021-01-18 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0fbe79b3 2021-01-18 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0fbe79b3 2021-01-18 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0fbe79b3 2021-01-18 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0fbe79b3 2021-01-18 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0fbe79b3 2021-01-18 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 0fbe79b3 2021-01-18 op #include <errno.h>
20 0fbe79b3 2021-01-18 op #include <stdlib.h>
21 0fbe79b3 2021-01-18 op #include <string.h>
24 b2a6b613 2021-01-21 op init_mime(struct mime *mime)
26 b2a6b613 2021-01-21 op mime->len = 0;
27 b2a6b613 2021-01-21 op mime->cap = 16;
29 b2a6b613 2021-01-21 op mime->t = calloc(mime->cap, sizeof(struct etm));
30 b2a6b613 2021-01-21 op if (mime->t == NULL)
31 0fbe79b3 2021-01-18 op fatal("calloc: %s", strerror(errno));
34 0fbe79b3 2021-01-18 op /* register mime for the given extension */
36 b2a6b613 2021-01-21 op add_mime(struct mime *mime, const char *mt, const char *ext)
38 d8d170aa 2022-04-08 op struct etm *t;
39 d8d170aa 2022-04-08 op size_t newcap;
41 b2a6b613 2021-01-21 op if (mime->len == mime->cap) {
42 d8d170aa 2022-04-08 op newcap = mime->cap * 1.5;
43 d8d170aa 2022-04-08 op t = recallocarray(mime->t, mime->cap, newcap,
44 95210bb3 2021-01-21 op sizeof(struct etm));
45 d8d170aa 2022-04-08 op if (t == NULL)
48 d8d170aa 2022-04-08 op mime->cap = newcap;
51 aa9543b9 2022-09-10 op t = &mime->t[mime->len];
52 aa9543b9 2022-09-10 op if (strlcpy(t->mime, mt, sizeof(t->mime)) >= sizeof(t->mime))
54 aa9543b9 2022-09-10 op if (strlcpy(t->ext, ext, sizeof(t->ext)) >= sizeof(t->ext))
60 0fbe79b3 2021-01-18 op /* load a default set of common mime-extension associations */
62 b2a6b613 2021-01-21 op load_default_mime(struct mime *mime)
64 d8d170aa 2022-04-08 op const struct mapping {
65 d8d170aa 2022-04-08 op const char *mime;
66 d8d170aa 2022-04-08 op const char *ext;
68 0fbe79b3 2021-01-18 op {"application/pdf", "pdf"},
69 0fbe79b3 2021-01-18 op {"image/gif", "gif"},
70 0fbe79b3 2021-01-18 op {"image/jpeg", "jpg"},
71 0fbe79b3 2021-01-18 op {"image/jpeg", "jpeg"},
72 0fbe79b3 2021-01-18 op {"image/png", "png"},
73 0fbe79b3 2021-01-18 op {"image/svg+xml", "svg"},
74 0fbe79b3 2021-01-18 op {"text/gemini", "gemini"},
75 0fbe79b3 2021-01-18 op {"text/gemini", "gmi"},
76 0fbe79b3 2021-01-18 op {"text/markdown", "markdown"},
77 0fbe79b3 2021-01-18 op {"text/markdown", "md"},
78 0fbe79b3 2021-01-18 op {"text/plain", "txt"},
79 0bfdcb17 2021-04-22 op {"text/x-patch", "diff"},
80 4d820b6c 2021-04-21 op {"text/x-patch", "patch"},
81 0fbe79b3 2021-01-18 op {"text/xml", "xml"},
85 cd5826b8 2022-09-10 op /* don't load the default if `types' was used. */
86 cd5826b8 2022-09-10 op if (mime->len != 0)
89 d8d170aa 2022-04-08 op for (i = m; i->mime != NULL; ++i) {
90 d8d170aa 2022-04-08 op if (add_mime(mime, i->mime, i->ext) == -1)
97 0fbe79b3 2021-01-18 op static const char *
98 0fbe79b3 2021-01-18 op path_ext(const char *path)
100 0fbe79b3 2021-01-18 op const char *end;
102 0fbe79b3 2021-01-18 op end = path + strlen(path)-1;
103 0fbe79b3 2021-01-18 op for (; end != path; --end) {
104 0fbe79b3 2021-01-18 op if (*end == '.')
105 0fbe79b3 2021-01-18 op return end+1;
106 0fbe79b3 2021-01-18 op if (*end == '/')
114 18bd8391 2022-04-08 op mime_comp(const void *a, const void *b)
116 18bd8391 2022-04-08 op const struct etm *x = a, *y = b;
118 18bd8391 2022-04-08 op return strcmp(x->ext, y->ext);
122 18bd8391 2022-04-08 op sort_mime(struct mime *m)
124 18bd8391 2022-04-08 op qsort(m->t, m->len, sizeof(*m->t), mime_comp);
128 18bd8391 2022-04-08 op mime_find(const void *a, const void *b)
130 18bd8391 2022-04-08 op const char *ext = a;
131 18bd8391 2022-04-08 op const struct etm *x = b;
133 18bd8391 2022-04-08 op return strcmp(ext, x->ext);
137 6119e13e 2021-01-19 op mime(struct vhost *host, const char *path)
139 6119e13e 2021-01-19 op const char *def, *ext;
140 0fbe79b3 2021-01-18 op struct etm *t;
142 c8b74339 2021-01-24 op def = vhost_default_mime(host, path);
144 0fbe79b3 2021-01-18 op if ((ext = path_ext(path)) == NULL)
147 18bd8391 2022-04-08 op t = bsearch(ext, conf.mime.t, conf.mime.len, sizeof(*conf.mime.t),
149 18bd8391 2022-04-08 op if (t != NULL)
150 18bd8391 2022-04-08 op return t->mime;
151 8fb0df4d 2022-04-08 op if (!strcmp(ext, "gmi") || !strcmp(ext, "gemini"))
152 8fb0df4d 2022-04-08 op return "text/gemini";
157 d8d170aa 2022-04-08 op free_mime(struct mime *m)