From b521048fd64acf14af837f043332d5b2c75392c3 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Fri, 17 Apr 2015 09:43:58 -0700 Subject: [PATCH] gitweb: detect correct type for blobs < 512 bytes The Perl -T/-B file operators return a wrong result when used on a pipe that delivers less than 512 bytes before reaching end of file unless the perlio layer is present. Modern Perls generally are configured to use the perlio layer by default. However, some older Perls are configured to use the stdio layer by default instead. When dealing with blob data, check to see if the perlio layer is present and add it if not so that we get the correct -T result for small text blobs and do not send them back as an application/octet-stream blob_plain file download. Signed-off-by: Kyle J. McKay --- gitweb/gitweb.perl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index c041019136..8fa4637c5f 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -3913,6 +3913,18 @@ sub blob_mimetype { my $filename = shift; my $mime; + # The -T/-B file operators produce the wrong result unless a perlio + # layer is present when the file handle is a pipe that delivers less + # than 512 bytes of data before reaching EOF. + # + # If we are running in a Perl that uses the stdio layer rather than the + # unix+perlio layers we will end up adding a perlio layer on top of the + # stdio layer and get a second level of buffering. This is harmless + # and it makes the -T/-B file operators work properly in all cases. + + binmode $fd, ":perlio" or die_error(500, "Adding perlio layer failed") + unless grep /^perlio$/, PerlIO::get_layers($fd); + $mime = mimetype_guess($filename) if defined $filename; if (!$mime && $filename) { -- 2.11.4.GIT