Blob


1 /*
2 * Copyright (c) 2021, 2022, 2023 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 #include "log.h"
25 void
26 init_mime(struct mime *mime)
27 {
28 mime->len = 0;
29 mime->cap = 16;
31 mime->t = calloc(mime->cap, sizeof(struct etm));
32 if (mime->t == NULL)
33 fatal("calloc");
34 }
36 /* register mime for the given extension */
37 int
38 add_mime(struct mime *mime, const char *mt, const char *ext)
39 {
40 struct etm *t;
41 size_t newcap;
43 if (mime->len == mime->cap) {
44 newcap = mime->cap * 1.5;
45 t = recallocarray(mime->t, mime->cap, newcap,
46 sizeof(struct etm));
47 if (t == NULL)
48 return -1;
49 mime->t = t;
50 mime->cap = newcap;
51 }
53 t = &mime->t[mime->len];
54 if (strlcpy(t->mime, mt, sizeof(t->mime)) >= sizeof(t->mime))
55 return -1;
56 if (strlcpy(t->ext, ext, sizeof(t->ext)) >= sizeof(t->ext))
57 return -1;
58 mime->len++;
59 return 0;
60 }
62 /* load a default set of common mime-extension associations */
63 int
64 load_default_mime(struct mime *mime)
65 {
66 const struct mapping {
67 const char *mime;
68 const char *ext;
69 } m[] = {
70 {"application/pdf", "pdf"},
71 {"image/gif", "gif"},
72 {"image/jpeg", "jpg"},
73 {"image/jpeg", "jpeg"},
74 {"image/png", "png"},
75 {"image/svg+xml", "svg"},
76 {"text/gemini", "gemini"},
77 {"text/gemini", "gmi"},
78 {"text/markdown", "markdown"},
79 {"text/markdown", "md"},
80 {"text/plain", "txt"},
81 {"text/x-patch", "diff"},
82 {"text/x-patch", "patch"},
83 {"text/xml", "xml"},
84 {NULL, NULL}
85 }, *i;
87 /* don't load the default if `types' was used. */
88 if (mime->len != 0)
89 return 0;
91 for (i = m; i->mime != NULL; ++i) {
92 if (add_mime(mime, i->mime, i->ext) == -1)
93 return -1;
94 }
96 return 0;
97 }
99 static const char *
100 path_ext(const char *path)
102 const char *end;
104 end = path + strlen(path)-1;
105 for (; end != path; --end) {
106 if (*end == '.')
107 return end+1;
108 if (*end == '/')
109 break;
112 return NULL;
115 static int
116 mime_comp(const void *a, const void *b)
118 const struct etm *x = a, *y = b;
120 return strcmp(x->ext, y->ext);
123 void
124 sort_mime(struct mime *m)
126 qsort(m->t, m->len, sizeof(*m->t), mime_comp);
129 static int
130 mime_find(const void *a, const void *b)
132 const char *ext = a;
133 const struct etm *x = b;
135 return strcmp(ext, x->ext);
138 const char *
139 mime(struct conf *conf, struct vhost *host, const char *path)
141 const char *def, *ext;
142 struct etm *t;
144 def = vhost_default_mime(host, path);
146 if ((ext = path_ext(path)) == NULL)
147 return def;
149 t = bsearch(ext, conf->mime.t, conf->mime.len, sizeof(*conf->mime.t),
150 mime_find);
151 if (t != NULL)
152 return t->mime;
153 if (!strcmp(ext, "gmi") || !strcmp(ext, "gemini"))
154 return "text/gemini";
155 return def;
158 void
159 free_mime(struct mime *m)
161 free(m->t);