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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |