There are a couple of HTTP parsers in modules (e.g.
POE::Component::Server::HTTP, HTTP::Daemon, probably in SOAP::Lite too) but
not a standalone HTTP parser module.
I'd like to contribute an HTTP::Parser module to CPAN. It has only two
methods, "new" (no parameters) and "parse" which takes some text and either:
dies (on error) (I can return a string if people prefer it), or returns:
0: want more data but don't know how much
-1: like 0 but a hint that we want a line
<n>: want n bytes more
HTTP::Request object: finished parsing the request.
parse() should be called multiple times with incoming data until it returns a
HTTP::Request object or dies; this makes it work well with event-based
systems (I use it with the Event module). It handles chunked encoding.
A simple example:
my $parser = HTTP::Parser->new();
$parser->parse("GET / HTTP/1.1\x0d\x0aHost: foo\x0d\x0a\x0d\x0a");
returns an HTTP::Request which dumps out as:
bless( {
'_content' => '',
'_uri' => bless( do{\(my $o = '/')}, 'URI::_generic' ),
'_headers' => bless( {
'x-http-version' => '1.1',
'host' => 'foo'
}, 'HTTP::Headers' ),
'_method' => 'GET'
}, 'HTTP::Request' );
Note that it's up to the user to determine that since it's HTTP 1.1 (stored by
HTTP::Parser in the X-HTTP-Version header) and there's no Connection: close
header, that the connection may stay open and accept further requests;
HTTP::Parser just parses the request.
--
Dave
Isa. 40:31