Merge branch 'master' of ssh://lx-office.linet-services.de/~/lx-office-erp
[kivitendo-erp.git] / SL / FCGIFixes.pm
1 package SL::FCGIFixes;
2
3 use strict;
4
5 use Encode;
6 use FCGI;
7
8 # FCGI does not use Perl's I/O layer. Therefore it does not honor
9 # setting STDOUT to ":utf8" with "binmode".  Also FCGI starting with
10 # 0.69 implements proper handling for UTF-8 flagged strings -- namely
11 # by downgrading them into bytes. The combination of the two causes
12 # Lx-Office's way of handling strings to go belly up (storing
13 # everything in Perl's internal encoding and using Perl's I/O layer
14 # for automatic conversion on output).
15 #
16 # This workaround monkeypatches FCGI's print routine so that all of
17 # its arguments safe for "$self" are encoded into UTF-8 before calling
18 # FCGI's original PRINT function.
19 #
20 # However, this must not be done if raw I/O is requested -- e.g. when
21 # sending out binary data. Fortunately that has been centralized via
22 # Locale's "with_raw_io" function which sets a variable indicating
23 # that current I/O operations should be raw.
24
25 sub fix_print_and_internal_encoding_after_0_68 {
26   return if version->parse($FCGI::VERSION) <= version->parse("0.68");
27
28   my $encoder             = Encode::find_encoding('UTF-8');
29   my $original_fcgi_print = \&FCGI::Stream::PRINT;
30
31   no warnings 'redefine';
32
33   *FCGI::Stream::PRINT = sub {
34     if (!$::locale || !$::locale->raw_io_active) {
35       my $self = shift;
36       my @vals = map { $encoder->encode($_, Encode::FB_CROAK|Encode::LEAVE_SRC) } @_;
37       @_ = ($self, @vals);
38     }
39
40     goto $original_fcgi_print;
41   };
42 }
43
44 sub apply_fixes {
45   fix_print_and_internal_encoding_after_0_68();
46 }
47
48 1;