Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: libwww-perl, POE and Unicode

by danmcb (Monk)
on May 05, 2008 at 09:28 UTC ( #684593=note: print w/replies, xml ) Need Help??

in reply to Re: libwww-perl, POE and Unicode
in thread libwww-perl, POE and Unicode

the new test that has been added in HTTP::Message is this:
sub { utf8::downgrade($_[0], 1) or Carp::croak("HTTP::Message content must be bytes") }

Isn't suddenly deciding to die on something which previous releases didn't even test for a bit, erm, extreme? The thinking, technically speaking, may be completely correct, of course ...

Replies are listed 'Best First'.
Re^3: libwww-perl, POE and Unicode
by Juerd (Abbot) on May 05, 2008 at 10:36 UTC

    This is neither just a test, nor extreme. This snippet is exactly what every octet oriented Perl function should do.

    Doubtless, this will "break" some existing code, as it has clearly done in this case. But believe me, clear breakage with a meaningful error message is SO much better than the vague semi-random encoding trouble you could have gotten without it!

    An HTTP message is strictly a stream of octets. It is impossible to have unencoded text, because TCP does not support that. If you would try to use the rejected message over a socket, you would get Perl warnings about "wide characters", a sign that Perl is doing the best it can to force the invalid value into an octet form: it UTF-8 encodes and warns about it. However, Perl first tries to downgrade. If that succeeds, it uses the downgraded string and does not complain. This repairs several bugs caused by sloppy programming.

    The snippet that you cited does the same thing: downgrade and use the downgraded form, but if it cannot be downgraded (that is: if the string contains a character with an ordinal value greater than 255) to byte form, complain. The way in which it complains is different from Perls built in. This code DIES. Your gain: you now know exactly that there is a problem, and where it is. Without this new addition to LWP you would probably not have know that there was a problem, and if you had, you would probably still be looking for the source. And if you're like most Perl programmers who don't yet fully understand the octet/text separation logic, you would probably have mis-identified something else as the source, and "fixed" it by making your code forward incompatible. Much like PoCoCli::HTTP did.

    All I can say is: be glad, very glad, that LWP got this change. The pain it causes is temporarily, and much less than the pain it prevents.

    Juerd # { site => '', do_not_use => 'spamtrap', perl6_server => 'feather' }

      Hi there,

      before posting a bug report, I'd like a comment about why utf8::downgrade() had to be used instead of Encode::encode_utf8().

      The former modifies the input in-place (argh!) and fails if it isn't either Latin-1 or EBCDIC. This means that the behaviour changes depending on the platform, and your odds are gone if your text can't be converted into one of those two encodings (which means a big part of the world).

      The latter would leave its input unchanged, would behave the same whatever the platform (I hope) and would thus be truly "octet oriented".

      Do you think a bug report/patch would be well-placed in this case, or am I overlooking something?


      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Io ho capito... ma tu che hai detto?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2020-12-02 22:53 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (47 votes). Check out past polls.