]> Lady’s Gitweb - Gitweb/blobdiff - gitweb.perl
gitweb: Add charset info to "raw" output of 'text/plain' blobs
[Gitweb] / gitweb.perl
index 3e0ca4eac614743554baf7961905af78283fd07f032403ca2b16f9432cef60f6..da235e3bfaa3cf89a63bfdb436ef8bcf29a5b0686a0c0d95aebe7ecb7d64490b 100755 (executable)
@@ -2482,8 +2482,7 @@ sub blob_mimetype {
        return $default_blob_plain_mimetype unless $fd;
 
        if (-T $fd) {
        return $default_blob_plain_mimetype unless $fd;
 
        if (-T $fd) {
-               return 'text/plain' .
-                      ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
+               return 'text/plain';
        } elsif (! $filename) {
                return 'application/octet-stream';
        } elsif ($filename =~ m/\.png$/i) {
        } elsif (! $filename) {
                return 'application/octet-stream';
        } elsif ($filename =~ m/\.png$/i) {
@@ -2497,6 +2496,17 @@ sub blob_mimetype {
        }
 }
 
        }
 }
 
+sub blob_contenttype {
+       my ($fd, $file_name, $type) = @_;
+
+       $type ||= blob_mimetype($fd, $file_name);
+       if ($type eq 'text/plain' && defined $default_text_plain_charset) {
+               $type .= "; charset=$default_text_plain_charset";
+       }
+
+       return $type;
+}
+
 ## ======================================================================
 ## functions printing HTML: header, footer, error page
 
 ## ======================================================================
 ## functions printing HTML: header, footer, error page
 
@@ -4375,6 +4385,7 @@ sub git_heads {
 }
 
 sub git_blob_plain {
 }
 
 sub git_blob_plain {
+       my $type = shift;
        my $expires;
 
        if (!defined $hash) {
        my $expires;
 
        if (!defined $hash) {
@@ -4390,13 +4401,13 @@ sub git_blob_plain {
                $expires = "+1d";
        }
 
                $expires = "+1d";
        }
 
-       my $type = shift;
        open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
        open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
-               or die_error(undef, "Couldn't cat $file_name, $hash");
+               or die_error(undef, "Open git-cat-file blob '$hash' failed");
 
 
-       $type ||= blob_mimetype($fd, $file_name);
+       # content-type (can include charset)
+       $type = blob_contenttype($fd, $file_name, $type);
 
 
-       # save as filename, even when no $file_name is given
+       # "save as" filename, even when no $file_name is given
        my $save_as = "$hash";
        if (defined $file_name) {
                $save_as = $file_name;
        my $save_as = "$hash";
        if (defined $file_name) {
                $save_as = $file_name;
@@ -4405,9 +4416,9 @@ sub git_blob_plain {
        }
 
        print $cgi->header(
        }
 
        print $cgi->header(
-               -type => "$type",
-               -expires=>$expires,
-               -content_disposition => 'inline; filename="' . "$save_as" . '"');
+               -type => $type,
+               -expires => $expires,
+               -content_disposition => 'inline; filename="' . $save_as . '"');
        undef $/;
        binmode STDOUT, ':raw';
        print <$fd>;
        undef $/;
        binmode STDOUT, ':raw';
        print <$fd>;
This page took 0.159368 seconds and 4 git commands to generate.