Skip Menu |

This queue is for tickets about the HTTP-Parser CPAN distribution.

Report information
The Basics
Id: 30005
Status: resolved
Priority: 0/
Queue: HTTP-Parser

People
Owner: Nobody in particular
Requestors: david [...] edeca.net
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.03
Fixed in: 0.04



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.
From: david [...] edeca.net
On Mon Oct 15 05:24:54 2007, edeca wrote: Show quoted text
> I think the error might be on line 311 of Parser.pm: > > if($self->{data} =~ s/^([0-9a-fA-F]+)[^\x0d\x0a]*?\x0d?\x0a//) {
The error isn't here, it's further down. The correct length is extracted with the above regex but data is removed from the buffer at the wrong point. The attached patch fixes this. David
--- HTTP/Parser.pm 2007-10-23 10:42:14.000000000 +0100 +++ HTTP/Parser.pm 2007-10-25 11:26:25.000000000 +0100 @@ -335,9 +335,12 @@ if(length $self->{data} > $self->{chunk} and substr($self->{data},$self->{chunk},2) =~ /^(\x0d?\x0a)/) { my $crlf = $1; - $self->{obj}->add_content(substr($self->{data},0,delete $self->{chunk})); + $self->{obj}->add_content(substr($self->{data},0,$self->{chunk})); substr($self->{data},0,length $crlf) = ''; + # remove data from the buffer that we have already parsed + $self->{data} = substr($self->{data},delete $self->{chunk}); + # got chunks? goto CHUNK; }
Fixed in 0.04, credited patch from David Cannings.