HTTP::Parser::XS - a fast, primitive HTTP request parser
use HTTP::Parser::XS qw(parse_http_request); # for HTTP servers my $ret = parse_http_request( "GET / HTTP/1.0\r\nHost: ...\r\n\r\n", \%env, ); if ($ret == -2) { # request is incomplete ... } elsif ($ret == -1) { # request is broken ... } else { # $ret includes the size of the request, %env now contains a PSGI # request, if it is a POST / PUT request, read request content by # yourself ... } # for HTTP clients use HTTP::Parser::XS qw(parse_http_response HEADERS_AS_ARRAYREF); my %special_headers = ( 'content-length' => undef, ); my($ret, $minor_version, $status, $message, $headers) = parse_http_response($response, HEADERS_AS_ARRAYREF, \%special_headers); if($ret == -1) } # response is incomplete } elsif($ret == -2) { # response is broken } else { # $ret is the length of the headers, starting the content body # the other values are the response messages. For example: # $status = 200 # $message = "OK" # $headers = [ 'content-type' => 'text/html', ... ] # and $special_headers{'content-length'} will be filled in }
HTTP::Parser::XS is a fast, primitive HTTP request/response parser.
The request parser can be used either for writing a synchronous HTTP server or a event-driven server.
The response parser can be used for writing HTTP clients.
Note that even if this distribution name ends ::XS, pure Perl implementation is supported, so you can use this module on compiler-less environments.
::XS
Tries to parse given request string, and if successful, inserts variables into %env. For the name of the variables inserted, please refer to the PSGI specification. The return values are:
length of the request (request line and the request headers), in bytes
given request is corrupt
given request is incomplete
Tries to parse given response string. $header_format must be HEADERS_AS_ARRAYREF, HEADERS_AS_HASHREF, or HEADERS_NONE, which are exportable constants.
HEADERS_AS_ARRAYREF
HEADERS_AS_HASHREF
HEADERS_NONE
The optional %special_headers is for headers you specifically require. You can set any HTTP response header names, which must be lower-cased, and their default values, and then the values are filled in by parse_http_response(). For example, if you want the Cointent-Length field, set its name with default values like %h = ('content-length' => undef) and pass it as %special_headers. After parsing, $h{'content-length'} is set if the response has the Content-Length field, otherwise it's not touched.
parse_http_response()
Cointent-Length
%h = ('content-length' => undef)
$h{'content-length'}
Content-Length
The return values are:
$ret
The parsering status, which is the same as parse_http_response(). i.e. the length of the response headers in bytes, -1 for incomplete headers, or -2 for errors.
-1
-2
If the given response string is broken or imcomplete, parse_http_response() returns only this value.
$minor_version
The minor version of the given response. i.e. 1 for HTTP/1.1, 0 for HTTP/1.0.
1
0
$status
The HTTP status of the given response. e.g. 200 for success.
200
$message
The HTTP status message. e.g. OK for success.
OK
$headers
The HTTP headers for the given response. It is an ARRAY reference if $header_format is HEADERS_AS_ARRAYREF, a HASH reference on HEADERS_AS_HASHREF, an undef on HEADERS_NONE.
undef
The names of the headers are normalized to lower-cased.
Both parse_http_request() and parse_http_response() in XS implementation have some size limitations.
parse_http_request()
The number of headers is limited to 128. If it exceeds, both parsing routines report parsing errors, i.e. return -1 for $ret.
128
The size of header names is limited to 1024, but the parsers do not the same action.
1024
parse_http_request() returns -1 if too-long header names exist.
parse_http_request() simply ignores too-long header names.
Copyright 2009- Kazuho Oku
Kazuho Oku gfx mala tokuhirom
nothingmuch charsbar
http://github.com/kazuho/picohttpparser
HTTP::Parser HTTP::HeaderParser::XS
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install HTTP::Parser::XS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTTP::Parser::XS
CPAN shell
perl -MCPAN -e shell install HTTP::Parser::XS
For more information on module installation, please visit the detailed CPAN module installation guide.