X-Git-Url: https://git.ladys.computer/Gitweb/blobdiff_plain/cbce69e6debd656e01f53fc8cb24f08fcde0b936330be3dccc59d15e667397c1..2632541dc57dd0ccba7e315e2755c9dbd028ed210fff7a39a86c0346e879e365:/gitweb.cgi
diff --git a/gitweb.cgi b/gitweb.cgi
index 77b4c23..7d50f04 100755
--- a/gitweb.cgi
+++ b/gitweb.cgi
@@ -39,7 +39,7 @@ my $home_link = $my_uri;
my $home_text = "indextext.html";
# URI of default stylesheet
-my $stylesheet = "gitweb.css";
+my $stylesheet = "gitweb.css";
# source of projects list
#my $projects_list = $projectroot;
@@ -47,7 +47,12 @@ my $projects_list = "index/index.aux";
# default blob_plain mimetype and default charset for text/plain blob
my $default_blob_plain_mimetype = 'text/plain';
-my $default_text_plain_charset = 'utf-8'; # can be undefined
+my $default_text_plain_charset = undef;
+
+# file to use for guessing MIME types before trying /etc/mime.types
+# (relative to the current git repository)
+my $mimetypes_file = undef;
+
# input validation and dispatch
my $action = $cgi->param('a');
@@ -256,6 +261,12 @@ sub git_header_html {
$title .= " - $project";
if (defined $action) {
$title .= "/$action";
+ if (defined $file_name) {
+ $title .= " - $file_name";
+ if ($action eq "tree" && $file_name !~ m|/$|) {
+ $title .= "/";
+ }
+ }
}
}
print $cgi->header(-type=>'text/html', -charset => 'utf-8', -status=> $status, -expires => $expires);
@@ -267,7 +278,7 @@ sub git_header_html {
-
+
$title
$rss_link
@@ -1486,6 +1497,40 @@ sub git_blob {
git_footer_html();
}
+sub mimetype_guess_file {
+ my $filename = shift;
+ my $mimemap = shift;
+ -r $mimemap or return undef;
+
+ my %mimemap;
+ open(MIME, $mimemap) or return undef;
+ while () {
+ my ($mime, $exts) = split(/\t+/);
+ my @exts = split(/\s+/, $exts);
+ foreach my $ext (@exts) {
+ $mimemap{$ext} = $mime;
+ }
+ }
+ close(MIME);
+
+ $filename =~ /\.(.*?)$/;
+ return $mimemap{$1};
+}
+
+sub mimetype_guess {
+ my $filename = shift;
+ my $mime;
+ $filename =~ /\./ or return undef;
+
+ if ($mimetypes_file) {
+ my $file = $mimetypes_file;
+ #$file =~ m#^/# or $file = "$projectroot/$path/$file";
+ $mime = mimetype_guess_file($filename, $file);
+ }
+ $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
+ return $mime;
+}
+
sub git_blob_plain_mimetype {
my $fd = shift;
my $filename = shift;
@@ -1493,6 +1538,11 @@ sub git_blob_plain_mimetype {
# just in case
return $default_blob_plain_mimetype unless $fd;
+ if ($filename) {
+ my $mime = mimetype_guess($filename);
+ $mime and return $mime;
+ }
+
if (-T $fd) {
return 'text/plain' .
($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
@@ -1523,9 +1573,9 @@ sub git_blob_plain {
print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
undef $/;
- binmode STDOUT, ':raw' unless $type =~ m/^text\//;
+ binmode STDOUT, ':raw';
print <$fd>;
- binmode STDOUT, ':utf8' unless $type =~ m/^text\//;
+ binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
$/ = "\n";
close $fd;
}