diff --git a/src/main.c b/src/main.c index 3129636..8c50a53 100644 --- a/src/main.c +++ b/src/main.c @@ -38,7 +38,7 @@ static void signal_int(int signum) { int main(int argc, char** argv) { - mime_load(NULL); + mime_init(NULL); config_server *config = config_server_new(); if (config_read_ini("khttpd.ini", config) < 0) { fatal("Could not read config"); @@ -152,7 +152,7 @@ int main(int argc, char** argv) { } } - mime_free(); + mime_destroy(); config_server_delete(config); svr_release(serverfd); serverfd = 0; diff --git a/src/mime.c b/src/mime.c index 5c80a83..b5886b3 100644 --- a/src/mime.c +++ b/src/mime.c @@ -10,7 +10,18 @@ mime_type *mime_list = NULL; -int mime_load(const char* file) { +int mime_init(const char* file) { + mime_destroy(); + return mime_load(&mime_list, NULL); +} +void mime_destroy() { + if (mime_list != NULL) { + mime_free(mime_list); + } + mime_list = NULL; +} + +int mime_load(mime_type **list, const char* file) { FILE *mimetypes = fopen(file == NULL ? MIME_DEFAULT_FILE : file, "r"); if (mimetypes == NULL) { return -1; @@ -45,38 +56,40 @@ int mime_load(const char* file) { free(buffer); fclose(mimetypes); - if (mime_list != NULL) { - mime_free(); - } - mime_list = new_list; + *list = new_list; count = 0; mime_type *elem; - LL_COUNT(mime_list, elem, count); + LL_COUNT(new_list, elem, count); return count; } -void mime_free() { +void mime_free(mime_type *list) { + if (list == NULL) { + list = mime_list; + } mime_type *elem, *tmp; - LL_FOREACH_SAFE(mime_list, elem, tmp) { + LL_FOREACH_SAFE(list, elem, tmp) { free(elem->extension); free(elem->mime); free(elem); } - mime_list = NULL; } -void mime_print_all() { +void mime_print_all(mime_type *list) { mime_type *elem; - LL_FOREACH(mime_list, elem) { + LL_FOREACH(list, elem) { printf("%s\t\t%s\n", elem->mime, elem->extension); } } const char* mime_get_type(const char* filename, const char* fallback) { + return mime_list_get_type(mime_list, filename, fallback); +} +const char* mime_list_get_type(mime_type *list, const char* filename, const char* fallback) { char *ext = strrchr(filename, '.'); if (ext != NULL && strlen(ext) > 1) { ext++; //Skip . mime_type *elem; - LL_FOREACH(mime_list, elem) { + LL_FOREACH(list, elem) { if (strcmp(ext, elem->extension) == 0) { return elem->mime; } @@ -84,7 +97,6 @@ const char* mime_get_type(const char* filename, const char* fallback) { } return mime_get_type_magic(filename, fallback); } - const char* mime_get_type_magic(const char* filename, const char* fallback) { magic_t magic; @@ -100,4 +112,4 @@ const char* mime_get_type_magic(const char* filename, const char* fallback) { } return fallback; -} \ No newline at end of file +} diff --git a/src/mime.h b/src/mime.h index ada68dd..ccab8c9 100644 --- a/src/mime.h +++ b/src/mime.h @@ -24,11 +24,15 @@ extern "C" { extern mime_type *mime_list; - int mime_load(const char* file); - void mime_free(); - void mime_print_all(); + int mime_init(const char* file); + void mime_destroy(); + + int mime_load(mime_type **list, const char* file); + void mime_free(mime_type *list); + void mime_print_all(mime_type *list); const char* mime_get_type(const char* filename, const char* fallback); + const char* mime_list_get_type(mime_type *list, const char* filename, const char* fallback); const char* mime_get_type_magic(const char* filename, const char* fallback); #ifdef __cplusplus