Subject: | Problems parsing chunked data |
Perl version: "This is perl, v5.8.8 built for i686-linux"
Package: HTTP::Parser v0.03
Standard Linux build
I've been playing with HTTP::Parser which has worked well until now,
but suddenly I've found that it doesn't behave nicely with chunked
data.
Here's a test.pl that shows the issue:
## test.pl ##
#!/usr/bin/perl
use HTTP::Parser;
use strict;
my $parser = HTTP::Parser->new(response => 1);
my $status;
while (<STDIN>) {
$status = $parser->add($_);
}
# Status should now contain the final status after all data was added
if(0 == $status) {
print "Response: " . $parser->object()->code(); # HTTP::Response
} elsif(-2 == $status) {
print "need a line of data\n";
} elsif(-1 == $status) {
print "need more data\n";
} else { # $status > 0
print "need $status byte(s)\n";
}
## END ##
And here's some real chunked data (from Google mail CSS) that shows
the problem:
## css ##
HTTP/1.1 200 OK
Cache-control: private
Content-Type: text/css; charset=UTF-8
ETag: "17cga4dsba0ku"
Last-Modified: Fri, 05 Sep 2003 02:11:15 GMT
Expires: Sun, 04 Nov 2007 08:28:04 GMT
Transfer-Encoding: chunked
Server: GFE/1.3
Date: Mon, 15 Oct 2007 08:28:04 GMT
261
/* These styles are hidden from Netscape 4.x */
.m td, .l td{padding-left:6px}
.th td{border-bottom:1px solid #bbb}
.th a:link, .th a:active, .th a:visited{text-decoration:none}
.bn td{border:1px solid #FAD163}
.ct textarea, .ct input, .ct
select{font-family:arial,sans-serif;font-size:100%}
.compose .mi{width:80ex;font-family:arial,sans-serif;font-size:100%}
.compose .i, .search .i{width:99%;font-family:arial,sans-serif;font-size:100%}
.qr textarea{width:80ex;font-family:arial,sans-serif;font-size:100%}
.qr input{font-family:arial,sans-serif;font-size:100%}
.thi {border:2px solid #c3d9ff;padding:5px}
0
## END ##
Piping a non-chunked response works fine. Piping the data above to
the test script does not work. HTTP::Parser dies with the error:
"expected chunked enoding, got '* These styles are hidden from
Netscape ...' at /usr/lib/perl5/site_perl/5.8.8/HTTP/Parser.pm line
325, <STDIN> line 24."
I think the error might be on line 311 of Parser.pm:
if($self->{data} =~ s/^([0-9a-fA-F]+)[^\x0d\x0a]*?\x0d?\x0a//) {
I'm not entirely sure what is wrong, so any help would be appreciated.