Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: "use encoding" behaviour change under Perl 5.10?

by gnosek (Sexton)
on Mar 21, 2009 at 15:28 UTC ( [id://752255]=note: print w/replies, xml ) Need Help??


in reply to Re: "use encoding" behaviour change under Perl 5.10?
in thread "use encoding" behaviour change under Perl 5.10?

Thanks, it hasn't bitten me yet but I'll keep an eye on it. I have encountered problems in the other half of the expression.

A form field value of '<ę' (less than sign, small e with ogonek) got urlencoded as '%3C%C4%99' by the browser and decoded as '<\x{fffd}\x{fffd}' by 'chr(hex($1))'. What really drove me mad was that 'ę' itself ('%C4%99') was handled properly so I thought it was some HTML escaping misfeature that appeared between CGI versions.

  • Comment on Re^2: "use encoding" behaviour change under Perl 5.10?

Replies are listed 'Best First'.
Re^3: "use encoding" behaviour change under Perl 5.10?
by diotalevi (Canon) on Mar 25, 2009 at 15:37 UTC

    You've got another problem. Your browser incorrectly passed the ę character as %C4%88 that is, as a string which happens to be UTF-8 but should have passed it as %u0119. You've got to *guess* during your URL parsing if someone has started passing you a byte-string which happens to look like a UTF-8 string and if so, reinterpret it as UTF-8 instead. If you're being fully careful, you should also be using the Content-Type of the HTTP header your server sends, the client sends, and the HTML content encoding you sent when generating your page.

    Here's what I use.

    sub uri_unescape { my $to_decode = shift; return if ! defined $to_decode; $to_decode =~ s/\+/ /g; # A "good enough" check to see if the browser encoded high bit # characters as UTF-8 by looking for something that resembles a # URL-encoded utf8 octet series # # utf8 encoding starts with 11xxxxxx in the first byte and # 10xxxxxx in the subsequent bytes # http://en.wikipedia.org/wiki/UTF-8#Description my $is_byte_encoded_utf8 = $to_decode =~ /%[c-fC-F][[:xdigit:]]%[8 +9abAB][[:xdigit:]]/; $to_decode =~ s/%([[:xdigit:]]{2})/chr hex "0x$1"/eg; # Decode the "UTF-8" if it looked like it was such. If I enter # this block, the string may now be UTF-8 encoded as a perl # string. if ($is_byte_encoded_utf8) { $to_decode = Encode::decode( 'utf8', $to_decode ); } # Promote %uXXXX escapes up to characters. This is after the # Encode call so I don't inadvertently cause a promotion to utf8 # and then ask Encode to do another promotion. $to_decode =~ s/%u([[:xdigit:]]{4})/chr hex "0x$1"/eg; return $to_decode; }

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://752255]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-19 11:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found