perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Warnier ...@ice-sa.com>
Subject Re: Charset in response
Date Thu, 29 Nov 2012 14:50:05 GMT
Torsten Förtsch wrote:
> 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.

I have to, to be able to be consistent in my string-replacement logic.

> 
>   $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.
> 

I'll try the above and let you know.

I guess that if I can do
open my $fh, '>:Apache2:encoding(...)', $r;
then $r, under the hood, must be some kind of filehandle too.
And then I could just do
binmode($r,":encoding($charset)");
but then, this being mod_perl, it may leave it that way and have unexpected side-effects 
somewhere else..


Mime
View raw message