Blob


1 /*
2 * Copyright (c) 2021 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 <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "gmid.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 void
36 add_mime(struct mime *mime, const char *mt, const char *ext)
37 {
38 if (mime->len == mime->cap) {
39 mime->cap *= 1.5;
40 mime->t = realloc(mime->t, mime->cap * sizeof(struct etm));
41 if (mime->t == NULL)
42 fatal("realloc: %s", strerror(errno));
43 }
45 mime->t[mime->len].mime = mt;
46 mime->t[mime->len].ext = ext;
47 mime->len++;
48 }
50 /* load a default set of common mime-extension associations */
51 void
52 load_default_mime(struct mime *mime)
53 {
54 struct etm *i, m[] = {
55 {"application/pdf", "pdf"},
56 {"image/gif", "gif"},
57 {"image/jpeg", "jpg"},
58 {"image/jpeg", "jpeg"},
59 {"image/png", "png"},
60 {"image/svg+xml", "svg"},
61 {"text/gemini", "gemini"},
62 {"text/gemini", "gmi"},
63 {"text/markdown", "markdown"},
64 {"text/markdown", "md"},
65 {"text/plain", "txt"},
66 {"text/xml", "xml"},
67 {NULL, NULL}
68 };
70 for (i = m; i->mime != NULL; ++i)
71 add_mime(mime, i->mime, i->ext);
72 }
74 static const char *
75 path_ext(const char *path)
76 {
77 const char *end;
79 end = path + strlen(path)-1;
80 for (; end != path; --end) {
81 if (*end == '.')
82 return end+1;
83 if (*end == '/')
84 break;
85 }
87 return NULL;
88 }
90 const char *
91 mime(struct vhost *host, const char *path)
92 {
93 const char *def, *ext;
94 struct etm *t;
96 if ((def = host->default_mime) == NULL)
97 def = "application/octet-stream";
99 if ((ext = path_ext(path)) == NULL)
100 return def;
102 for (t = conf.mime.t; t->mime != NULL; ++t)
103 if (!strcmp(ext, t->ext))
104 return t->mime;
106 return def;