Diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Move the mimetype lookup into ui-plain.c
-rw-r--r--source/shared.c49
-rw-r--r--source/shared.h2
-rw-r--r--source/ui-plain.c51
3 files changed, 50 insertions, 52 deletions
diff --git a/source/shared.c b/source/shared.c
index c3867f6..5c5e65c 100644
--- a/source/shared.c
+++ b/source/shared.c
@@ -564,55 +564,6 @@ char *cgit_expand_macros(const char *text)
return result;
}
-char *cgit_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;
-}
-
int cgit_readme_is_tracked(const char *readme)
{
const char *colon = strchr(readme, ':');
diff --git a/source/shared.h b/source/shared.h
index 590198a..e1eb39b 100644
--- a/source/shared.h
+++ b/source/shared.h
@@ -109,8 +109,6 @@ extern void cgit_utf8_sanitize(char *text);
*/
extern char *cgit_expand_macros(const char *text);
-extern char *cgit_get_mimetype_for_filename(const char *filename);
-
/*
* Whether a readme setting names a file tracked in the repository, written
* as ref:path or :path, rather than a file on the server's disk.
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) {