Code refactoring: get_file_mime got into mime.c and all security code moved into a procedure

This commit is contained in:
Solene Rapenne 2020-12-04 19:08:36 +01:00
commit 345215fa9b
2 changed files with 79 additions and 63 deletions

25
mimes.c
View file

@ -1,10 +1,15 @@
#include <string.h>
#include <unistd.h>
#include <string.h>
void get_file_mime(const char *, char *, const ssize_t);
struct mimes {
char extension[10];
char type [70];
};
struct mimes database[] = {
{"7z", "application/x-7z-compressed"},
{"atom", "application/atom+xml"},
@ -110,3 +115,23 @@ struct mimes database[] = {
{"xpi", "application/x-xpinstall"},
{"zip", "application/zip"}
};
void
get_file_mime(const char *path, char *type, const ssize_t type_size)
{
char *extension;
extension = strrchr(path, '.');
/* look for the MIME in the database */
for (int i = 0; i < sizeof(database) / sizeof(struct mimes); i++) {
if (strcmp(database[i].extension, extension + 1) == 0) {
strlcpy(type, database[i].type, type_size);
break;
}
}
/* if no MIME have been found, set a default one */
if (strlen(type) == 0)
strlcpy(type, "text/gemini", type_size);
}