HTTP::Parser2::XS - yet another http parser
use HTTP::Parser2::XS; # request my $buf = "GET /foo%20bar/ HTTP/1.0\x0d\x0a". "Host: localhost\x0d\x0a". "\x0d\x0a"; my $r = {}; my $rv = parse_http_request($buf, $r); if ($rv == -1) { # bad request or internal error } elsif ($rv == -2 && length($buf) > 4096) { # incomplete request and too long already, # no point allowing something like this } elsif ($rv == -2) { # incomplete request, call again when there is more data # in the buffer } else { # $rv contains the length of the request header on success } if (exists $r->{'host'} && $r->{'host'}->[0] eq 'localhost') { # ... } if ($r->{'_uri'} eq '/foo bar') { # ... } # response my $buf = "HTTP/1.0 200 OK\x0d\x0a". "Content-type: text/html\x0d\x0a". "\x0d\x0a". "foo bar"; my $r = {}; my $rv = parse_http_response($buf, $r); if ($rv == -1) { # bad reponse or internal error } elsif ($rv == -2 && length($buf) > 4096) { # incomplete response header and too long already, # no point allowing something like this } elsif ($rv == -2) { # incomplete response, call again when there is more data # in the buffer } else { # $rv contains the length of the response header on success } if (exists $r->{'content-type'} && $r->{'content-type'}->[0] eq 'text/html') { # ... } if ($r->{'_status'} eq '200') { # ... }
HTTP::Parser2::XS parses data into a bit different form making perl code more clear and consistent.
parse_http_request parse_http_response
Parses HTTP request in $buf
into the hashref $r
. Returns length
of the header on success, -1
on error and -2
if request isn't complete
yet, i.e. doesn't have an entire header.
Converts each header name to lower-case and stores each value as an arrayref.
For example $r->{'host'}->[0]
returns a Host
header and
@{$r->{'cookie'}}
returns all the cookie headers.
Additionally adds the following elements:
Request method, usually "GET", "HEAD", "POST" or "PUT".
Unchanged undecoded raw request uri.
Decoded request uri without query string. A lot like $uri
in nginx.
Query string. Everything after question mark.
Protocol and version. Either "HTTP/1.0" or "HTTP/1.1".
Either 1
or 0
. Examines connection header and protocol version to
decide whether or not keep-alive connection is desired. And if it is
sets $r->{'_keepalive'}
to 1
.
Parses content-length header. Stores length as a numeric value (SvNV to be precise) or undef if there is no content-length header.
Parses HTTP response in $buf
into the hashref $r
. Returns length
of the header on success, -1
on error and -2
if response isn't complete
yet, i.e. doesn't have an entire header.
Converts each header name to lower-case and stores each value as an arrayref.
Additionally adds the following elements:
Protocol and version. Either "HTTP/1.0" or "HTTP/1.1".
Response status. For example "200" for "HTTP/1.0 200 OK" response.
Status message. For example "OK" for "HTTP/1.0 200 OK" response.
Either 1
or 0
. Examines connection header and protocol version to
decide whether or not keep-alive connection is desired. And if it is
sets $r->{'_keepalive'}
to 1
.
Parses content-length header. Stores length as a numeric value (SvNV to be precise) or undef if there is no content-length header.
HTTP::Parser::XS
Alexandr Gomoliako <zzz@zzz.org.ua>
This module uses Kazuho Oku's code from HTTP::Parser::XS.
Copyright 2011 Alexandr Gomoliako, Kazuho Oku. All rights reserved.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
© 2012 Alexandr Gomoliako