Blob


1 /*
2 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "gmid.h"
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
23 void
24 init_mime(struct mime *mime)
25 {
26 mime->len = 0;
27 mime->cap = 16;
29 mime->t = calloc(mime->cap, sizeof(struct etm));
30 if (mime->t == NULL)
31 fatal("calloc: %s", strerror(errno));
32 }
34 /* register mime for the given extension */
35 int
36 add_mime(struct mime *mime, const char *mt, const char *ext)
37 {
38 struct etm *t;
39 size_t newcap;
41 if (mime->len == mime->cap) {
42 newcap = mime->cap * 1.5;
43 t = recallocarray(mime->t, mime->cap, newcap,
44 sizeof(struct etm));
45 if (t == NULL)
46 return -1;
47 mime->t = t;
48 mime->cap = newcap;
49 }
51 t = &mime->t[mime->len];
52 if (strlcpy(t->mime, mt, sizeof(t->mime)) >= sizeof(t->mime))
53 return -1;
54 if (strlcpy(t->ext, ext, sizeof(t->ext)) >= sizeof(t->ext))
55 return -1;
56 mime->len++;
57 return 0;
58 }
60 /* load a default set of common mime-extension associations */
61 int
62 load_default_mime(struct mime *mime)
63 {
64 const struct mapping {
65 const char *mime;
66 const char *ext;
67 } m[] = {
68 {"application/pdf", "pdf"},
69 {"image/gif", "gif"},
70 {"image/jpeg", "jpg"},
71 {"image/jpeg", "jpeg"},
72 {"image/png", "png"},
73 {"image/svg+xml", "svg"},
74 {"text/gemini", "gemini"},
75 {"text/gemini", "gmi"},
76 {"text/markdown", "markdown"},
77 {"text/markdown", "md"},
78 {"text/plain", "txt"},
79 {"text/x-patch", "diff"},
80 {"text/x-patch", "patch"},
81 {"text/xml", "xml"},
82 {NULL, NULL}
83 }, *i;
85 /* don't load the default if `types' was used. */
86 if (mime->len != 0)
87 return 0;
89 for (i = m; i->mime != NULL; ++i) {
90 if (add_mime(mime, i->mime, i->ext) == -1)
91 return -1;
92 }
94 return 0;
95 }
97 static const char *
98 path_ext(const char *path)
99 {
100 const char *end;
102 end = path + strlen(path)-1;
103 for (; end != path; --end) {
104 if (*end == '.')
105 return end+1;
106 if (*end == '/')
107 break;
110 return NULL;
113 static int
114 mime_comp(const void *a, const void *b)
116 const struct etm *x = a, *y = b;
118 return strcmp(x->ext, y->ext);
121 void
122 sort_mime(struct mime *m)
124 qsort(m->t, m->len, sizeof(*m->t), mime_comp);
127 static int
128 mime_find(const void *a, const void *b)
130 const char *ext = a;
131 const struct etm *x = b;
133 return strcmp(ext, x->ext);
136 const char *
137 mime(struct vhost *host, const char *path)
139 const char *def, *ext;
140 struct etm *t;
142 def = vhost_default_mime(host, path);
144 if ((ext = path_ext(path)) == NULL)
145 return def;
147 t = bsearch(ext, conf.mime.t, conf.mime.len, sizeof(*conf.mime.t),
148 mime_find);
149 if (t != NULL)
150 return t->mime;
151 if (!strcmp(ext, "gmi") || !strcmp(ext, "gemini"))
152 return "text/gemini";
153 return def;
156 void
157 free_mime(struct mime *m)
159 free(m->t);