From: Ferry Huberts <[email protected]>

Signed-off-by: Ferry Huberts <[email protected]>
---
 cgit.c       |    2 ++
 cgit.h       |    1 +
 cgitrc.5.txt |   11 +++++++++++
 ui-plain.c   |   44 +++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/cgit.c b/cgit.c
index b7807ad..abb698b 100644
--- a/cgit.c
+++ b/cgit.c
@@ -236,6 +236,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.ssdiff = atoi(value);
        else if (!strcmp(name, "agefile"))
                ctx.cfg.agefile = xstrdup(value);
+       else if (!strcmp(name, "mimetype-file"))
+               ctx.cfg.mimetype_file = xstrdup(value);
        else if (!strcmp(name, "renamelimit"))
                ctx.cfg.renamelimit = atoi(value);
        else if (!strcmp(name, "remove-suffix"))
diff --git a/cgit.h b/cgit.h
index bad66f0..db24941 100644
--- a/cgit.h
+++ b/cgit.h
@@ -175,6 +175,7 @@ struct cgit_config {
        char *index_info;
        char *logo;
        char *logo_link;
+       char *mimetype_file;
        char *module_link;
        char *project_list;
        char *readme;
diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index 4721c1e..b1a41b2 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -222,6 +222,17 @@ max-stats::
        "month", "quarter" and "year". If unspecified, statistics are
        disabled. Default value: none. See also: "repo.max-stats".
 
+mimetype-file::
+       Specifies the file to use for automatic mimetype lookup. If specified
+       then this field takes precedence over "mimetype.<ext>". If unspecified
+       then no such lookup is performed. The typical file to use on a Linux
+       system /etc/mime.types. Default value: none. See also: "mimetype.<ext>".
+       The format of the file must comply to:
+       - a comment line is an empty line or a line starting hash (#) optional
+         preceded by whitespace,
+       - a non-comment line starts with the mimetype (like image/png), followed
+         by one or more file extensions, all separated by whitespace.
+
 mimetype.<ext>::
        Set the mimetype for the specified filename extension. This is used
        by the `plain` command when returning blob content.
diff --git a/ui-plain.c b/ui-plain.c
index 733db4d..85ea7da 100644
--- a/ui-plain.c
+++ b/ui-plain.c
@@ -6,6 +6,8 @@
  *   (see COPYING for full license text)
  */
 
+#include <stdio.h>
+#include <stdbool.h>
 #include "cgit.h"
 #include "html.h"
 #include "ui-shared.h"
@@ -13,12 +15,42 @@
 int match_baselen;
 int match;
 
+static char *get_mimetype_from_file(const char *filename, const char *ext) {
+       static const char *delimiters = " \t\r\n";
+       char *result = NULL;
+       FILE *fd;
+       char line[1024];
+       char *mimetype;
+       char *token;
+
+       if (!filename)
+               return NULL;
+
+       fd = fopen(filename, "r");
+       if (fd) {
+               while (fgets(line, sizeof(line), fd)) {
+                       mimetype = strtok(line, delimiters);
+                       if (mimetype && (mimetype[0] != '#')) {
+                               while ((token = strtok(NULL, delimiters))) {
+                                       if (!strcasecmp(ext, token)) {
+                                               result = xstrdup(mimetype);
+                                               goto endloop;
+                                       }
+                               }
+                       }
+               }
+               endloop: fclose(fd);
+       }
+       return result;
+}
+
 static void print_object(const unsigned char *sha1, const char *path)
 {
        enum object_type type;
        char *buf, *ext;
        unsigned long size;
        struct string_list_item *mime;
+       bool freemime = false;
 
        type = sha1_object_info(sha1, &size);
        if (type == OBJ_BAD) {
@@ -34,9 +66,13 @@ static void print_object(const unsigned char *sha1, const 
char *path)
        ctx.page.mimetype = NULL;
        ext = strrchr(path, '.');
        if (ext && *(++ext)) {
-               mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
-               if (mime)
-                       ctx.page.mimetype = (char *)mime->util;
+               if ((ctx.page.mimetype = 
get_mimetype_from_file(ctx.cfg.mimetype_file, ext))) {
+                       freemime = true;
+               } else {
+                       mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
+                       if (mime)
+                               ctx.page.mimetype = (char *)mime->util;
+               }
        }
        if (!ctx.page.mimetype) {
                if (buffer_is_binary(buf, size))
@@ -50,6 +86,8 @@ static void print_object(const unsigned char *sha1, const 
char *path)
        cgit_print_http_headers(&ctx);
        html_raw(buf, size);
        match = 1;
+       if (freemime)
+               free(ctx.page.mimetype);
 }
 
 static char *buildpath(const char *base, int baselen, const char *path)
-- 
1.7.5.4


_______________________________________________
cgit mailing list
[email protected]
http://hjemli.net/mailman/listinfo/cgit

Reply via email to