I just found that on very specific web pages parsed by my scripts, that setting a UserAgent value through LWP::UserAgent seems to crash XML::LibXML, and I can't figure out why.

Here's a snippet to exhibit this behavior. Run this, then uncomment the $browser->agent($ua); call to see the crash:

use strict; use LWP::UserAgent; use XML::LibXML; # Or http://www.cboe.com/Chinese/ my $url = 'http://www.cboe.com/Spanish/'; my $ua = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)'; my $browser = LWP::UserAgent->new(); # Uncomment this line to crash libxml's parser # $browser->agent($ua); my $response = $browser->get($url); my $content = $response->content; my $type = $response->content_type; print "Cleaning $url ($type)...\n"; clean_html($content); sub clean_html { my $input = shift; my $parser = XML::LibXML->new(); $parser->recover(1); my $cleaned = $parser->parse_html_string($input)->toStringHTML; }

It only happens on some pages, and I can't find a pattern in the actual HTML content itself that causes it. Here's the error I get when it dies (a few hundred of these for this particular page parsed):

output conversion failed due to conv error Bytes: 0xE4 0xE5 0xE6 0xE8 xmlOutputBufferWrite: encoder error output conversion failed due to conv error Bytes: 0xE5 0xE6 0xE8 0x26 xmlOutputBufferWrite: encoder error ...

Comment out the UserAgent value, and it seems to work perfectly, without any errors. What could be causing this?

Update: Further investigation shows that the '/' in the UserAgent value is stuffing up XML::LibXML here. Changing the '/' to anything else will cause the page to work.

Why would a slash character in the UserAgent string dump libxml?


In reply to Crashing XML::LibXML by setting UserAgent by hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.