2 0fbe79b3 2021-01-18 op * Copyright (c) 2021 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 0fbe79b3 2021-01-18 op #include <errno.h>
18 0fbe79b3 2021-01-18 op #include <stdlib.h>
19 0fbe79b3 2021-01-18 op #include <string.h>
21 0fbe79b3 2021-01-18 op #include "gmid.h"
24 0fbe79b3 2021-01-18 op init_mime(void)
26 a010b0dd 2021-01-18 op conf.mimes.len = 0;
27 6119e13e 2021-01-19 op conf.mimes.cap = 16;
29 a010b0dd 2021-01-18 op conf.mimes.t = calloc(conf.mimes.cap, sizeof(struct etm));
30 a010b0dd 2021-01-18 op if (conf.mimes.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 0fbe79b3 2021-01-18 op add_mime(const char *mime, const char *ext)
38 a010b0dd 2021-01-18 op if (conf.mimes.len == conf.mimes.cap) {
39 a010b0dd 2021-01-18 op conf.mimes.cap *= 1.5;
40 a010b0dd 2021-01-18 op conf.mimes.t = realloc(conf.mimes.t,
41 a010b0dd 2021-01-18 op conf.mimes.cap * sizeof(struct etm));
42 a010b0dd 2021-01-18 op if (conf.mimes.t == NULL)
43 0fbe79b3 2021-01-18 op fatal("realloc: %s", strerror(errno));
46 a010b0dd 2021-01-18 op conf.mimes.t[conf.mimes.len].mime = mime;
47 a010b0dd 2021-01-18 op conf.mimes.t[conf.mimes.len].ext = ext;
48 a010b0dd 2021-01-18 op conf.mimes.len++;
51 0fbe79b3 2021-01-18 op /* load a default set of common mime-extension associations */
53 0fbe79b3 2021-01-18 op load_default_mime()
55 0fbe79b3 2021-01-18 op struct etm *i, m[] = {
56 0fbe79b3 2021-01-18 op {"application/pdf", "pdf"},
57 0fbe79b3 2021-01-18 op {"image/gif", "gif"},
58 0fbe79b3 2021-01-18 op {"image/jpeg", "jpg"},
59 0fbe79b3 2021-01-18 op {"image/jpeg", "jpeg"},
60 0fbe79b3 2021-01-18 op {"image/png", "png"},
61 0fbe79b3 2021-01-18 op {"image/svg+xml", "svg"},
62 0fbe79b3 2021-01-18 op {"text/gemini", "gemini"},
63 0fbe79b3 2021-01-18 op {"text/gemini", "gmi"},
64 0fbe79b3 2021-01-18 op {"text/markdown", "markdown"},
65 0fbe79b3 2021-01-18 op {"text/markdown", "md"},
66 0fbe79b3 2021-01-18 op {"text/plain", "txt"},
67 0fbe79b3 2021-01-18 op {"text/xml", "xml"},
71 0fbe79b3 2021-01-18 op for (i = m; i->mime != NULL; ++i)
72 0fbe79b3 2021-01-18 op add_mime(i->mime, i->ext);
75 0fbe79b3 2021-01-18 op static const char *
76 0fbe79b3 2021-01-18 op path_ext(const char *path)
78 0fbe79b3 2021-01-18 op const char *end;
80 0fbe79b3 2021-01-18 op end = path + strlen(path)-1;
81 0fbe79b3 2021-01-18 op for (; end != path; --end) {
82 0fbe79b3 2021-01-18 op if (*end == '.')
84 0fbe79b3 2021-01-18 op if (*end == '/')
92 6119e13e 2021-01-19 op mime(struct vhost *host, const char *path)
94 6119e13e 2021-01-19 op const char *def, *ext;
95 0fbe79b3 2021-01-18 op struct etm *t;
97 6119e13e 2021-01-19 op if ((def = host->default_mime) == NULL)
98 6119e13e 2021-01-19 op def = "application/octet-stream";
100 0fbe79b3 2021-01-18 op if ((ext = path_ext(path)) == NULL)
103 a010b0dd 2021-01-18 op for (t = conf.mimes.t; t->mime != NULL; ++t)
104 0fbe79b3 2021-01-18 op if (!strcmp(ext, t->ext))
105 0fbe79b3 2021-01-18 op return t->mime;