[PATCH 1/1] ui_plain: automatically lookup mimetype when mimetype-file is set

Ferry Huberts mailings at hupie.com
Thu Jun 23 02:04:14 CEST 2011


forget this patch please.

It's 2AM and it appears I need some sleep.
Will send a new patch in the morning

On 06/23/2011 02:02 AM, Ferry Huberts wrote:
> From: Ferry Huberts <ferry.huberts at pelagic.nl>
> 
> Signed-off-by: Ferry Huberts <ferry.huberts at pelagic.nl>
> ---
>  cgit.c       |    2 ++
>  cgit.h       |    1 +
>  cgitrc.5.txt |   10 ++++++++++
>  ui-plain.c   |   41 ++++++++++++++++++++++++++++++++++++++---
>  4 files changed, 51 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..3a3d1b6 100644
> --- a/cgitrc.5.txt
> +++ b/cgitrc.5.txt
> @@ -222,6 +222,16 @@ 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. 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..7062733 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,39 @@
>  int match_baselen;
>  int match;
>  
> +static char * getMimeFromFile(char *ext) {
> +	static const char * delimiters = " \t\r\n";
> +	char * result = NULL;
> +	FILE * fd;
> +	char line[1024];
> +	char * mimetype;
> +	char * token;
> +
> +	fd = fopen("/etc/mime.types", "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 +63,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.cfg.mimetype_file && (ctx.page.mimetype = getMimeFromFile(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 +83,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)


-- 
Ferry Huberts




More information about the CGit mailing list