aboutsummaryrefslogtreecommitdiffstats
path: root/ui-plain.c
diff options
context:
space:
mode:
authorLars Hjemli2012-03-18 21:01:28 +0000
committerLars Hjemli2012-03-18 21:01:28 +0000
commitae90a0b2d18d66e7b1cb98b2e34fd697f8b5311a (patch)
tree0a2dd27e9d527914019026a9860c93c879b35cd7 /ui-plain.c
parent181b6e789b79fc385e5ec75b2b83f01192d54051 (diff)
parentd01c600c179593a53162a9d4e3040ecfc5078fdc (diff)
downloadcgit-ae90a0b2d18d66e7b1cb98b2e34fd697f8b5311a.tar.gz
cgit-ae90a0b2d18d66e7b1cb98b2e34fd697f8b5311a.tar.bz2
cgit-ae90a0b2d18d66e7b1cb98b2e34fd697f8b5311a.zip
Merge branch 'fh/mimetypes'
Diffstat (limited to 'ui-plain.c')
-rw-r--r--ui-plain.c52
1 files changed, 51 insertions, 1 deletions
diff --git a/ui-plain.c b/ui-plain.c
index 2abd210..85877d7 100644
--- a/ui-plain.c
+++ b/ui-plain.c
@@ -6,6 +6,7 @@
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include <stdio.h>
9#include "cgit.h" 10#include "cgit.h"
10#include "html.h" 11#include "html.h"
11#include "ui-shared.h" 12#include "ui-shared.h"
@@ -13,12 +14,53 @@
13int match_baselen; 14int match_baselen;
14int match; 15int match;
15 16
17static char *get_mimetype_from_file(const char *filename, const char *ext)
18{
19 static const char *delimiters;
20 char *result;
21 FILE *fd;
22 char line[1024];
23 char *mimetype;
24 char *token;
25
26 if (!filename)
27 return NULL;
28
29 fd = fopen(filename, "r");
30 if (!fd)
31 return NULL;
32
33 delimiters = " \t\r\n";
34 result = NULL;
35
36 /* loop over all lines in the file */
37 while (!result && fgets(line, sizeof(line), fd)) {
38 mimetype = strtok(line, delimiters);
39
40 /* skip empty lines and comment lines */
41 if (!mimetype || (mimetype[0] == '#'))
42 continue;
43
44 /* loop over all extensions of mimetype */
45 while ((token = strtok(NULL, delimiters))) {
46 if (!strcasecmp(ext, token)) {
47 result = xstrdup(mimetype);
48 break;
49 }
50 }
51 }
52 fclose(fd);
53
54 return result;
55}
56
16static void print_object(const unsigned char *sha1, const char *path) 57static void print_object(const unsigned char *sha1, const char *path)
17{ 58{
18 enum object_type type; 59 enum object_type type;
19 char *buf, *ext; 60 char *buf, *ext;
20 unsigned long size; 61 unsigned long size;
21 struct string_list_item *mime; 62 struct string_list_item *mime;
63 int freemime;
22 64
23 type = sha1_object_info(sha1, &size); 65 type = sha1_object_info(sha1, &size);
24 if (type == OBJ_BAD) { 66 if (type == OBJ_BAD) {
@@ -33,10 +75,16 @@ static void print_object(const unsigned char *sha1, const char *path)
33 } 75 }
34 ctx.page.mimetype = NULL; 76 ctx.page.mimetype = NULL;
35 ext = strrchr(path, '.'); 77 ext = strrchr(path, '.');
78 freemime = 0;
36 if (ext && *(++ext)) { 79 if (ext && *(++ext)) {
37 mime = string_list_lookup(&ctx.cfg.mimetypes, ext); 80 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
38 if (mime) 81 if (mime) {
39 ctx.page.mimetype = (char *)mime->util; 82 ctx.page.mimetype = (char *)mime->util;
83 } else {
84 ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
85 if (ctx.page.mimetype)
86 freemime = 1;
87 }
40 } 88 }
41 if (!ctx.page.mimetype) { 89 if (!ctx.page.mimetype) {
42 if (buffer_is_binary(buf, size)) 90 if (buffer_is_binary(buf, size))
@@ -50,6 +98,8 @@ static void print_object(const unsigned char *sha1, const char *path)
50 cgit_print_http_headers(&ctx); 98 cgit_print_http_headers(&ctx);
51 html_raw(buf, size); 99 html_raw(buf, size);
52 match = 1; 100 match = 1;
101 if (freemime)
102 free(ctx.page.mimetype);
53} 103}
54 104
55static char *buildpath(const char *base, int baselen, const char *path) 105static char *buildpath(const char *base, int baselen, const char *path)