Kazuho Oku > HTTP-Parser-XS > HTTP::Parser::XS



Annotate this POD


Open  1
View/Report Bugs
Module Version: 0.17   Source  





  # for HTTP servers
  my $ret = parse_http_request(
  if ($ret == -2) {
      # request is incomplete
  } elsif ($ret == -1) {
      # request is broken
  } else {
      # yourself

  # for HTTP clients
  my %special_headers = (
    'content-length' => undef,

  if($ret == -1) }
    # response is incomplete
  elsif($ret == -2) {
    # response is broken
  else {

    # the other values are the response messages. For example:
    # $status  = 200
    # $message = "OK"





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.


parse_http_request($request_string, \%env)





given request is corrupt


given request is incomplete

Note that the semantics of PATH_INFO is somewhat different from Apache. First, HTTP::Parser::XS does not validate the variable; it does not raise an error even if PATH_INFO does not start with "/". Second, the variable is conformant to RFC 3875 (and PSGI / Plack) in the fact that "//" and ".." appearing in PATH_INFO are preserved whereas Apache transcodes them.

parse_http_response($response_string, $header_format, \%special_headers)

Tries to parse given response string. $header_format must be HEADERS_AS_ARRAYREF, HEADERS_AS_HASHREF, or HEADERS_NONE, which are exportable constants.

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.

The return values are:


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.

If the given response string is broken or imcomplete, parse_http_response() returns only this value.






The HTTP status message. e.g. OK for success.


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.

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.

The number of headers


The size of header names


parse_http_request() returns -1 if too-long header names exist.

parse_http_request() simply ignores too-long header names.


Copyright 2009- Kazuho Oku






syntax highlighting: