diff options
Diffstat (limited to 'ui-plain.c')
| -rw-r--r-- | ui-plain.c | 52 |
1 files changed, 51 insertions, 1 deletions
| @@ -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 @@ | |||
| 13 | int match_baselen; | 14 | int match_baselen; |
| 14 | int match; | 15 | int match; |
| 15 | 16 | ||
| 17 | static 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 | |||
| 16 | static void print_object(const unsigned char *sha1, const char *path) | 57 | static 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 | ||
| 55 | static char *buildpath(const char *base, int baselen, const char *path) | 105 | static char *buildpath(const char *base, int baselen, const char *path) |
