Diff options
Diffstat (limited to 'source/ui-plain.c')
| -rw-r--r-- | source/ui-plain.c | 51 |
1 file changed, 50 insertions, 1 deletion
diff --git a/source/ui-plain.c b/source/ui-plain.c index 0d12896..43bdb22 100644 --- a/source/ui-plain.c +++ b/source/ui-plain.c @@ -42,6 +42,55 @@ static int is_unsafe_type(const char *mimetype) strcmp(mimetype, "application/pdf"); } +static char *get_mimetype_for_filename(const char *filename) +{ + const char *ext; + char *type, line[1024]; + struct string_list fields = STRING_LIST_INIT_NODUP; + size_t i; + FILE *file; + struct string_list_item *entry; + + if (!filename) + return NULL; + + ext = strrchr(filename, '.'); + if (!ext) + return NULL; + ++ext; + if (!ext[0]) + return NULL; + entry = string_list_lookup(&ctx.cfg.mimetypes, ext); + if (entry) + return xstrdup(entry->util); + + if (!ctx.cfg.mimetype_file) + return NULL; + file = fopen(ctx.cfg.mimetype_file, "r"); + if (!file) + return NULL; + while (fgets(line, sizeof(line), file)) { + // A line of a mime.types file is one type followed by every + // extension that maps to it. + string_list_split_in_place(&fields, line, " \t\r\n", -1); + string_list_remove_empty_items(&fields, 0); + if (!fields.nr || fields.items[0].string[0] == '#') { + string_list_clear(&fields, 0); + continue; + } + type = fields.items[0].string; + for (i = 1; i < fields.nr; i++) { + if (!strcasecmp(ext, fields.items[i].string)) { + fclose(file); + return xstrdup(type); + } + } + string_list_clear(&fields, 0); + } + fclose(file); + return NULL; +} + /* * Writes the response for the object, error pages included, so the walk * does not go on to report the path as missing. @@ -69,7 +118,7 @@ static void print_object(const struct object_id *oid, const char *path) return; } - mimetype = cgit_get_mimetype_for_filename(path); + mimetype = get_mimetype_for_filename(path); ctx.page.mimetype = mimetype; if (!ctx.repo->enable_html_serving) { |
