diff options
author | Christian Hesse | 2015-08-16 14:53:52 +0200 |
---|---|---|
committer | Jason A. Donenfeld | 2015-08-17 14:25:08 +0200 |
commit | aa943bc9a68ccdcc5cbe29f6ac3b5e787d4c22ca (patch) | |
tree | fc3118e65804d0dcc206e8d39fd55a76f9b3b9d2 /ui-plain.c | |
parent | f5c83d7b5ddceb03e1c6bda2e43c48500c7da9f5 (diff) | |
download | cgit-aa943bc9a68ccdcc5cbe29f6ac3b5e787d4c22ca.tar.gz cgit-aa943bc9a68ccdcc5cbe29f6ac3b5e787d4c22ca.tar.bz2 cgit-aa943bc9a68ccdcc5cbe29f6ac3b5e787d4c22ca.zip |
refactor get_mimetype_from_file() to get_mimetype_for_filename()
* handle mimetype within a single function
* return allocated memory on success
Signed-off-by: Christian Hesse <mail@eworm.de>
Diffstat (limited to 'ui-plain.c')
-rw-r--r-- | ui-plain.c | 28 |
1 files changed, 6 insertions, 22 deletions
@@ -19,10 +19,8 @@ struct walk_tree_context { | |||
19 | static int print_object(const unsigned char *sha1, const char *path) | 19 | static int print_object(const unsigned char *sha1, const char *path) |
20 | { | 20 | { |
21 | enum object_type type; | 21 | enum object_type type; |
22 | char *buf, *ext; | 22 | char *buf, *mimetype; |
23 | unsigned long size; | 23 | unsigned long size; |
24 | struct string_list_item *mime; | ||
25 | int freemime; | ||
26 | 24 | ||
27 | type = sha1_object_info(sha1, &size); | 25 | type = sha1_object_info(sha1, &size); |
28 | if (type == OBJ_BAD) { | 26 | if (type == OBJ_BAD) { |
@@ -35,22 +33,10 @@ static int print_object(const unsigned char *sha1, const char *path) | |||
35 | cgit_print_error_page(404, "Not found", "Not found"); | 33 | cgit_print_error_page(404, "Not found", "Not found"); |
36 | return 0; | 34 | return 0; |
37 | } | 35 | } |
38 | ctx.page.mimetype = NULL; | 36 | |
39 | ext = strrchr(path, '.'); | 37 | mimetype = get_mimetype_for_filename(path); |
40 | freemime = 0; | 38 | ctx.page.mimetype = mimetype; |
41 | if (ext && *(++ext)) { | 39 | |
42 | mime = string_list_lookup(&ctx.cfg.mimetypes, ext); | ||
43 | if (mime) { | ||
44 | ctx.page.mimetype = (char *)mime->util; | ||
45 | ctx.page.charset = NULL; | ||
46 | } else { | ||
47 | ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext); | ||
48 | if (ctx.page.mimetype) { | ||
49 | freemime = 1; | ||
50 | ctx.page.charset = NULL; | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | if (!ctx.page.mimetype) { | 40 | if (!ctx.page.mimetype) { |
55 | if (buffer_is_binary(buf, size)) { | 41 | if (buffer_is_binary(buf, size)) { |
56 | ctx.page.mimetype = "application/octet-stream"; | 42 | ctx.page.mimetype = "application/octet-stream"; |
@@ -64,9 +50,7 @@ static int print_object(const unsigned char *sha1, const char *path) | |||
64 | ctx.page.etag = sha1_to_hex(sha1); | 50 | ctx.page.etag = sha1_to_hex(sha1); |
65 | cgit_print_http_headers(); | 51 | cgit_print_http_headers(); |
66 | html_raw(buf, size); | 52 | html_raw(buf, size); |
67 | /* If we allocated this, then casting away const is safe. */ | 53 | free(mimetype); |
68 | if (freemime) | ||
69 | free((char*) ctx.page.mimetype); | ||
70 | return 1; | 54 | return 1; |
71 | } | 55 | } |
72 | 56 | ||