Unexpected output in browser with CGI::Fast to_json

99 Views Asked by At

I have a server with openSUSE Leap 15.3.

On this server I run Apache 2.4.43 with apache2-mod_fcgi.

I have the following program: test.fcgi

#!/usr/bin/perl
use JSON;
use CGI::Fast;

while (CGI::Fast->new) {
   print "Content-type: text/html; charset=utf-8\n\n";

   $array{strng} = "öäüöä";

   $strng_json1 = to_json (\%array);
   $strng_json2 = "{\"strng\":\"öäüöä\"}";

   if ($strng_json1 eq $strng_json2) {
      print "\$strng_json1 and \$strng_json2 are equal <br><br>";
   }

   print "\$strng_json1: $strng_json1 <br>";
   print "\$strng_json2: $strng_json2 <br>";
}

If I start this program in a browser I see the output:

$strng_json1 and $strng_json2 are equal

$strng_json1: {"strng":"öäüöä"}
$strng_json2: {"strng":"öäüöä"} 

What I don't understand is why, if $strng_json1 and $strng_json2 are equal, in the browser I see two different strings.

1

There are 1 best solutions below

0
ikegami On

You are dealing with a bug in FCGI (used by CGI::Fast), and bugs of your own. Fix yours by adding use utf8; and encoding your output. (Normally, I'd suggest using use open ":std", ":encoding(UTF-8)"; to handle the latter, but that won't work here.) The bug in FCGI is unlikely to bother you then.


Test your solution with

  • A data structure that contains a string that contains characters > 0xFF.
  • A data structure that contains a string that contains characters > 0x7F, but none that contains character > 0xFF.