summaryrefslogtreecommitdiffstats
path: root/src/mime.c
blob: 1a60ae018936f6da5830ea72ddb8a3ef3297c77b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdbool.h>
#include <string.h>
#include "mime.h"

static bool
has_suffix(const char *str, const char *suff)
{
	size_t a = strlen(str), b = strlen(suff);
	if (a < b) {
		return false;
	}
	return strncmp(&str[a - b], suff, b) == 0;
}

const char *
gmnisrv_mimetype_for_path(const char *path)
{
	// TODO: Read /etc/mime.types
	// TODO: Consider adding content-disposition fields like filename
	if (has_suffix(path, ".gmi") || has_suffix(path, ".gemini")) {
		return "text/gemini";
	}
	if (has_suffix(path, ".txt")) {
		return "text/plain";
	}
	if (has_suffix(path, ".xml")) {
		return "text/xml";
	}
	if (has_suffix(path, ".png")) {
		return "image/png";
	}
	return "application/octet-stream";
}