Re: Charset in response

Torsten Förtsch <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
On 11/29/2012 10:37 AM, André Warnier wrote:
> When I say that it doesn't work, I mean in fact :
> - the "Content-Type" response header sent by the server is properly set
> according to what I do above (as verified in a browser plugin)
> - but if what I print contains "accented" characters, they are not being
> encoded properly
> 
> So, do I need to set something else so that the $r->print(string) will
> output "string" properly ?
> 
> 
> Background :
> 
> My PerlResponseHandler reads a html file from disk, replaces some
> strings into it, and sends the result out via $r->print.
> The source html file can be encoded in iso-8859-1 or UTF-8, and it
> contains a proper declaration of the charset under which it is really
> encoded :
> 
> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
> or
> <meta http-equiv="content-type" content="text/html; charset=UTF-8">
> 
> To read the file, I first open it "raw", read a few lines, checking for
> the above <meta> tag.  If found, I note the charset (say in $charset),
> close the file, and re-open it as
> 
> open(my $fh,"<:encoding($charset)", $file);
> 
> (note : if $charset is "UTF-8", then the open becomes
> open(my $fh,'<:utf8', $file);)

So, you convert the octet stream into a character stream when you read
the file. You have to do the reverse when you write it.

  $r->print(Encode::encode $encoding, $string);

Modperl usually uses perlio. So, perl-script handler should be able to
push an encoding layer on top of the :Apache2 layer.

  binmode STDOUT, ':encoding(...)'

But I haven't tried this yet.

Now, that I think of it, perhaps even the following would work

  open my $fh, '>:Apache2:encoding(...)', $r;
  print $fh $string;

If it does not work it would be good to make it so.

Torsten
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.