@matheusbsbs / matheus-bagliano / commits / 7f718e8b46
Fork 0

gitweb: Add charset info to "raw" output of 'text/plain' blobs

Earlier "blob_plain" view sent "charset=utf-8" only when gitweb guessed the content type to be text by reading from it, and not when the MIME type was obtained from /etc/mime.types, or when gitweb couldn't guess mimetype and used $default_blob_plain_mimetype. This fixes the bug by always add charset info from $default_text_plain_charset (if it is defined) to "raw" (a=blob_plain) output for 'text/plain' blobs. Generating information for Content-Type: header got separated into blob_contenttype() subroutine; adding charset info in a special case was removed from blob_mimetype(), which now should return mimetype only. While at it cleanup code a bit: put subroutine parameter initialization first, make error message more robust (when $file_name is not defined) if more cryptic, remove unnecessary '"' around variable ("$var" -> $var). Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Jun 3, 2008 at 16:47 UTC 7f718e8b46b37a9e1caf58a372938817f04885d9
1 file changed +20 -9
gitweb/gitweb.perl
+20 -9
@@ -2481,8 +2481,7 @@ sub blob_mimetype {
2481 return $default_blob_plain_mimetype unless $fd;
2482
2483 if (-T $fd) {
2484 - return 'text/plain' .
2485 - ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2484 + return 'text/plain';
2485 } elsif (! $filename) {
2486 return 'application/octet-stream';
2487 } elsif ($filename =~ m/\.png$/i) {
@@ -2496,6 +2495,17 @@ sub blob_mimetype {
2495 }
2496 }
2497
2498 +sub blob_contenttype {
2499 + my ($fd, $file_name, $type) = @_;
2500 +
2501 + $type ||= blob_mimetype($fd, $file_name);
2502 + if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2503 + $type .= "; charset=$default_text_plain_charset";
2504 + }
2505 +
2506 + return $type;
2507 +}
2508 +
2509 ## ======================================================================
2510 ## functions printing HTML: header, footer, error page
2511
@@ -4374,6 +4384,7 @@ sub git_heads {
4384 }
4385
4386 sub git_blob_plain {
4387 + my $type = shift;
4388 my $expires;
4389
4390 if (!defined $hash) {
@@ -4389,13 +4400,13 @@ sub git_blob_plain {
4400 $expires = "+1d";
4401 }
4402
4392 - my $type = shift;
4403 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4394 - or die_error(undef, "Couldn't cat $file_name, $hash");
4404 + or die_error(undef, "Open git-cat-file blob '$hash' failed");
4405
4396 - $type ||= blob_mimetype($fd, $file_name);
4406 + # content-type (can include charset)
4407 + $type = blob_contenttype($fd, $file_name, $type);
4408
4398 - # save as filename, even when no $file_name is given
4409 + # "save as" filename, even when no $file_name is given
4410 my $save_as = "$hash";
4411 if (defined $file_name) {
4412 $save_as = $file_name;
@@ -4404,9 +4415,9 @@ sub git_blob_plain {
4415 }
4416
4417 print $cgi->header(
4407 - -type => "$type",
4408 - -expires=>$expires,
4409 - -content_disposition => 'inline; filename="' . "$save_as" . '"');
4418 + -type => $type,
4419 + -expires => $expires,
4420 + -content_disposition => 'inline; filename="' . $save_as . '"');
4421 undef $/;
4422 binmode STDOUT, ':raw';
4423 print <$fd>;