]> Sergey Matveev's repositories - public-inbox.git/commitdiff
cgi: be strict about UTF-8 encoding in HTML and XML
authorEric Wong <e@80x24.org>
Thu, 10 Apr 2014 19:26:40 +0000 (19:26 +0000)
committerEric Wong <e@80x24.org>
Fri, 11 Apr 2014 22:23:58 +0000 (22:23 +0000)
Hopefully this forces us to generate valid UTF-8 data.

public-inbox-cgi

index 91314f06836456414a172e7b0e47669f20a5ecb4..cd79a4a7961c102b5f1960eeaf012d0a9b2484aa 100755 (executable)
@@ -26,11 +26,10 @@ BEGIN {
        }
 }
 
-binmode STDOUT, ':utf8';
-
 my $ret = main();
 
 my ($status, $headers, $body) = @$ret;
+set_binmode($headers);
 if (@ARGV && $ARGV[0] eq 'static') {
        print $body;
 } else { # CGI
@@ -143,3 +142,13 @@ sub get_mid_txt {
        my $x = mid2blob($ctx);
        $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
 }
+
+sub set_binmode {
+       my ($headers) = @_;
+       if ($headers->{'Content-Type'} eq 'text/plain') {
+               # no way to validate raw messages, mixed encoding is possible.
+               binmode STDOUT;
+       } else { # strict encoding for HTML and XML
+               binmode STDOUT, ':encoding(UTF-8)';
+       }
+}