@matheusbsbs / matheus-bagliano / commits / 2ecfcdecc6
Fork 0

gitweb: fix UTF-8 encoding when using CGI::Fast

FCGI streams are implemented using the older stream API: TIEHANDLE, therefore applying PerlIO layers using binmode() has no effect to them. The solution in this patch is to redefine the FCGI::Stream::PRINT function to use UTF-8 as output encoding, except within git_blob_plain() and git_snapshot() which must still output in raw binary mode. This problem and solution were previously reported back in 2012: - http://git.661346.n2.nabble.com/Gitweb-running-as-FCGI-does-not-print-its-output-in-UTF-8-td7573415.html - http://stackoverflow.com/questions/5005104 Signed-off-by: Julien Moutinho <julm+git@sourcephile.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Julien Moutinho committed Mar 29, 2020 at 01:20 UTC 2ecfcdecc612505291cfdffb886b5d7615c3e581
1 file changed +16
gitweb/gitweb.perl
+16
@@ -1291,9 +1291,23 @@ our $is_last_request = sub { 1 };
1291 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1292 our $CGI = 'CGI';
1293 our $cgi;
1294 +our $FCGI_Stream_PRINT_raw = \&FCGI::Stream::PRINT;
1295 sub configure_as_fcgi {
1296 require CGI::Fast;
1297 our $CGI = 'CGI::Fast';
1298 + # FCGI is not Unicode aware hence the UTF-8 encoding must be done manually.
1299 + # However no encoding must be done within git_blob_plain() and git_snapshot()
1300 + # which must still output in raw binary mode.
1301 + no warnings 'redefine';
1302 + my $enc = Encode::find_encoding('UTF-8');
1303 + *FCGI::Stream::PRINT = sub {
1304 + my @OUTPUT = @_;
1305 + for (my $i = 1; $i < @_; $i++) {
1306 + $OUTPUT[$i] = $enc->encode($_[$i], Encode::FB_CROAK|Encode::LEAVE_SRC);
1307 + }
1308 + @_ = @OUTPUT;
1309 + goto $FCGI_Stream_PRINT_raw;
1310 + };
1311
1312 my $request_number = 0;
1313 # let each child service 100 requests
@@ -7079,6 +7093,7 @@ sub git_blob_plain {
7093 ($sandbox ? 'attachment' : 'inline')
7094 . '; filename="' . $save_as . '"');
7095 local $/ = undef;
7096 + local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
7097 binmode STDOUT, ':raw';
7098 print <$fd>;
7099 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
@@ -7417,6 +7432,7 @@ sub git_snapshot {
7432
7433 open my $fd, "-|", $cmd
7434 or die_error(500, "Execute git-archive failed");
7435 + local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
7436 binmode STDOUT, ':raw';
7437 print <$fd>;
7438 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi