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).
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.
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.
25 sub fix_print_and_internal_encoding_after_0_68 {
26 return if version->parse($FCGI::VERSION) <= version->parse("0.68");
28 my $encoder = Encode::find_encoding('UTF-8');
29 my $original_fcgi_print = \&FCGI::Stream::PRINT;
31 no warnings 'redefine';
33 *FCGI::Stream::PRINT = sub {
34 if (!$::locale || !$::locale->raw_io_active) {
36 my @vals = map { $encoder->encode($_, Encode::FB_CROAK|Encode::LEAVE_SRC) } @_;
40 goto $original_fcgi_print;
45 fix_print_and_internal_encoding_after_0_68();