You are viewing a plain text version of this content. The canonical link for it is here.
Posted to apreq-dev@httpd.apache.org by Joe Schaefer <jo...@sunstarsys.com> on 2004/10/05 18:20:46 UTC
Re: Apache::Request, APR::Table and UTF8
Boris Zentner <bz...@2bz.de> writes:
[...]
> In my case I get data via POST requests, where the data is partly in utf8
> _sometimes_. So My initiate idea was to scan for that data and set the utf8
> flag where needed.
>
> my $t = $apr->params;
> for( values %$t ) {
> if ( $something ) {
> Encode::_utf8_on($_);
> } elsif ( $something_else ) {
> $_ = Encode::decode( 'iso-8859-1', $_ );
> }
> # else data is already in good shape.
> }
>
> It is important that the flag is now correctly delivered since the
> modules that get the data are out of my control. Any other solution is
> highly error-prone. And wrong since length, ord, chr, substr, index,
> chop, regex and more work different now.
>
> Perl > 5.6.0 do the utf8 conversion automatic so there is no other
> option that supporting this flag. So that $t->get/set and $t->do( )
> work.
>
> It is just one bit that needs to be reserved for any value in the table.
Any opinions out there? As I mentioned to Boris on dev@perl, we used to
have a charset attribute for apreq_value_t. I think it might be
worthwhile to resurrect it in some limited form. That way we have room
for improvement within apreq2, instead of delaying charset support until
apreq3.
We could use three-bit field for marking the charset:
0 - unknown
1 - ASCII
2 - UTF-8
3 - UTF-16
[ room for 4 more iso? charsets ]
and the current code could just set the charset to 0 - unknown.
That would leave a little room (at least 5 bits) for future expansion
within the apreq_value_t. We could use that room for other features,
like marking SvTAINT on a per-value level instead of per-table.
Thoughts?
--
Joe Schaefer
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Am 06.10.2004 um 00:56 schrieb Stas Bekman:
> Boris Zentner wrote:
>> Hi,
>> Am 05.10.2004 um 18:34 schrieb David Wheeler:
>>> On Oct 5, 2004, at 9:20 AM, Joe Schaefer wrote:
>>>
>>>> We could use three-bit field for marking the charset:
>>>>
>>>> 0 - unknown
>>>> 1 - ASCII
>>>> 2 - UTF-8
>>>> 3 - UTF-16
>>>> [ room for 4 more iso? charsets ]
>
> So once these flags are there, it's a total user's responsibility to
> do the right thing, correct?. I was just wondering whether we should
I think, the user can act on the information, but the perlglue in
Apache::Request especial any set and get function set or clears the
perlutf8 flag. Which is off by default, but if Perl puts something into
the table the flag may be set and delivered back by get.
In my opinion Apache::Table has to do this, but for my purpose it is
enough if Apache::Request do it.
For Apache::Table I do not know how to handle the merge compress and
overlap functions with different charsets.
> do that in the APR::Table instead, if it's going to serve other
> purposes. Of course if this is only really important for
> Apache::Request, then that's probably the best place to do that.
>
> Of course users can store the string with the flag by themselves, by
> simply storing: "$flag" . "string" and get it back as they have stored
> it.
>
>
> --
> __________________________________________________________________
> Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
> http://stason.org/ mod_perl Guide ---> http://perl.apache.org
> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> http://modperlbook.org http://apache.org http://ticketmaster.com
>
>
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi,
Am 06.10.2004 um 02:42 schrieb Stas Bekman:
> David Wheeler wrote:
>> On Oct 5, 2004, at 4:55 PM, Stas Bekman wrote:
>>> What you and Boris say makes perfect sense, but we certainly don't
>>> want to try to support all possible formats out there and try to
>>> handle those on behalf of users. If we do we may need to spawn
>>> APR::Table to be a project on its own with its own dedicated
>>> developers team. this is something suitable for Apache::Request
>>> which deals with a sub-set of formats, it's not suitable for
>>> APR::Table which is a general purpose thing.
>> Yes, I'm not talking about remembering what character set a string is
>> in, only about preserving the value of the Perl utf8 flag.
>
> OK, how about this idea: reserve the first char for a perl flags only,
> so if I have:
>
> perl -MDevel::Peek -le '$x = "\x{f6}\x{e4}\x{fc}"; Dump $x'
> SV = PV(0x804cef8) at 0x80656d8
> REFCNT = 1
> FLAGS = (POK,pPOK)
> PV = 0x805f540 "\366\344\374"\0
> CUR = 3
> LEN = 4
>
> sv_flags are:
>
> struct STRUCT_SV { /* struct sv { */
> void* sv_any; /* pointer to something */
> U32 sv_refcnt; /* how many references to us */
> U32 sv_flags; /* what we are */
> };
>
> so it won't fit in one char, which is only 8 bit. Should we want to
> store any other perl flags other than UTF8?
>
The taint flag.
> Notice that my proposal is different from Joe's since I don't suggest
> to deal with charsets, but only preserve certain bits from the perl
> flags as Boris and David suggest.
>
> Will that create a mess for Apache::Request table subclass Joe?
>
> So on get() the SvPV will point to the 1st char instead of 0th, and
> the added overhead will be to allocate one more char, and set it at
> create time and read it and adjust the SV flag on get.
>
This does all I want it to do. As long as all get, set, do ...
functions honor the flags.
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi,
Am 06.10.2004 um 19:37 schrieb Geoffrey Young:
>
>> I think that Boris wants it to not be the user's responsibility, and
>> I'm
>> inclined to agree with him.
>
> I'm still seeing it as the responsibility of the user here. the user
> best
> knows the kind of data they expect in their application. pushing off
> the
That is not true, Im sure you did not feel a need since your data is ok
only by luck. utf8 and usascii is equal on the first 128 chars. A
application can not know if the data in the table of a inherited object
is utf8 or not they mean different things. A user has not even to know
if the data store is inherited by APR::Table or not. It may work as
glue to apache, but anything that is used by a usermodule can not rely
on this.
Perl do the conversion depending on utf8 flag so the loose of the flag
force perl to do the conversion again.
Perl do the conversion to utf8 automatic, so all results on utf8 data
from the table and real utf8 data give wrong results.
use Encode;
$\ = $/;
$x = chr(0x2624); # make x utf8 string with 1 char.
print "\$x length: ", length $x;
$y = $x; # copy x
Encode::_utf8_off($y); # simulate store get into APR::Table
print "\$y length: ", length $y;
$z = $x . $y;
print "\$z length: ", length $z;
Encode::_utf8_off($z); # simulate store get into APR::Table
print "\$z length: ", length $z;
__OUTPUT__
$x length: 1
$y length: 3
$z length: 4
$z length: 9
The correct result is:
$x length: 1
$y length: 1
$z length: 2
$z length: 2
But thats not all, nothing work correct. substr index chop s/// m//
rindex length ...
> responsibility of interpreting application data (charsets,
> taintedness, etc)
Thats not my intention. The charsets are different from taintedness and
utf8. Lets separate the charsets from the flags. My interest is only in
the utf8 flag, but the taintness flag has the same issue. I do _not_
want any interpretation of any flag, I just want it back if I put it in
*no* data change at all.
The flags belongs to the values. So if the values are moved somehow or
copied the values must have there flags.
# $t is a table object.
@values = values %$t;
$result = dosomething(\@values);
> to APR::Table or libapreq just seems like a huge mistake to me when
> you look
> at these libraries as bridges to an interface that is clearly non-perl.
>
It is up to you to make a decision on the flags. For me, is clear,
currently I need to copy the data a second time to overcome the loose
of the flags. This is bad and slower as a CGI object from the
beginning. This may force me longterm to remove my requirement of
libapreq2 and mod_perl toward CGI and mod_cgi or another persistent
solution.
> anyway, I don't mean to rant on, and I'll admit that I'm far from an
> expert
> in dealing with unicode nuances. but I see some kind of scope creep
> going
> on here that has me a bit, well, concerned for the future of what used
> to be
> a nice, simple interface.
>
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by Geoffrey Young <ge...@modperlcookbook.org>.
Joe Schaefer wrote:
> Stas Bekman <st...@stason.org> writes:
>
> [...]
>
>
>>OK, how about this idea: reserve the first char for a perl flags only,
>
>
> Will apache understand what you're doing with the values in APR::Table?
> If you start embedding special characters in the value, won't that cause
> a problem for folks that use APR::Table to set response headers?
>
> If so, I'm against such a change in mod_perl. APR::Table is chiefly
> for working with HTTP headers, which are US-ASCII. I don't consider
> the lack of additional charset support to be a bug in APR::Table.
I strongly agree here.
>
> [...]
>
>
>>Will that create a mess for Apache::Request table subclass Joe?
>
>
> Probably not, since we override almost everything in APR::Table.
> I'm worried more about how apache will deal with it.
which is the point I raised last week (or was it the week before?) over on
dev@modperl, so I agree here too.
which is a good time to comment on a comment david made:
> For the same reason that when I store a reference I
> don't want to get back a string such as "HASH(0x800368)",
it's been a long-standing understanding/tradition that if you try to store a
reference to a hash in an apache table this is _exactly_ what will happen.
the Apache::Table/APR::Table interface has never promised to dereference
variables for users. we have a non-apache/perl-only interface for this kind
of thing (which has come up already :)
> when I store a
> string that's got the utf8 flag enabled, I want to get it back that way.
and for that same reason as with the hashref, I'm inclined to think that
utf8 data should be handled the same way. I think we need to keep true to
the perspective that APR::Table is merely an interface into the underlying C
code - I'm not so sure we should expect it to have the kind of magical
properties I think people are proposing.
separate from this is whether utf8 data coming from a POST is capable of
being handled correctly via libapreq. while I don't think any embedded logic
belongs in the underlying APR::Table abstraction, I'll defer to joe if he
thinks logic is required in libapreq. but personally I haven't seen a
persuasive case for this:
> I think that Boris wants it to not be the user's responsibility, and I'm
> inclined to agree with him.
I'm still seeing it as the responsibility of the user here. the user best
knows the kind of data they expect in their application. pushing off the
responsibility of interpreting application data (charsets, taintedness, etc)
to APR::Table or libapreq just seems like a huge mistake to me when you look
at these libraries as bridges to an interface that is clearly non-perl.
anyway, I don't mean to rant on, and I'll admit that I'm far from an expert
in dealing with unicode nuances. but I see some kind of scope creep going
on here that has me a bit, well, concerned for the future of what used to be
a nice, simple interface.
--Geoff
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 8, 2004, at 1:40 AM, Boris Zentner wrote:
> I agree with everything in this mail. I swallow/delay my suggestions
> on the charset issue, since people confuses it with the utf8 issue to
> much.
I think what we need comes down to this: Where it makes sense for a
Perl module to store Perl values and return them to a client, it should
preserve the flags on those Perl variables as much as possible. Flags
include "ut8" and "taint". Where it doesn't make sense (because the
underlying implementation isn't designed to support anything other than
simple ASCII strings, for example), it shouldn't.
N'est pas?
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hello,
Am 08.10.2004 um 03:31 schrieb Joe Schaefer:
> Boris Zentner <bz...@2bz.de> writes:
>> Am 08.10.2004 um 00:29 schrieb Markus Wichitill:
>
> [...]
>> Thats right, I think a perl library should support perl where you
>> think in terms of C.
>
> Please keep in mind that libapreq is, first and foremost, a C library.
> That's why this project is under the Apache Server Project, not the
> mod_perl project. libapreq1 is the main parser library for mod_perl,
> mod_dtcl and mod_ruby. Someday I hope libapreq2 gets an even wider
> variety of language bindings, but keep in mind that we already have
> folks here that use libapreq2 in C-only applications, e.g. Bojan's
> mod_spin. libapreq2 is designed so that all such apps are able to
> share the parsed data with each other.
>
I was not refering to libapreq2, I mean the glue to perl should support
perl as much as it can, Not only wrap to the functions of the library.
> So when evaluating a new feature, it's important to determine where
> its value lies among the entire user community. I believe charset
> support will become increasingly important over the expected lifespan
> of libapreq2, so it would be wise to plan ahead for that. Because
> once we release 2.05, we're stuck with whatever ABI it has.
I agree with everything in this mail. I swallow/delay my suggestions on
the charset issue, since people confuses it with the utf8 issue to
much.
>
> --
> Joe Schaefer
>
>
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by Joe Schaefer <jo...@sunstarsys.com>.
Boris Zentner <bz...@2bz.de> writes:
> Hi,
>
> Am 08.10.2004 um 00:29 schrieb Markus Wichitill:
[...]
> > We seem to mainly differ about whose responsibility it is to handle
> > the UTF-8 issue, the developer's who has all the relevant info, or
> > that of a thin XS layer over Apache internals, which doesn't know
> > anything about the context of the incoming data.
> >
>
> Thats right, I think a perl library should support perl where you
> think in terms of C.
Please keep in mind that libapreq is, first and foremost, a C library.
That's why this project is under the Apache Server Project, not the
mod_perl project. libapreq1 is the main parser library for mod_perl,
mod_dtcl and mod_ruby. Someday I hope libapreq2 gets an even wider
variety of language bindings, but keep in mind that we already have
folks here that use libapreq2 in C-only applications, e.g. Bojan's
mod_spin. libapreq2 is designed so that all such apps are able to
share the parsed data with each other.
So when evaluating a new feature, it's important to determine where
its value lies among the entire user community. I believe charset
support will become increasingly important over the expected lifespan
of libapreq2, so it would be wise to plan ahead for that. Because
once we release 2.05, we're stuck with whatever ABI it has.
--
Joe Schaefer
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi,
Am 08.10.2004 um 00:29 schrieb Markus Wichitill:
[...]
> A light-weight library like apreq should not try to handle every
> possible format under the sun, that's why I've already cautioned
> against linking in full XML support.
>
But I do not want any handling, just preventing the flag if the data is
touched from perl itself. And even the the data itself is not changed.
I just want the utf8 flag saved. And moved around if I move the data
from within perl.
>> Also I can think of the DECODE_UTF8 flag from your example as a
>> utf8-flag-on-for-all parameters in the table.
>
> Technically utf8::decode doesn't set the flag for 7-bit strings.
That does not matter, since the data is the same set or not.
>
>> I understand, that APR::Table can not change it's current behavior.
>> But Apache::Request can and should. I'm really frightened that so
>> much emails and examples do not convert you all on how important
>> correct data for perl is.
>
> We seem to mainly differ about whose responsibility it is to handle
> the UTF-8 issue, the developer's who has all the relevant info, or
> that of a thin XS layer over Apache internals, which doesn't know
> anything about the context of the incoming data.
>
Thats right, I think a perl library should support perl where you think
in terms of C.
> BTW, I think you make things look a bit too simple in your first post
> by hiding much of the real complexity behind $something and
> $something_else. I'm still not sure if you expect Apache::Request to
> do heuristic scanning of the input to automatically determine the
> encoding (which would be unreliable at best)?
The $something and $something_else example use keys and/or values and
so loose the context from key to value, so a application can not know
which data is utf8 and which not.
>
>> Again, no conversion just back what I put in that is the minimum
>> requirement for any data-store.
>
> Stop thinking of those tables as data stores, they're APIs to a
> webserver that doesn't really support Unicode. Of course I might be
> biased, since I've always treated Apache::Request as a read-only
> object anyway.
But It is allowed to put data in and I use that a lot. Also this
explain why you can use decode since your data is never changed. In my
case a result may replace the data again and again. And get wrong and
wronger with every store.
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by Markus Wichitill <ma...@gmx.de>.
Boris Zentner wrote:
>>> A simple form of UTF-8 support in Apache::Request that I wouldn't mind
>>> would be a flag "DECODE_UTF8 => 1", that when passed to new() would
>>> cause Apache::Request to call utf8::decode on the returned string every
>>> time param(), body() etc. is called, but would leave the original
>
> This is not possible. The reason is that if you call decode for every
> parameter, *ALL* parameters must be in utf8. That is not true.
As I said, I know this simple all-or-nothing approach is not the solution
you want, but it's certainly "possible" and a solution that would be enough
for most applications. I don't know what kind of mixed input your
application receives in a single request, but in the common case of a
browser-submitted form, it's either all UTF-8 or not, depending on the
encoding of the HTML page (or maybe the accept-charset attribute, if any
browsers support that).
My stance here is that your application with mixed input is rather specific
if not unusual, and therefore needs to do its own handling of the issue,
even if that means you have to do more wrapping/subclassing and have to
educate your co-developers about how to use the resulting interfaces. Which
you already did.
A light-weight library like apreq should not try to handle every possible
format under the sun, that's why I've already cautioned against linking in
full XML support.
> Also I can think of the DECODE_UTF8 flag from your example as a
> utf8-flag-on-for-all parameters in the table.
Technically utf8::decode doesn't set the flag for 7-bit strings.
> I understand, that APR::Table can not change it's current behavior. But
> Apache::Request can and should. I'm really frightened that so much
> emails and examples do not convert you all on how important correct data
> for perl is.
We seem to mainly differ about whose responsibility it is to handle the
UTF-8 issue, the developer's who has all the relevant info, or that of a
thin XS layer over Apache internals, which doesn't know anything about the
context of the incoming data.
BTW, I think you make things look a bit too simple in your first post by
hiding much of the real complexity behind $something and $something_else.
I'm still not sure if you expect Apache::Request to do heuristic scanning of
the input to automatically determine the encoding (which would be unreliable
at best)?
> Again, no conversion just back what I put in that is the minimum
> requirement for any data-store.
Stop thinking of those tables as data stores, they're APIs to a webserver
that doesn't really support Unicode. Of course I might be biased, since I've
always treated Apache::Request as a read-only object anyway.
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi,
Am 08.10.2004 um 00:54 schrieb David Wheeler:
> On Oct 7, 2004, at 3:37 PM, Boris Zentner wrote:
>
>> Thats exactly how my application work. Internaly all data is in the
>> same charset for perl 5.6.1 and all is utf8 for perl >= 5.8.0.
>
> Bricolage, too.
>
>> The whole discussion came on since I do the conversion from
>> Apache::Request's byte strings to perls utf8 for most but not all
>> bytestrings. Then _all_ other application parts have no need to ever
>> bother with any conversion. But that is not all, other parts can also
>> insert data into the object ( utf8 or not).
>
> Not Bricolage. It all has to be utf8 or it's not a string. It's just
> easier not to mix things up.
I have no control over all parts, but there is no problem only, when
the data is back without the flag of course.
> [...]
>
>> Uhh, I do this already, but in fact I replace Apache::Request with
>> something similar, so why using Apache::Request?
>
> I don't understand. Did you subclass it or replace it with something
> else? There's a very big difference--subclassing is much less work!
>
subclass, I thought on getting rid of Apache::Request.
>>> I think that this may not be possible given what you submit to the
>>> browser and get back from the browser is not Perl data (e.g.,
>>> params). But pnotes(), for example, should do the right thing.
>>
>> Hmm, look:
>>
>> 1. the data touch my server all data is a sequence of bytes.
>> 2. Apache::Request put this into a table for me. ( the data is
>> untouched utf8 or not )
>> 3. my module inherited from Apache::Request convert the utf8 data to
>> utf8 with the flag inside the inherited new.
>>
>> my $c = 0; # a counter to convert only some data to utf8
>> for ( values %$t ) {
>> $_ = Encode::encode_utf8($_) if ( $c++ & 1 );
>> }
>>
>> I need only these lines in step three and all other modules can use
>> the Apache::Request object and get straight data. Otherwise I need to
>> copy the data to maybe pnotes.
>
> So you use your subclass where you need utf8 and use Apache::Request
> itself where you don't. No?
I use Apache::Request, if I do not feel to change something. Otherwise
I use a CGI like data container.
>
>> a $t->get should return now the correct data regardless if the flag
>> was on or off. A $t->set store the data ( the same data as it does
>> now ) but also the flag. If a $t->get read the data back the flag is
>> restored. If the data is passed to the next handler in mod_perl it
>> works as before ( it drops/ignores the flag, the data is the same ).
>
> I see. You're saying that because you basically store back to
> Apache::Request, you want it to remember what you've stored. But I
> don't think that Apache::Request was designed for storing data, just
> for fetching request data and sending response data.
>
Yes.
If a store is not allowed, I'm really wrong and apologize for all the
noise!
> Can you show the actual code?
>
Sure,
here is some. Have fun.
MyReq
|
Apache::Request::KL
|
Apache::Request
The KL::Param is only a fallback Data container, I think it is
exchangeable by CGI
I have no control over MyReq.pm and MyKL/Common.pm
###################################################################
package Apache::Request::KL;
use Apache::Request;
use APR::Table ();
our @ISA = qw(Apache::Request);
sub init { return shift }
sub new {
my $class = shift;
my $rr = shift;
my $self = bless { _r => Apache::Request->new($rr, @_) }, $class;
$self->init;
$self;
}
sub _get_params_table {
my $self = shift;
return $self->{params_table} ||= $self->{_r}->params;
}
sub param {
my $self = shift;
my $ppo = $self->{param_obj};
if ( $ppo ) {
return wantarray ? @{[ $ppo->param(@_) ]} : scalar($ppo->param(@_));
}
my $t = $self->{params_table}
||= $self->{_r}->params;
if ( @_ == 0 ) {
my @keys = ();
$t->do( sub { push @keys, $_[0]; 1 } );
return wantarray ? (@keys) : scalar(@keys);
}
# only one argument
elsif ( @_ == 1 ) {
if (wantarray) {
my @list = $t->get(@_);
return @list;
}
else {
return $t->get(@_);
}
}
# insert something
else {
while ( @_ > 0 ) {
my ( $k, $v ) = splice @_, 0, 2;
if ( ref $v eq 'ARRAY' ) {
$t->unset($k);
$t->merge( $k => $v->[$_] ) for ( 0 .. $#$v );
}
elsif ( !defined($v) ) {
$t->unset($k);
}
else {
$t->set( $k => $v );
}
}
}
}
1;
########################################################################
####
package MyReq;
use strict;
use warnings;
use Encode;
use APR::Table;
use KL::Param;
our @ISA = 'Apache::Request::KL';
sub init {
my $self = shift;
my $type = $self->headers_in->{'Content-Type'};
return if !$type or $type ne 'application/x-www-form-urlencoded';
# no hint, no change!
my $hint = $self->param('hint');
return unless $hint;
use bytes;
my $t = $self->_get_params_table;
my ( $ppo, $k, $v );
if ( $hint eq $MyKL::Common::HINT_ISO_8859_1 ) {
$ppo = $self->{param_obj} = KL::Param->new;
while ( ( $k, $v ) = each %$t ) {
$ppo->param( $k,
$MyKL::Common::Skip{$k}
? $v
: Encode::decode( 'iso-8859-1', $v ) );
}
}
elsif ( $hint eq $MyKL::Common::HINT_UTF8 ) {
$ppo = $self->{param_obj} = KL::Param->new;
while ( ( $k, $v ) = each %$t ) {
Encode::encode_utf8($v);
$ppo->param( $k, $v );
}
}
return $self;
}
1;
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 7, 2004, at 3:37 PM, Boris Zentner wrote:
> Thats exactly how my application work. Internaly all data is in the
> same charset for perl 5.6.1 and all is utf8 for perl >= 5.8.0.
Bricolage, too.
> The whole discussion came on since I do the conversion from
> Apache::Request's byte strings to perls utf8 for most but not all
> bytestrings. Then _all_ other application parts have no need to ever
> bother with any conversion. But that is not all, other parts can also
> insert data into the object ( utf8 or not).
Not Bricolage. It all has to be utf8 or it's not a string. It's just
easier not to mix things up.
> Now I have a CGI like object, that is similar to pnotes inherited from
> Apache::Request the problem here is why use Apache::Request, if I have
> to use another tool anyway?
Well, I think it has been said that Joe will make sure that
Apache::Request (libapreq) will do the right thing with Perl variable
flags, incuding utf8. But in general, you shouldn't really need it. All
I use Apache::Request for is reading parameters and headers, setting
headers, and printing. So the only data I'm fetching from it is
params(), really, and that can be subclassed as Paul Lindner has
demonstrated. (And thanks for that link, Geoff--I wish I'd thought of
that!).
http://www.modperlcookbook.org/code/ch10/Cookbook/TransformRequest.pm
> Uhh, I do this already, but in fact I replace Apache::Request with
> something similar, so why using Apache::Request?
I don't understand. Did you subclass it or replace it with something
else? There's a very big difference--subclassing is much less work!
>> I think that this may not be possible given what you submit to the
>> browser and get back from the browser is not Perl data (e.g.,
>> params). But pnotes(), for example, should do the right thing.
>
> Hmm, look:
>
> 1. the data touch my server all data is a sequence of bytes.
> 2. Apache::Request put this into a table for me. ( the data is
> untouched utf8 or not )
> 3. my module inherited from Apache::Request convert the utf8 data to
> utf8 with the flag inside the inherited new.
>
> my $c = 0; # a counter to convert only some data to utf8
> for ( values %$t ) {
> $_ = Encode::encode_utf8($_) if ( $c++ & 1 );
> }
>
> I need only these lines in step three and all other modules can use
> the Apache::Request object and get straight data. Otherwise I need to
> copy the data to maybe pnotes.
So you use your subclass where you need utf8 and use Apache::Request
itself where you don't. No?
> a $t->get should return now the correct data regardless if the flag
> was on or off. A $t->set store the data ( the same data as it does now
> ) but also the flag. If a $t->get read the data back the flag is
> restored. If the data is passed to the next handler in mod_perl it
> works as before ( it drops/ignores the flag, the data is the same ).
I see. You're saying that because you basically store back to
Apache::Request, you want it to remember what you've stored. But I
don't think that Apache::Request was designed for storing data, just
for fetching request data and sending response data.
Can you show the actual code?
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi David,
Am 07.10.2004 um 23:52 schrieb David Wheeler:
> On Oct 7, 2004, at 1:31 PM, Boris Zentner wrote:
>
>> This is not possible. The reason is that if you call decode for every
>> parameter, *ALL* parameters must be in utf8. That is not true.
>> Also I can think of the DECODE_UTF8 flag from your example as a
>> utf8-flag-on-for-all parameters in the table. This is wrong for the
>> same reason as always off is wrong.
>
> Well, I suppose that depends on how you write your application.
> Bricolage, for example, is written in such a way that it expects
> request parameters to all be in the same character set. So to decode
> them all would be simple (and very cool).
>
Thats exactly how my application work. Internaly all data is in the
same charset for perl 5.6.1 and all is utf8 for perl >= 5.8.0.
The whole discussion came on since I do the conversion from
Apache::Request's byte strings to perls utf8 for most but not all
bytestrings. Then _all_ other application parts have no need to ever
bother with any conversion. But that is not all, other parts can also
insert data into the object ( utf8 or not).
Now I have a CGI like object, that is similar to pnotes inherited from
Apache::Request the problem here is why use Apache::Request, if I have
to use another tool anyway?
>> My current solution is subclass Apache::Request copy all data to a
>> CGI like object with utf8 conversion, where needed. Then all other
>> modules act only on the CGI object with the correct utf8 flags and
>> values. This is far better than any hacks for all on or all off
>> aproach.
>
> Yes, it would be cool and proper to have Apache::Request do UTF-8
> conversion, which I think a subclass would easily be able to do.
>
Uhh, I do this already, but in fact I replace Apache::Request with
something similar, so why using Apache::Request?
>> I understand, that APR::Table can not change it's current behavior.
>> But Apache::Request can and should. I'm really frightened that so
>> much emails and examples do not convert you all on how important
>> correct data for perl is.
>
> I think that we're all in agreement that perl variable flags should be
> preserved where the underlying code is designed to handle Perl data
> (such as pnote()), not just character data (as with APR::Table).
>
>> Again, no conversion just back what I put in that is the minimum
>> requirement for any data-store. There is no option, perl do it
>> automatic.
>
> I think that this may not be possible given what you submit to the
> browser and get back from the browser is not Perl data (e.g., params).
> But pnotes(), for example, should do the right thing.
Hmm, look:
1. the data touch my server all data is a sequence of bytes.
2. Apache::Request put this into a table for me. ( the data is
untouched utf8 or not )
3. my module inherited from Apache::Request convert the utf8 data to
utf8 with the flag inside the inherited new.
my $c = 0; # a counter to convert only some data to utf8
for ( values %$t ) {
$_ = Encode::encode_utf8($_) if ( $c++ & 1 );
}
I need only these lines in step three and all other modules can use the
Apache::Request object and get straight data. Otherwise I need to copy
the data to maybe pnotes.
a $t->get should return now the correct data regardless if the flag was
on or off. A $t->set store the data ( the same data as it does now )
but also the flag. If a $t->get read the data back the flag is
restored. If the data is passed to the next handler in mod_perl it
works as before ( it drops/ignores the flag, the data is the same ).
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 7, 2004, at 1:31 PM, Boris Zentner wrote:
> This is not possible. The reason is that if you call decode for every
> parameter, *ALL* parameters must be in utf8. That is not true.
> Also I can think of the DECODE_UTF8 flag from your example as a
> utf8-flag-on-for-all parameters in the table. This is wrong for the
> same reason as always off is wrong.
Well, I suppose that depends on how you write your application.
Bricolage, for example, is written in such a way that it expects
request parameters to all be in the same character set. So to decode
them all would be simple (and very cool).
> My current solution is subclass Apache::Request copy all data to a CGI
> like object with utf8 conversion, where needed. Then all other modules
> act only on the CGI object with the correct utf8 flags and values.
> This is far better than any hacks for all on or all off aproach.
Yes, it would be cool and proper to have Apache::Request do UTF-8
conversion, which I think a subclass would easily be able to do.
> I understand, that APR::Table can not change it's current behavior.
> But Apache::Request can and should. I'm really frightened that so much
> emails and examples do not convert you all on how important correct
> data for perl is.
I think that we're all in agreement that perl variable flags should be
preserved where the underlying code is designed to handle Perl data
(such as pnote()), not just character data (as with APR::Table).
> Again, no conversion just back what I put in that is the minimum
> requirement for any data-store. There is no option, perl do it
> automatic.
I think that this may not be possible given what you submit to the
browser and get back from the browser is not Perl data (e.g., params).
But pnotes(), for example, should do the right thing.
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi again,
Am 07.10.2004 um 20:50 schrieb Geoffrey Young:
>
>> A simple form of UTF-8 support in Apache::Request that I wouldn't mind
>> would be a flag "DECODE_UTF8 => 1", that when passed to new() would
>> cause Apache::Request to call utf8::decode on the returned string
>> every
>> time param(), body() etc. is called, but would leave the original
This is not possible. The reason is that if you call decode for every
parameter, *ALL* parameters must be in utf8. That is not true.
Also I can think of the DECODE_UTF8 flag from your example as a
utf8-flag-on-for-all parameters in the table. This is wrong for the
same reason as always off is wrong.
>> data/tables alone. That might be convenient when writing small
>> handlers
>> that don't warrant a library with wrapper functions etc.
>
> this sounds like a libapreq-1.XX wrapper that my coauthor paul wrote -
> IIRC
> he said he was using pretty much the exact same thing at critical path
> at
> the time.
>
> # the wrappers themselves
> http://www.modperlcookbook.org/code/ch10/Cookbook/TransformRequest.pm
> http://www.modperlcookbook.org/code/ch10/Cookbook/UTF8Request.pm
>
> # sample usage
> http://www.modperlcookbook.org/code/ch10/Cookbook/Japanese.pm
>
> I'm not sure how portable this is to libapreq2, but it can't be that
> far
> off. it also was quite a few years ago at this point, so you might
> want to
> do something more clever now :)
My current solution is subclass Apache::Request copy all data to a CGI
like object with utf8 conversion, where needed. Then all other modules
act only on the CGI object with the correct utf8 flags and values. This
is far better than any hacks for all on or all off aproach.
I understand, that APR::Table can not change it's current behavior. But
Apache::Request can and should. I'm really frightened that so much
emails and examples do not convert you all on how important correct
data for perl is.
Again, no conversion just back what I put in that is the minimum
requirement for any data-store. There is no option, perl do it
automatic.
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> A simple form of UTF-8 support in Apache::Request that I wouldn't mind
> would be a flag "DECODE_UTF8 => 1", that when passed to new() would
> cause Apache::Request to call utf8::decode on the returned string every
> time param(), body() etc. is called, but would leave the original
> data/tables alone. That might be convenient when writing small handlers
> that don't warrant a library with wrapper functions etc.
this sounds like a libapreq-1.XX wrapper that my coauthor paul wrote - IIRC
he said he was using pretty much the exact same thing at critical path at
the time.
# the wrappers themselves
http://www.modperlcookbook.org/code/ch10/Cookbook/TransformRequest.pm
http://www.modperlcookbook.org/code/ch10/Cookbook/UTF8Request.pm
# sample usage
http://www.modperlcookbook.org/code/ch10/Cookbook/Japanese.pm
I'm not sure how portable this is to libapreq2, but it can't be that far
off. it also was quite a few years ago at this point, so you might want to
do something more clever now :)
--Geoff
Re: Apache::Request, APR::Table and UTF8
Posted by Markus Wichitill <ma...@gmx.de>.
David Wheeler wrote:
> Yes, if APR::Table is designed specifically to support US-ASCII only,
> then it seem pointless to add this support. I mean, even if the utf8
> flag could be maintained, if the underlying Apache C code doesn't
> properly handle anything over the 256th byte because it doesn't support
> it (and so, theoretically, what it does with bytes over 256 is
> unpredictable),
Make sure to patent bytes > 256, sounds like there could be a lot of money
in those. SCNR ;)
Nah, those bytes would be treated the same as normal 8-bit characters (which
aren't allowed in headers either), that's the whole point of UTF-8. If
Apache is doing any length checks, it wouldn't count the length in logical
characters, but it wouldn't asplode or anything.
But I think that neither APR::Table nor Apache::Request need UTF-8 support,
it's easy enough to wrap/subclass those functions in do exactly what you
want in those wrappers.
Just don't stuff your own strings into APR::Tables, they're part of the
Apache API, which doesn't directly support UTF-8. They're not a Perl utility
class for general use, which indeed should support storing any special flags.
Some people might want to use utf8::decode on the input, others are afraid
of utf8::decode's experimental status and prefer Encode::decode_utf8, or are
crazy enough to call the faster Encode::_utf8_on (which might crash the
server or send it into an infinite loop if the data wasn't valid UTF-8).
Others again might want to use Unicode::Normalize::NFC on all parameter
strings after decoding. Too many options here for a one-size-fits-all
approach. Unicode is not simple.
The rather vague "$something" variables in the thread starter post even
sound like something like Encode::Guess might be needed, which is even more
outside of the responsibility of Apache::Request. The developer and specific
application has to know the encoding, heuristic scanning in Apache::Request
would slow and error-prone.
Regarding performance, using wrapper functions and/or storing your
parameters in a Perl hash might have a tiny performance hit, but "tiny" is
the keyword here, because who really uses more than half a dozen parameters
most of the time. I mainly use Apache::Request for the better performance
and less wasted memory with handling MFD uploads, not because of a
nanosecond more or less when parsing a query string.
On a related note, I was hoping for UTF-8 support in DBI, because I thought
calling utf8::decode for hundreds of fields in hashes returned by DBI might
be more of a performance issue than. But I've experimented with that since
then, and even calling utf8::decode for no less than ~700 fields only
reduced the overall performance from around 25 req/s (with all UTF-8
processing enabled) to 24 req/s (with all text being treated as 8-bit) in my
test case, and that also includes the total overhead of UTF-8 in Perl, in a
forum application that does lots of regex operations on those Unicode
strings. Pretty impressive I think.
A simple form of UTF-8 support in Apache::Request that I wouldn't mind would
be a flag "DECODE_UTF8 => 1", that when passed to new() would cause
Apache::Request to call utf8::decode on the returned string every time
param(), body() etc. is called, but would leave the original data/tables
alone. That might be convenient when writing small handlers that don't
warrant a library with wrapper functions etc. But I don't think that's what
Boris wants.
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 6, 2004, at 8:46 PM, Stas Bekman wrote:
>> Will apache understand what you're doing with the values in
>> APR::Table?
>> If you start embedding special characters in the value, won't that
>> cause a problem for folks that use APR::Table to set response
>> headers?
>
> Yes, of course. I haven't thought of that. So it's obviously a no go.
Yes, if APR::Table is designed specifically to support US-ASCII only,
then it seem pointless to add this support. I mean, even if the utf8
flag could be maintained, if the underlying Apache C code doesn't
properly handle anything over the 256th byte because it doesn't support
it (and so, theoretically, what it does with bytes over 256 is
unpredictable), then I'm happy to use other tools like pnotes() that
*will* do the right thing.
If notes() uses APR::Table, though, I think it would be helpful for the
documentation to mention that it supports US-ASCII only and to use
pnotes() if you need something else.
Regards,
David
(Who might need to rethink Apache::FakeTable as a result of all this...)
Re: Apache::Request, APR::Table and UTF8
Posted by Stas Bekman <st...@stason.org>.
Joe Schaefer wrote:
> Stas Bekman <st...@stason.org> writes:
>
> [...]
>
>
>>OK, how about this idea: reserve the first char for a perl flags only,
>
>
> Will apache understand what you're doing with the values in APR::Table?
> If you start embedding special characters in the value, won't that cause
> a problem for folks that use APR::Table to set response headers?
Yes, of course. I haven't thought of that. So it's obviously a no go.
Sorry, folks, there is not much we can do here.
Joe will handle it at the apreq level, but the core APR::Table is just not
suitable for anything but plain strings, that no need for extra flags
(since we can't store them). The only alternative I was mentioining before
is to decode the data to the bytecodes, but it's not efficient.
And yes it'd be nice to preserve the tainted flag too, but for the same
reason we can't.
As long as apreq has it resolved, there is no problem really, since nobody
forces you to use APR::Table for storing data. Use pnotes or any other
perl based storage instead.
Of course we ought to document that in the APR::Table manpage...
--
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
Re: Apache::Request, APR::Table and UTF8
Posted by Joe Schaefer <jo...@sunstarsys.com>.
Stas Bekman <st...@stason.org> writes:
[...]
> OK, how about this idea: reserve the first char for a perl flags only,
Will apache understand what you're doing with the values in APR::Table?
If you start embedding special characters in the value, won't that cause
a problem for folks that use APR::Table to set response headers?
If so, I'm against such a change in mod_perl. APR::Table is chiefly
for working with HTTP headers, which are US-ASCII. I don't consider
the lack of additional charset support to be a bug in APR::Table.
[...]
> Will that create a mess for Apache::Request table subclass Joe?
Probably not, since we override almost everything in APR::Table.
I'm worried more about how apache will deal with it.
--
Joe Schaefer
Re: Apache::Request, APR::Table and UTF8
Posted by Stas Bekman <st...@stason.org>.
David Wheeler wrote:
> On Oct 5, 2004, at 4:55 PM, Stas Bekman wrote:
>
>> What you and Boris say makes perfect sense, but we certainly don't
>> want to try to support all possible formats out there and try to
>> handle those on behalf of users. If we do we may need to spawn
>> APR::Table to be a project on its own with its own dedicated
>> developers team. this is something suitable for Apache::Request which
>> deals with a sub-set of formats, it's not suitable for APR::Table
>> which is a general purpose thing.
>
>
> Yes, I'm not talking about remembering what character set a string is
> in, only about preserving the value of the Perl utf8 flag.
OK, how about this idea: reserve the first char for a perl flags only, so
if I have:
perl -MDevel::Peek -le '$x = "\x{f6}\x{e4}\x{fc}"; Dump $x'
SV = PV(0x804cef8) at 0x80656d8
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x805f540 "\366\344\374"\0
CUR = 3
LEN = 4
sv_flags are:
struct STRUCT_SV { /* struct sv { */
void* sv_any; /* pointer to something */
U32 sv_refcnt; /* how many references to us */
U32 sv_flags; /* what we are */
};
so it won't fit in one char, which is only 8 bit. Should we want to store
any other perl flags other than UTF8?
Notice that my proposal is different from Joe's since I don't suggest to
deal with charsets, but only preserve certain bits from the perl flags as
Boris and David suggest.
Will that create a mess for Apache::Request table subclass Joe?
So on get() the SvPV will point to the 1st char instead of 0th, and the
added overhead will be to allocate one more char, and set it at create
time and read it and adjust the SV flag on get.
--
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 5, 2004, at 4:55 PM, Stas Bekman wrote:
> What you and Boris say makes perfect sense, but we certainly don't
> want to try to support all possible formats out there and try to
> handle those on behalf of users. If we do we may need to spawn
> APR::Table to be a project on its own with its own dedicated
> developers team. this is something suitable for Apache::Request which
> deals with a sub-set of formats, it's not suitable for APR::Table
> which is a general purpose thing.
Yes, I'm not talking about remembering what character set a string is
in, only about preserving the value of the Perl utf8 flag.
> I don't know if it's a good idea to have APR::Table to deal only with
> utf8 as the only special case. Joe?
Only if you consider a need to support any other Perl variable flags as
special cases. Again, I'm just thinking of the value of the flag, not
the encoding of the data in the string.
> sure, but you can make it a perl scalar.
>
> string "foo" on the way to the table:
>
> utf8 => "1foo";
> non-utf8 => "0foo"
>
> on the way out, split and check the first char, and set the flag
> accordingly. Won't that work?
Yes, but I don't want to have to do that work, just as I wouldn't want
to serialize and deserialize hash or array references to store them in
a table. You don't have to do that, do you?
>> # Store
>> while (my ($k, $v) = each %stringmap) {
>> $r->pnotes( $k => $v );
>> $r->pnotes( "$k.utf8" => Encode::is_utf8($v);
>> }
>> # Fetch:
>> my %stringmap;
>> while (my ($k, $v) = each %{ {$r->pnotes }}) {
>> next if $k =~ /\.utf8$/;
>> Encode::_utf8_on($v) if $r->pnotes("$k.utf");
>> $stringmap{$k} = $v;
>> }
>> This is an extreme example, but I think that it makes the case. apreq
>> has to be able to know what kind of scalar it has, anyway, so it
>> should be able to know if the string is decoded to Perl's internal
>> utf8 representation, and if so, make sure it keeps the utf8 flag on.
>
> You certainly have no need to do that since pnotes already preserve
> the perl flags.
Phew, that's nice! But I meant to apply it to APR::Table.
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi,
Am 06.10.2004 um 01:55 schrieb Stas Bekman:
> David Wheeler wrote:
>> On Oct 5, 2004, at 3:56 PM, Stas Bekman wrote:
>>> So once these flags are there, it's a total user's responsibility to
>>> do the right thing, correct?. I was just wondering whether we should
>>> do that in the APR::Table instead, if it's going to serve other
>>> purposes. Of course if this is only really important for
>>> Apache::Request, then that's probably the best place to do that.
>> I think that Boris wants it to not be the user's responsibility, and
>> I'm inclined to agree with him. (Holler if I'm misrepresenting your
>> position, Boris.) For the same reason that when I store a reference I
>> don't want to get back a string such as "HASH(0x800368)", when I
>> store a string that's got the utf8 flag enabled, I want to get it
>> back that way. And if I have an array of strings, some with the flag
>> set and some without, I don't know which is which and I don't want to
>> have to think about which is which.
>
> What you and Boris say makes perfect sense, but we certainly don't
> want to try to support all possible formats out there and try to
> handle those on behalf of users. If we do we may need to spawn
> APR::Table to be a project on its own with its own dedicated
> developers team. this is something suitable for Apache::Request which
> deals with a sub-set of formats, it's not suitable for APR::Table
> which is a general purpose thing.
>
> I've proposed to Boris to try to decode the utf8 variable back into
> the bytecodes string, before storing it in the table, but he doesn't
> want that, since it adds an unwanted overhead.
>
decode is not enough, I do not know when to decode. And if I store
somewhere when I have to decode or copy the data to a CGI param object
as I do currently, I lose a *the* big advantage of mod_perl for me, its
speed.
So if my application use libapreq to get the data and then copy it to a
CGI object why not using CGI in the first place?
> I don't know if it's a good idea to have APR::Table to deal only with
> utf8 as the only special case. Joe?
>
>>> Of course users can store the string with the flag by themselves, by
>>> simply storing: "$flag" . "string" and get it back as they have
>>> stored it.
>> The flag is not a Perl scalar. It's an attribute of the C struct for
>> the scalar variable (sorry if I'm mangling the terminology here, I'm
>> not a C hacker). So it would be very inefficient to have to store all
>> my strings in a table like this
>
> sure, but you can make it a perl scalar.
>
> string "foo" on the way to the table:
>
> utf8 => "1foo";
> non-utf8 => "0foo"
This is one quick idea, that I considered already. But CGI handles the
utf8 flag already correct.
>
> on the way out, split and check the first char, and set the flag
> accordingly. Won't that work?
>
>> # Store
>> while (my ($k, $v) = each %stringmap) {
>> $r->pnotes( $k => $v );
>> $r->pnotes( "$k.utf8" => Encode::is_utf8($v);
>> }
>> # Fetch:
>> my %stringmap;
>> while (my ($k, $v) = each %{ {$r->pnotes }}) {
>> next if $k =~ /\.utf8$/;
>> Encode::_utf8_on($v) if $r->pnotes("$k.utf");
>> $stringmap{$k} = $v;
>> }
>> This is an extreme example, but I think that it makes the case. apreq
>> has to be able to know what kind of scalar it has, anyway, so it
>> should be able to know if the string is decoded to Perl's internal
>> utf8 representation, and if so, make sure it keeps the utf8 flag on.
>
> You certainly have no need to do that since pnotes already preserve
> the perl flags.
>
But I have to copy the data to a new object, and I have to rewrite all
code, that use $t->set $t->get $t->do and so on.
What David shows is only one way to preserve the flag.
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by Stas Bekman <st...@stason.org>.
David Wheeler wrote:
> On Oct 5, 2004, at 3:56 PM, Stas Bekman wrote:
>
>> So once these flags are there, it's a total user's responsibility to
>> do the right thing, correct?. I was just wondering whether we should
>> do that in the APR::Table instead, if it's going to serve other
>> purposes. Of course if this is only really important for
>> Apache::Request, then that's probably the best place to do that.
>
>
> I think that Boris wants it to not be the user's responsibility, and I'm
> inclined to agree with him. (Holler if I'm misrepresenting your
> position, Boris.) For the same reason that when I store a reference I
> don't want to get back a string such as "HASH(0x800368)", when I store a
> string that's got the utf8 flag enabled, I want to get it back that way.
> And if I have an array of strings, some with the flag set and some
> without, I don't know which is which and I don't want to have to think
> about which is which.
What you and Boris say makes perfect sense, but we certainly don't want to
try to support all possible formats out there and try to handle those on
behalf of users. If we do we may need to spawn APR::Table to be a project
on its own with its own dedicated developers team. this is something
suitable for Apache::Request which deals with a sub-set of formats, it's
not suitable for APR::Table which is a general purpose thing.
I've proposed to Boris to try to decode the utf8 variable back into the
bytecodes string, before storing it in the table, but he doesn't want
that, since it adds an unwanted overhead.
I don't know if it's a good idea to have APR::Table to deal only with utf8
as the only special case. Joe?
>> Of course users can store the string with the flag by themselves, by
>> simply storing: "$flag" . "string" and get it back as they have stored
>> it.
>
>
> The flag is not a Perl scalar. It's an attribute of the C struct for the
> scalar variable (sorry if I'm mangling the terminology here, I'm not a C
> hacker). So it would be very inefficient to have to store all my strings
> in a table like this
sure, but you can make it a perl scalar.
string "foo" on the way to the table:
utf8 => "1foo";
non-utf8 => "0foo"
on the way out, split and check the first char, and set the flag
accordingly. Won't that work?
> # Store
> while (my ($k, $v) = each %stringmap) {
> $r->pnotes( $k => $v );
> $r->pnotes( "$k.utf8" => Encode::is_utf8($v);
> }
>
> # Fetch:
> my %stringmap;
> while (my ($k, $v) = each %{ {$r->pnotes }}) {
> next if $k =~ /\.utf8$/;
> Encode::_utf8_on($v) if $r->pnotes("$k.utf");
> $stringmap{$k} = $v;
> }
>
> This is an extreme example, but I think that it makes the case. apreq
> has to be able to know what kind of scalar it has, anyway, so it should
> be able to know if the string is decoded to Perl's internal utf8
> representation, and if so, make sure it keeps the utf8 flag on.
You certainly have no need to do that since pnotes already preserve the
perl flags.
--
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 5, 2004, at 3:56 PM, Stas Bekman wrote:
> So once these flags are there, it's a total user's responsibility to
> do the right thing, correct?. I was just wondering whether we should
> do that in the APR::Table instead, if it's going to serve other
> purposes. Of course if this is only really important for
> Apache::Request, then that's probably the best place to do that.
I think that Boris wants it to not be the user's responsibility, and
I'm inclined to agree with him. (Holler if I'm misrepresenting your
position, Boris.) For the same reason that when I store a reference I
don't want to get back a string such as "HASH(0x800368)", when I store
a string that's got the utf8 flag enabled, I want to get it back that
way. And if I have an array of strings, some with the flag set and some
without, I don't know which is which and I don't want to have to think
about which is which.
> Of course users can store the string with the flag by themselves, by
> simply storing: "$flag" . "string" and get it back as they have stored
> it.
The flag is not a Perl scalar. It's an attribute of the C struct for
the scalar variable (sorry if I'm mangling the terminology here, I'm
not a C hacker). So it would be very inefficient to have to store all
my strings in a table like this
# Store
while (my ($k, $v) = each %stringmap) {
$r->pnotes( $k => $v );
$r->pnotes( "$k.utf8" => Encode::is_utf8($v);
}
# Fetch:
my %stringmap;
while (my ($k, $v) = each %{ {$r->pnotes }}) {
next if $k =~ /\.utf8$/;
Encode::_utf8_on($v) if $r->pnotes("$k.utf");
$stringmap{$k} = $v;
}
This is an extreme example, but I think that it makes the case. apreq
has to be able to know what kind of scalar it has, anyway, so it should
be able to know if the string is decoded to Perl's internal utf8
representation, and if so, make sure it keeps the utf8 flag on.
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by Stas Bekman <st...@stason.org>.
Boris Zentner wrote:
>
> Hi,
>
> Am 05.10.2004 um 18:34 schrieb David Wheeler:
>
>> On Oct 5, 2004, at 9:20 AM, Joe Schaefer wrote:
>>
>>> We could use three-bit field for marking the charset:
>>>
>>> 0 - unknown
>>> 1 - ASCII
>>> 2 - UTF-8
>>> 3 - UTF-16
>>> [ room for 4 more iso? charsets ]
So once these flags are there, it's a total user's responsibility to do
the right thing, correct?. I was just wondering whether we should do that
in the APR::Table instead, if it's going to serve other purposes. Of
course if this is only really important for Apache::Request, then that's
probably the best place to do that.
Of course users can store the string with the flag by themselves, by
simply storing: "$flag" . "string" and get it back as they have stored it.
--
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi,
Am 05.10.2004 um 18:34 schrieb David Wheeler:
> On Oct 5, 2004, at 9:20 AM, Joe Schaefer wrote:
>
>> We could use three-bit field for marking the charset:
>>
>> 0 - unknown
>> 1 - ASCII
>> 2 - UTF-8
>> 3 - UTF-16
>> [ room for 4 more iso? charsets ]
>
> Note that data encoded in UTF-8 is not the same as decoded to Perl's
> internal utf8 format. The latter has the same bytes, but the "utf8"
> flag has been set on the variable so that Perl knows how to properly
> count characters, among other things. I suspect that it is the loss of
> the setting of this flag on the string variable that Boris is
> reporting.
>
What I like to see is
0 - unknown
1 - ASCII
2 - UTF-8
3 - UTF-16
...
7 - perlutf8
The perlutf8 flag is somewhat unrelated to the charsets above, but this
allows us to transform from perl to charsets and gets the issue with
perls lost bit resloved. perls internal utf8 strings can differ from
utf8 even if I never noticed the difference.
> Regards,
>
> David
>
>
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 5, 2004, at 10:38 AM, Joe Schaefer wrote:
> Ok. This is what I need to better understand then. How do they
> differ, in terms of what's stored in SvCUR, and the actual bytes
> in the SvPVX array?
Just in the flag, I believe. utf8 has UTF-8 encoded data (although
Encode.pm calls this "decoded to Perl's internal string format", but I
think that's largely an implementation distinction), but UTF-8 strings
can be stored in variables without the utf8 flag set.
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by Joe Schaefer <jo...@sunstarsys.com>.
David Wheeler <da...@kineticode.com> writes:
[...]
> Yes, that's what I understood, as well. I was just pointing out that
> the utf8 flag is not the same as using the UTF-8 character set,
Ok. This is what I need to better understand then. How do they
differ, in terms of what's stored in SvCUR, and the actual bytes
in the SvPVX array?
--
Joe Schaefer
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 5, 2004, at 10:18 AM, Joe Schaefer wrote:
>>> 0 - unknown
>>> 1 - ASCII
>>> 2 - UTF-8
>>> 3 - UTF-16
>>> [ room for 4 more iso? charsets ]
>>
>> Note that data encoded in UTF-8 is not the same as decoded to Perl's
>> internal
>> utf8 format. The latter has the same bytes, but the "utf8" flag has
>> been set on the variable
>
> Sorry, I don't quite follow. We store params in a C struct that's
> currently devoid of any charset information. Boris wants to mark
> certain params as being utf8, so when a later Apache::Request module
> fetches them, they'll be returned from param() as utf8 strings instead
> of byte strings (iow, param will set the "utf8" flag for them).
Yes, that's what I understood, as well. I was just pointing out that
the utf8 flag is not the same as using the UTF-8 character set, and I
was saying this because "utf8" was not one of the options you mentioned
above.
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 5, 2004, at 6:03 PM, Joe Schaefer wrote:
> I don't want to move towards a Tower of Babel within apreq2.
> Supporting a few major charsets should be ok. What I'm trying
> to decide is how to map utf8 key-value pairs into the apreq_value_t
> above. Do we really need a dedicated perlutf8 flag, or can we
> use APREQ_CHARSET_UTF8 - so that the apreq_value_t's data will
> be meaningful to non-perl apps?
You need perlutf8 in order for Perl to recognize a string as utf8 so
that things like length() and substr() work properly. You need
APREQ_CHARSET_UTF8 if you want the encoding of strings to be meaningful
to other languages.
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by Stas Bekman <st...@stason.org>.
Joe Schaefer wrote:
> Boris Zentner <bz...@2bz.de> writes:
>
> [...]
>
>
>>Perhaps it is better to split the byte into nibbles 0:3 for the
>>charset and 4:7 for flags.
>
>
> Yes, more or less. Here's what I've been toying with right now:
>
> /** @brief libapreq's pre-extensible string type */
> typedef struct apreq_value_t {
> const char *name; /**< value's name */
> apr_size_t size; /**< Size of data.*/
> unsigned char flags; /**< charset:3, tainted:1, reserved:2, apps:2 */
oh, that's a good idea to keep track of the taint flag. since that's
another thing that we should probably restore on data retrieval in
APR::Table, if we decide to handle UTF8.
Too bad perl flags take 32U, it'd be the easiest to just store the flags
as is and then restore them all at once. (though may be that's not
feasible since the status of the new variable is not the same as it was
when it was stored)
--
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
Re: Apache::Request, APR::Table and UTF8
Posted by Joe Schaefer <jo...@sunstarsys.com>.
Boris Zentner <bz...@2bz.de> writes:
[...]
> Perhaps it is better to split the byte into nibbles 0:3 for the
> charset and 4:7 for flags.
Yes, more or less. Here's what I've been toying with right now:
/** @brief libapreq's pre-extensible string type */
typedef struct apreq_value_t {
const char *name; /**< value's name */
apr_size_t size; /**< Size of data.*/
unsigned char flags; /**< charset:3, tainted:1, reserved:2, apps:2 */
char data[1]; /**< Actual data bytes.*/
} apreq_value_t;
typedef enum {
APREQ_CHARSET_UNKNOWN = 0,
APREQ_CHARSET_ASCII = 1,
APREQ_CHARSET_UTF8 = 2,
APREQ_CHARSET_UTF16 = 3,
/* XXX: may add 4 additional charsets */
} apreq_charset_t;
#define apreq_value_get_charset(v) ((apreq_charset_t )((v)->flags & 7U)
#define apreq_value_set_charset(v,c) do { \
apreq_value_t *val = v; \
val->flags = (val->flags & ~7U) | ((c) & 7U); \
} while (0)
#define apreq_value_is_tainted(v) ((v)->flags & 8U)
#define apreq_value_tainted_on(v) ((v)->flags |= 8U)
#define apreq_value_tainted_off(v) ((v)->flags &= ~8U)
> This gives more room for charsets
I don't want to move towards a Tower of Babel within apreq2.
Supporting a few major charsets should be ok. What I'm trying
to decide is how to map utf8 key-value pairs into the apreq_value_t
above. Do we really need a dedicated perlutf8 flag, or can we
use APREQ_CHARSET_UTF8 - so that the apreq_value_t's data will
be meaningful to non-perl apps?
--
Joe Schaefer
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
Hi,
Am 05.10.2004 um 19:18 schrieb Joe Schaefer:
> David Wheeler <da...@kineticode.com> writes:
>
>> On Oct 5, 2004, at 9:20 AM, Joe Schaefer wrote:
>>
>>> We could use three-bit field for marking the charset:
>>>
>>> 0 - unknown
>>> 1 - ASCII
>>> 2 - UTF-8
>>> 3 - UTF-16
>>> [ room for 4 more iso? charsets ]
>>
Perhaps it is better to split the byte into nibbles 0:3 for the charset
and 4:7 for flags. This gives more room for charsets since the charset
is exclusive. ie: if it is ASCII it can not be UTF8.
0000 unknown
0001 ascii
0010 utf8
0011 utf16
0100 ...
...
1111 ...
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by Boris Zentner <bz...@2bz.de>.
HI,
Am 05.10.2004 um 19:48 schrieb Geoffrey Young:
>
>> We store params in a C struct that's
>> currently devoid of any charset information. Boris wants to mark
>> certain params as being utf8, so when a later Apache::Request module
>> fetches them, they'll be returned from param() as utf8 strings instead
>> of byte strings (iow, param will set the "utf8" flag for them).
>
> not to be the only one in the class that isn't able to follow along,
> but
> does calling Encode::_utf8_on() from userland not work?
>
No, it works, but only if _you_ know that this param is encoded in
utf8. If you do not know it perl act wrong and convert it again to utf8
that result in a string that is not ascii, not iso-something and not
utf just wrong data. Your 'solution' implies that you write the part
that insert and work with the data. It will never work with someone
other module, since he can not know.
It is really *no* userspace problem.
If your source use Encode::_utf8_on it is the same as setting the utf8
flag to always on. And thats wrong. Also it encodes a string for every
insert. It is allowed to insert data into apr with $apr->param( text =>
$var ); if var is already in utf8 I get it wrong back.
> I guess I'm still trying to reconcile how this is not a user-space
> problem -
> I would think that the various functions in Encode.pm and utf8.pm
> would be
> sufficient most of this stuff.
>
> --Geoff
>
>
--
Boris
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 5, 2004, at 10:48 AM, Geoffrey Young wrote:
> not to be the only one in the class that isn't able to follow along,
> but
> does calling Encode::_utf8_on() from userland not work?
Yes, it works, if you're confident that the string is encoded in UTF-8.
See my blog entries on this stuff for a bit more information.
http://www.justatheory.com/computers/programming/perl/utf8_trials.html
http://www.justatheory.com/computers/programming/perl/mod_perl/
escape_html_utf8.html
> I guess I'm still trying to reconcile how this is not a user-space
> problem -
I would think that the various functions in Encode.pm and utf8.pm would
be
> sufficient most of this stuff.
Because it's annoying that the user has to remember what had the flag
set and what didn't. If I think of pnotes() (for example--not sure if
the immediate issue applies to it) as a simple hash, then I expect to
get back what I put into it. So if I put in a string with the utf8 flag
set, I expect to get it back that way.
Regards,
David
Re: Apache::Request, APR::Table and UTF8
Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> We store params in a C struct that's
> currently devoid of any charset information. Boris wants to mark
> certain params as being utf8, so when a later Apache::Request module
> fetches them, they'll be returned from param() as utf8 strings instead
> of byte strings (iow, param will set the "utf8" flag for them).
not to be the only one in the class that isn't able to follow along, but
does calling Encode::_utf8_on() from userland not work?
I guess I'm still trying to reconcile how this is not a user-space problem -
I would think that the various functions in Encode.pm and utf8.pm would be
sufficient most of this stuff.
--Geoff
Re: Apache::Request, APR::Table and UTF8
Posted by Joe Schaefer <jo...@sunstarsys.com>.
David Wheeler <da...@kineticode.com> writes:
> On Oct 5, 2004, at 9:20 AM, Joe Schaefer wrote:
>
> > We could use three-bit field for marking the charset:
> >
> > 0 - unknown
> > 1 - ASCII
> > 2 - UTF-8
> > 3 - UTF-16
> > [ room for 4 more iso? charsets ]
>
> Note that data encoded in UTF-8 is not the same as decoded to Perl's internal
> utf8 format. The latter has the same bytes, but the "utf8" flag has
> been set on the variable
Sorry, I don't quite follow. We store params in a C struct that's
currently devoid of any charset information. Boris wants to mark
certain params as being utf8, so when a later Apache::Request module
fetches them, they'll be returned from param() as utf8 strings instead
of byte strings (iow, param will set the "utf8" flag for them).
--
Joe Schaefer
Re: Apache::Request, APR::Table and UTF8
Posted by David Wheeler <da...@kineticode.com>.
On Oct 5, 2004, at 9:20 AM, Joe Schaefer wrote:
> We could use three-bit field for marking the charset:
>
> 0 - unknown
> 1 - ASCII
> 2 - UTF-8
> 3 - UTF-16
> [ room for 4 more iso? charsets ]
Note that data encoded in UTF-8 is not the same as decoded to Perl's
internal utf8 format. The latter has the same bytes, but the "utf8"
flag has been set on the variable so that Perl knows how to properly
count characters, among other things. I suspect that it is the loss of
the setting of this flag on the string variable that Boris is
reporting.
Regards,
David