@matheusbsbs / matheus-bagliano / commits / f5aa79d909
Fork 0

gitweb: safely output binary files for 'blob_plain' action

gitweb tries now to output correct Content-Type header for 'blob_plain' action; for now text/plain for text files, appropriate image MIME type for *.png, *.gif and *.jpg/*.jpeg files, and application/octet-stream for other binary files. Introduced new configuration variables: $default_blob_plain_mimetype and $default_text_plain_charset (only 'utf-8' is guaranteed to work for the latter). binmode changed to ':raw' in git_blob_plain for output of non-text files. Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Jun 17, 2006 at 13:32 UTC f5aa79d909f8c4838bf500c22fa5f7967d219925
1 file changed +39 -4
gitweb/gitweb.cgi
+39 -4
@@ -39,12 +39,16 @@ my $home_link = $my_uri;
39 my $home_text = "indextext.html";
40
41 # URI of default stylesheet
42 -my $stylesheet = "gitweb.css";
42 +my $stylesheet = "gitweb.css";
43
44 # source of projects list
45 #my $projects_list = $projectroot;
46 my $projects_list = "index/index.aux";
47
48 +# default blob_plain mimetype and default charset for text/plain blob
49 +my $default_blob_plain_mimetype = 'text/plain';
50 +my $default_text_plain_charset = 'utf-8'; # can be undefined
51 +
52 # input validation and dispatch
53 my $action = $cgi->param('a');
54 if (defined $action) {
@@ -1482,15 +1486,46 @@ sub git_blob {
1486 git_footer_html();
1487 }
1488
1489 +sub git_blob_plain_mimetype {
1490 + my $fd = shift;
1491 + my $filename = shift;
1492 +
1493 + # just in case
1494 + return $default_blob_plain_mimetype unless $fd;
1495 +
1496 + if (-T $fd) {
1497 + return 'text/plain' .
1498 + ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1499 + } elsif (! $filename) {
1500 + return 'application/octet-stream';
1501 + } elsif ($filename =~ m/\.png$/i) {
1502 + return 'image/png';
1503 + } elsif ($filename =~ m/\.gif$/i) {
1504 + return 'image/gif';
1505 + } elsif ($filename =~ m/\.jpe?g$/i) {
1506 + return 'image/jpeg';
1507 + } else {
1508 + return 'application/octet-stream';
1509 + }
1510 +}
1511 +
1512 sub git_blob_plain {
1486 - my $save_as = "$hash.txt";
1513 + open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1514 + my $type = git_blob_plain_mimetype($fd, $file_name);
1515 +
1516 + # save as filename, even when no $file_name is given
1517 + my $save_as = "$hash";
1518 if (defined $file_name) {
1519 $save_as = $file_name;
1520 + } elsif ($type =~ m/^text\//) {
1521 + $save_as .= '.txt';
1522 }
1490 - print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"$save_as\"");
1491 - open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1523 +
1524 + print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1525 undef $/;
1526 + binmode STDOUT, ':raw' unless $type =~ m/^text\//;
1527 print <$fd>;
1528 + binmode STDOUT, ':utf8' unless $type =~ m/^text\//;
1529 $/ = "\n";
1530 close $fd;
1531 }