{"thread":{"id":"32414","subject":"Gitweb running as FCGI does not print its output in UTF-8","startedAt":"2012-12-20T13:53:57Z","lastAt":"2012-12-20T13:53:57Z","messageCount":1,"participants":["Sergio Talens-Oliag"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"205253","messageId":"20121220135357.GA27920@iti.es","threadId":"32414","inReplyTo":null,"subject":"Gitweb running as FCGI does not print its output in UTF-8","fromName":"Sergio Talens-Oliag","fromEmail":"sto@iti.es","sentAt":"2012-12-20T13:53:57Z","receivedAt":"2012-12-20T13:53:57Z","isPatch":false,"sender":{"key":"sto@iti.es","avatar":null},"body":"Hello, I'm sending this message to explain a problem I've found with\ngitweb.cgi when running it using a call like the following:\n\n    export FCGI_SOCKET_PATH=\"/run/gitweb.socket\"\n    gitweb.cgi --fcgi --nproc 2\n\nI've fixed the problem for my installation usign a wrapper script, as\nexplained below.\n\nI'm not sending a patch because I'm no expert in Perl and I'm unsure that\nthe solution I've found is the right one, but in the hope that my description\nof the problem and the provided solution will be useful for others.\n\nProblem description:\n\n   When using gitweb.cgi in FCGI mode the answers from the script are returned\n   using the wrong encoding (UTF-8 characters are printed as LATIN-1).\n   \n   It seems that the problem appears because the FCGI streams are implemented\n   using the older stream API, TIEHANDLE and applying PerlIO layers using\n   binmode() has no effect to them.\n\nApplied solution:\n\n   A solution similar to the use of binmode for the output stream is to\n   redefine the FCGI::Stream::PRINT function to use UTF-8 as output encoding.\n\n   To do it with minimal chages I've created a gitweb.cgi wrapper that\n   redefines the function and runs the original script; I'm doing it like this\n   to be able to use the packaged script until upstream includes a fix for the\n   problem.\n\nWrapper code (uses /usr/share/gitweb/gitweb.cgi as the PATH for gitweb.cgi):\n\n  #!/usr/bin/perl\n  # gitweb.cgi wrapper that fixes the UTF-8 problem with fastcgi\n\n  # Local redefinition of FCGI::Stream::PRINT\n  use Encode;\n  use FCGI;\n\n  our $enc = Encode::find_encoding('UTF-8');\n  our $org = \\&FCGI::Stream::PRINT;\n  no warnings 'redefine';\n\n  local *FCGI::Stream::PRINT = sub {\n    my @OUTPUT = @_;\n    for (my $i = 1; $i < @_; $i++) {\n      $OUTPUT[$i] = $enc->encode($_[$i], Encode::FB_CROAK|Encode::LEAVE_SRC);\n    }\n    @_ = @OUTPUT;\n    goto $org;\n  };\n\n  # Execute original script\n  do \"/usr/share/gitweb/gitweb.cgi\";\n\nReferences:\n\n   The applied solution has been found on the following StackOverflow\n   question:\n\n     http://stackoverflow.com/questions/5005104\n\nEnvironment:\n\n  The system I'm using is Debian Wheezy and I'm serving gitweb using nginx's\n  fastcgi interface.\n\n  The versions of the related debian packages are:\n  \n\tPackage: git\n\tVersion: 1:1.7.10.4-1+wheezy1\n\tPackage: libcgi-fast-perl\n\tVersion: 5.14.2-16\n\tPackage: libfcgi-procmanager-perl\n\tVersion: 0.24-1\n\tPackage: perl\n\tVersion: 5.14.2-16\n\n  To launch gitweb as fastcgi I'm using an init.d script that runs the wrapper\n  script in the background as the `www-user` using a call similar to the\n  following:\n\n    export FCGI_SOCKET_PATH=\"/run/gitweb/socket\"\n    gitweb.cgi-wrapper --fcgi --nproc 2\n\nGreetings,\n\n  Sergio.\n\n-- \nSergio Talens-Oliag <sto@iti.es>               <http://www.iti.es/>\nKey fingerprint = FF77 A16B 9D09 FC7B 6656 CFAD 261D E19A 578A 36F2\n"}]}