]> Lady’s Gitweb - Gitweb/blobdiff - gitweb.perl
gitweb: fix UTF-8 encoding when using CGI::Fast
[Gitweb] / gitweb.perl
index 2e0de7e530e6424a09cf0effae37a7cd0b73c6d80c64c7188279446edef29867..7f9a7ddbbdf7904291e1b8e891ab4763c803331cf18fa28d69e682b67e743906 100755 (executable)
@@ -1292,9 +1292,23 @@ our $is_last_request = sub { 1 };
 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
 our $CGI = 'CGI';
 our $cgi;
 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
 our $CGI = 'CGI';
 our $cgi;
+our $FCGI_Stream_PRINT_raw = \&FCGI::Stream::PRINT;
 sub configure_as_fcgi {
        require CGI::Fast;
        our $CGI = 'CGI::Fast';
 sub configure_as_fcgi {
        require CGI::Fast;
        our $CGI = 'CGI::Fast';
+       # FCGI is not Unicode aware hence the UTF-8 encoding must be done manually.
+       # However no encoding must be done within git_blob_plain() and git_snapshot()
+       # which must still output in raw binary mode.
+       no warnings 'redefine';
+       my $enc = Encode::find_encoding('UTF-8');
+       *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 $FCGI_Stream_PRINT_raw;
+       };
 
        my $request_number = 0;
        # let each child service 100 requests
 
        my $request_number = 0;
        # let each child service 100 requests
@@ -7080,6 +7094,7 @@ sub git_blob_plain {
                        ($sandbox ? 'attachment' : 'inline')
                        . '; filename="' . $save_as . '"');
        local $/ = undef;
                        ($sandbox ? 'attachment' : 'inline')
                        . '; filename="' . $save_as . '"');
        local $/ = undef;
+       local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
        binmode STDOUT, ':raw';
        print <$fd>;
        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
        binmode STDOUT, ':raw';
        print <$fd>;
        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
@@ -7418,6 +7433,7 @@ sub git_snapshot {
 
        open my $fd, "-|", $cmd
                or die_error(500, "Execute git-archive failed");
 
        open my $fd, "-|", $cmd
                or die_error(500, "Execute git-archive failed");
+       local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
        binmode STDOUT, ':raw';
        print <$fd>;
        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
        binmode STDOUT, ':raw';
        print <$fd>;
        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
This page took 0.242108 seconds and 4 git commands to generate.