]> Git — Sourcephile - sourcephile-nix.git/blob - overlays/gitweb/0001-gitweb-fix-UTF-8-encoding-when-using-CGI-Fast.patch
gitweb: patch gitweb directly
[sourcephile-nix.git] / overlays / gitweb / 0001-gitweb-fix-UTF-8-encoding-when-using-CGI-Fast.patch
1 From d13e561ee74fc926fc7ff285857ca58002e04dc1 Mon Sep 17 00:00:00 2001
2 From: Julien Moutinho <julm+git@sourcephile.fr>
3 Date: Sat, 28 Mar 2020 19:10:52 +0100
4 Subject: [PATCH] gitweb: fix UTF-8 encoding when using CGI::Fast
5
6 FCGI streams are implemented using the older stream API: TIEHANDLE,
7 therefore applying PerlIO layers using binmode() has no effect to them.
8 The solution in this patch is to redefine the FCGI::Stream::PRINT function
9 to use UTF-8 as output encoding, except within git_blob_plain() and git_snapshot()
10 which must still output in raw binary mode.
11
12 This problem and solution were previously reported back in 2012:
13 - http://git.661346.n2.nabble.com/Gitweb-running-as-FCGI-does-not-print-its-output-in-UTF-8-td7573415.html
14 - http://stackoverflow.com/questions/5005104
15
16 Signed-off-by: Julien Moutinho <julm+git@sourcephile.fr>
17 ---
18 gitweb/gitweb.perl | 16 ++++++++++++++++
19 1 file changed, 16 insertions(+)
20
21 diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
22 index 65a3a9e..83fd031 100755
23 --- a/gitweb/gitweb.perl
24 +++ b/gitweb/gitweb.perl
25 @@ -1291,9 +1291,23 @@ sub run_request {
26 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
27 our $CGI = 'CGI';
28 our $cgi;
29 +our $FCGI_Stream_PRINT_raw = \&FCGI::Stream::PRINT;
30 sub configure_as_fcgi {
31 require CGI::Fast;
32 our $CGI = 'CGI::Fast';
33 + # FCGI is not Unicode aware hence the UTF-8 encoding must be done manually.
34 + # However no encoding must be done within git_blob_plain() and git_snapshot()
35 + # which must still output in raw binary mode.
36 + no warnings 'redefine';
37 + my $enc = Encode::find_encoding('UTF-8');
38 + *FCGI::Stream::PRINT = sub {
39 + my @OUTPUT = @_;
40 + for (my $i = 1; $i < @_; $i++) {
41 + $OUTPUT[$i] = $enc->encode($_[$i], Encode::FB_CROAK|Encode::LEAVE_SRC);
42 + }
43 + @_ = @OUTPUT;
44 + goto $FCGI_Stream_PRINT_raw;
45 + };
46
47 my $request_number = 0;
48 # let each child service 100 requests
49 @@ -7079,6 +7093,7 @@ sub git_blob_plain {
50 ($sandbox ? 'attachment' : 'inline')
51 . '; filename="' . $save_as . '"');
52 local $/ = undef;
53 + local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
54 binmode STDOUT, ':raw';
55 print <$fd>;
56 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
57 @@ -7417,6 +7432,7 @@ sub git_snapshot {
58
59 open my $fd, "-|", $cmd
60 or die_error(500, "Execute git-archive failed");
61 + local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
62 binmode STDOUT, ':raw';
63 print <$fd>;
64 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
65 --
66 2.25.1
67