Subject: | HTTP::Parser should support multiple requests/responses |
Date: | Mon, 23 Aug 2010 02:48:07 -0700 |
To: | "bug-HTTP-Parser [...] rt.cpan.org" <bug-HTTP-Parser [...] rt.cpan.org> |
From: | Daniel Burr <dburr [...] topcon.com> |
It is currently not possible to use the public interface of HTTP::Parser to parse more than one response (or request) using a single instance of the parser. It is not an uncommon scenario to have multiple requests and responses sent on the same connection which therefore requires more than one HTTP::Response/HTTP::Request object. The code to handle this would be something like:
if($parser->add($data) == 0) {
do {
handle_response($parser->object(), $data);
} while($parser->extra() > 0 && $parser->add() == 0);
}
Unfortunately this does not work with HTTP::Parser 0.05. There user can work around this in one of 2 ways:
1) Create a new instance of the parser using the extra() data after parsing the first response
2) Reset the parser by breaking encapsulation:
if($parser->add($data) == 0) {
do {
handle_response($parser->object(), $data);
$parser->{state} = 'blank';
} while($parser->extra() > 0 && $parser->add() == 0);
}
While this is a workable solution, it is not very nice. I would suggest that the following change is made to Parser.pm in _parse_body():
if(length $self->{data} >= $length) {
$self->{obj}->content(substr($self->{data},0,$length,''));
+ $self->{state} = 'blank';
return 0;
}
If this is going to break backwards compatibility then you could alternatively add a new method (e.g. reset()) which simply sets the state to 'blank'.
DB
Confidentiality Notice: This message (including attachments) is a private communication solely for use of the intended recipient(s).
If you are not the intended recipient(s) or believe you received this message in error, notify the sender immediately and then delete this
message. Any other use, retention, dissemination or copying is prohibited and may be a violation of law, including the Electronic
Communication Privacy Act of 1986."