Skip to content

Commit 7a8bb6d

Browse files
committed
Merge branch 'jm/gitweb-fastcgi-utf8'
Gitweb update. * jm/gitweb-fastcgi-utf8: gitweb: fix UTF-8 encoding when using CGI::Fast
2 parents 7d28d69 + 2ecfcde commit 7a8bb6d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

gitweb/gitweb.perl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,9 +1291,23 @@ sub run_request {
12911291
our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
12921292
our $CGI = 'CGI';
12931293
our $cgi;
1294+
our $FCGI_Stream_PRINT_raw = \&FCGI::Stream::PRINT;
12941295
sub configure_as_fcgi {
12951296
require CGI::Fast;
12961297
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+
};
12971311

12981312
my $request_number = 0;
12991313
# let each child service 100 requests
@@ -7079,6 +7093,7 @@ sub git_blob_plain {
70797093
($sandbox ? 'attachment' : 'inline')
70807094
. '; filename="' . $save_as . '"');
70817095
local $/ = undef;
7096+
local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
70827097
binmode STDOUT, ':raw';
70837098
print <$fd>;
70847099
binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
@@ -7417,6 +7432,7 @@ sub git_snapshot {
74177432

74187433
open my $fd, "-|", $cmd
74197434
or die_error(500, "Execute git-archive failed");
7435+
local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
74207436
binmode STDOUT, ':raw';
74217437
print <$fd>;
74227438
binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi

0 commit comments

Comments
 (0)