Subject: Gitweb running as FCGI does not print its output in UTF-8

From: Sergio Talens-Oliag <[email-masked]>
Date: Thu, 20 Dec 2012 14:53:57 +0100

Hello, I'm sending this message to explain a problem I've found with
gitweb.cgi when running it using a call like the following:

    export FCGI_SOCKET_PATH="/run/gitweb.socket"
    gitweb.cgi --fcgi --nproc 2

I've fixed the problem for my installation usign a wrapper script, as
explained below.

I'm not sending a patch because I'm no expert in Perl and I'm unsure that
the solution I've found is the right one, but in the hope that my description
of the problem and the provided solution will be useful for others.

Problem description:

   When using gitweb.cgi in FCGI mode the answers from the script are returned
   using the wrong encoding (UTF-8 characters are printed as LATIN-1).
   
   It seems that the problem appears because the FCGI streams are implemented
   using the older stream API, TIEHANDLE and applying PerlIO layers using
   binmode() has no effect to them.

Applied solution:

   A solution similar to the use of binmode for the output stream is to
   redefine the FCGI::Stream::PRINT function to use UTF-8 as output encoding.

   To do it with minimal chages I've created a gitweb.cgi wrapper that
   redefines the function and runs the original script; I'm doing it like this
   to be able to use the packaged script until upstream includes a fix for the
   problem.

Wrapper code (uses /usr/share/gitweb/gitweb.cgi as the PATH for gitweb.cgi):

  #!/usr/bin/perl
  # gitweb.cgi wrapper that fixes the UTF-8 problem with fastcgi

  # Local redefinition of FCGI::Stream::PRINT
  use Encode;
  use FCGI;

  our $enc = Encode::find_encoding('UTF-8');
  our $org = \&FCGI::Stream::PRINT;
  no warnings 'redefine';

  local *FCGI::Stream::PRINT = sub {
    my @OUTPUT = @_;
    for (my $i = 1; $i < @_; $i++) {
      $OUTPUT[$i] = $enc->encode($_[$i], Encode::FB_CROAK|Encode::LEAVE_SRC);
    }
    @_ = @OUTPUT;
    goto $org;
  };

  # Execute original script
  do "/usr/share/gitweb/gitweb.cgi";

References:

   The applied solution has been found on the following StackOverflow
   question:

     http://stackoverflow.com/questions/5005104

Environment:

  The system I'm using is Debian Wheezy and I'm serving gitweb using nginx's
  fastcgi interface.

  The versions of the related debian packages are:
  
	Package: git
	Version: 1:1.7.10.4-1+wheezy1
	Package: libcgi-fast-perl
	Version: 5.14.2-16
	Package: libfcgi-procmanager-perl
	Version: 0.24-1
	Package: perl
	Version: 5.14.2-16

  To launch gitweb as fastcgi I'm using an init.d script that runs the wrapper
  script in the background as the `www-user` using a call similar to the
  following:

    export FCGI_SOCKET_PATH="/run/gitweb/socket"
    gitweb.cgi-wrapper --fcgi --nproc 2

Greetings,

  Sergio.

-- 
Sergio Talens-Oliag <[email-masked]>               <http://www.iti.es/>
Key fingerprint = FF77 A16B 9D09 FC7B 6656 CFAD 261D E19A 578A 36F2