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 char *mimetype, *extension;
39 struct etm *t;
40 size_t newcap;
42 if (mime->len == mime->cap) {
43 newcap = mime->cap * 1.5;
44 t = recallocarray(mime->t, mime->cap, newcap,
45 sizeof(struct etm));
46 if (t == NULL)
47 return -1;
48 mime->t = t;
49 mime->cap = newcap;
50 }
52 if ((mimetype = strdup(mt)) == NULL)
53 return -1;
54 if ((extension = strdup(ext)) == NULL) {
55 free(mimetype);
56 return -1;
57 }
59 mime->t[mime->len].mime = mimetype;
60 mime->t[mime->len].ext = extension;
61 mime->len++;
62 return 0;
63 }
65 /* load a default set of common mime-extension associations */
66 int
67 load_default_mime(struct mime *mime)
68 {
69 const struct mapping {
70 const char *mime;
71 const char *ext;
72 } m[] = {
73 {"application/pdf", "pdf"},
74 {"image/gif", "gif"},
75 {"image/jpeg", "jpg"},
76 {"image/jpeg", "jpeg"},
77 {"image/png", "png"},
78 {"image/svg+xml", "svg"},
79 {"text/gemini", "gemini"},
80 {"text/gemini", "gmi"},
81 {"text/markdown", "markdown"},
82 {"text/markdown", "md"},
83 {"text/plain", "txt"},
84 {"text/x-patch", "diff"},
85 {"text/x-patch", "patch"},
86 {"text/xml", "xml"},
87 {NULL, NULL}
88 }, *i;
90 for (i = m; i->mime != NULL; ++i) {
91 if (add_mime(mime, i->mime, i->ext) == -1)
92 return -1;
93 }
95 return 0;
96 }
98 static const char *
99 path_ext(const char *path)
101 const char *end;
103 end = path + strlen(path)-1;
104 for (; end != path; --end) {
105 if (*end == '.')
106 return end+1;
107 if (*end == '/')
108 break;
111 return NULL;
114 static int
115 mime_comp(const void *a, const void *b)
117 const struct etm *x = a, *y = b;
119 return strcmp(x->ext, y->ext);
122 void
123 sort_mime(struct mime *m)
125 qsort(m->t, m->len, sizeof(*m->t), mime_comp);
128 static int
129 mime_find(const void *a, const void *b)
131 const char *ext = a;
132 const struct etm *x = b;
134 return strcmp(ext, x->ext);
137 const char *
138 mime(struct vhost *host, const char *path)
140 const char *def, *ext;
141 struct etm *t;
143 def = vhost_default_mime(host, path);
145 if ((ext = path_ext(path)) == NULL)
146 return def;
148 t = bsearch(ext, conf.mime.t, conf.mime.len, sizeof(*conf.mime.t),
149 mime_find);
150 if (t != NULL)
151 return t->mime;
152 if (!strcmp(ext, "gmi") || !strcmp(ext, "gemini"))
153 return "text/gemini";
154 return def;
157 void
158 free_mime(struct mime *m)
160 struct etm *t;
162 for (t = m->t; t->mime != NULL; ++t) {
163 free(t->mime);
164 free(t->ext);
167 free(m->t);