2021-01-18 19:18:57 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-02-12 13:47:20 +01:00
|
|
|
#include "gmid.h"
|
|
|
|
|
2021-01-18 19:18:57 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
void
|
2021-01-21 16:01:37 +01:00
|
|
|
init_mime(struct mime *mime)
|
2021-01-18 19:18:57 +01:00
|
|
|
{
|
2021-01-21 16:01:37 +01:00
|
|
|
mime->len = 0;
|
|
|
|
mime->cap = 16;
|
2021-01-18 19:18:57 +01:00
|
|
|
|
2021-01-21 16:01:37 +01:00
|
|
|
mime->t = calloc(mime->cap, sizeof(struct etm));
|
|
|
|
if (mime->t == NULL)
|
2021-01-18 19:18:57 +01:00
|
|
|
fatal("calloc: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register mime for the given extension */
|
|
|
|
void
|
2021-01-21 16:01:37 +01:00
|
|
|
add_mime(struct mime *mime, const char *mt, const char *ext)
|
2021-01-18 19:18:57 +01:00
|
|
|
{
|
2021-01-21 17:14:01 +01:00
|
|
|
size_t oldcap;
|
|
|
|
|
2021-01-21 16:01:37 +01:00
|
|
|
if (mime->len == mime->cap) {
|
2021-01-21 17:14:01 +01:00
|
|
|
oldcap = mime->cap;
|
2021-01-21 16:01:37 +01:00
|
|
|
mime->cap *= 1.5;
|
2021-01-21 17:14:01 +01:00
|
|
|
mime->t = recallocarray(mime->t, oldcap, mime->cap,
|
|
|
|
sizeof(struct etm));
|
2021-01-21 16:01:37 +01:00
|
|
|
if (mime->t == NULL)
|
2021-01-21 17:14:01 +01:00
|
|
|
err(1, "recallocarray");
|
2021-01-18 19:18:57 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 16:01:37 +01:00
|
|
|
mime->t[mime->len].mime = mt;
|
|
|
|
mime->t[mime->len].ext = ext;
|
|
|
|
mime->len++;
|
2021-01-18 19:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* load a default set of common mime-extension associations */
|
|
|
|
void
|
2021-01-21 16:01:37 +01:00
|
|
|
load_default_mime(struct mime *mime)
|
2021-01-18 19:18:57 +01:00
|
|
|
{
|
|
|
|
struct etm *i, m[] = {
|
|
|
|
{"application/pdf", "pdf"},
|
|
|
|
{"image/gif", "gif"},
|
|
|
|
{"image/jpeg", "jpg"},
|
|
|
|
{"image/jpeg", "jpeg"},
|
|
|
|
{"image/png", "png"},
|
|
|
|
{"image/svg+xml", "svg"},
|
|
|
|
{"text/gemini", "gemini"},
|
|
|
|
{"text/gemini", "gmi"},
|
|
|
|
{"text/markdown", "markdown"},
|
|
|
|
{"text/markdown", "md"},
|
|
|
|
{"text/plain", "txt"},
|
2021-04-22 21:10:08 +02:00
|
|
|
{"text/x-patch", "diff"},
|
2021-04-21 09:51:29 +02:00
|
|
|
{"text/x-patch", "patch"},
|
2021-01-18 19:18:57 +01:00
|
|
|
{"text/xml", "xml"},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = m; i->mime != NULL; ++i)
|
2021-01-21 16:01:37 +01:00
|
|
|
add_mime(mime, i->mime, i->ext);
|
2021-01-18 19:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
path_ext(const char *path)
|
|
|
|
{
|
|
|
|
const char *end;
|
|
|
|
|
|
|
|
end = path + strlen(path)-1;
|
|
|
|
for (; end != path; --end) {
|
|
|
|
if (*end == '.')
|
|
|
|
return end+1;
|
|
|
|
if (*end == '/')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2021-01-19 12:28:41 +01:00
|
|
|
mime(struct vhost *host, const char *path)
|
2021-01-18 19:18:57 +01:00
|
|
|
{
|
2021-01-19 12:28:41 +01:00
|
|
|
const char *def, *ext;
|
2021-01-18 19:18:57 +01:00
|
|
|
struct etm *t;
|
|
|
|
|
2021-01-24 15:11:40 +01:00
|
|
|
def = vhost_default_mime(host, path);
|
2021-01-19 12:28:41 +01:00
|
|
|
|
2021-01-18 19:18:57 +01:00
|
|
|
if ((ext = path_ext(path)) == NULL)
|
2021-01-19 12:28:41 +01:00
|
|
|
return def;
|
2021-01-18 19:18:57 +01:00
|
|
|
|
2021-01-21 16:01:37 +01:00
|
|
|
for (t = conf.mime.t; t->mime != NULL; ++t)
|
2021-01-18 19:18:57 +01:00
|
|
|
if (!strcmp(ext, t->ext))
|
|
|
|
return t->mime;
|
|
|
|
|
2021-01-19 12:28:41 +01:00
|
|
|
return def;
|
2021-01-18 19:18:57 +01:00
|
|
|
}
|