HTTP-Response-Parser

 view release on metacpan or  search on metacpan

lib/HTTP/Response/Parser.pm  view on Meta::CPAN


  use HTTP::Response::Parser qw(parse parse_http_response);
  
  $res = HTTP::Response::Parser::parse("HTTP/1.1 200 OK\r\n\r\n", "Content body");
   or
  $res = HTTP::Response::Parser::parse("HTTP/1.1 200 OK\r\n\r\nContent Body");
  if ($res) {
      $res->isa('HTTP::Response'); # true
      $res->{_headers}->isa('HTTP::Headers'); # true
  } else {
      # something wrong
  }
  
  # parse header only, return parsed bytes length.
  $res = {};
  $parsed = parse_http_response("HTTP/1.1 200 OK\r\n\r\nContent", $res); # return n bytes

  if ($parsed == -1) {
      # invalid response, maybe this is not HTTP Response
  } elsif ($parsed == -2) {
      # parsed correctly, but incomplete response. 
  } else {
      $parsed; # length of "HTTP/1.1 200 OK\r\n\r\n"
      $res->{_rc}; # 200
      $res->{_protocol}; # HTTP/1.1
      $res->{_msg}; # OK
      $res->{_headers}; # just a HASH
      $res->isa('HTTP::Response'); # false
  }


=head1 DESCRIPTION

This is a fast HTTP response parser. Create L<HTTP::Response> object same as HTTP::Response->parse.

XS parser is 10x faster than HTTP::Response, so that's useful for high performance crawler or HTTP-based RPC.

If you want incremental parser, you can use L<HTTP::Parser>. And see also L<HTTP::Parser::XS>, if you want faster request parser.

This module is using picohttpparser(http://github.com/kazuho/picohttpparser) by kazuho oku.

=head1 GLOBAL VARIABLES

=over 4

=item $HTTP::Response::Parser::RESPONSE_CLASS

The class of response object. (Default is 'HTTP::Response')

If set empty string then parse() function return a HASH that not blessed.

=item $HTTP::Response::Parser::HEADER_CLASS

The class of $res->{_headers}. (Default is 'HTTP::Headers')

=head1 BENCHMARK

Compare with HTTP::Response->parse.

 parse small_header
 Benchmark: timing 20000 iterations of parse, xs...
 parse: 11 wallclock secs ( 5.05 usr +  0.01 sys =  5.06 CPU) @ 3952.57/s (n=20000)
 xs:  2 wallclock secs ( 0.63 usr +  0.00 sys =  0.63 CPU) @ 31746.03/s (n=20000)

 parse large_header
 Benchmark: timing 20000 iterations of parse, xs...
 parse: 26 wallclock secs (15.33 usr +  0.10 sys = 15.43 CPU) @ 1296.18/s (n=20000)
 xs:  2 wallclock secs ( 1.22 usr +  0.00 sys =  1.22 CPU) @ 16393.44/s (n=20000)


=head1 EXPORTS

Nothing by default. You can import "parse", "parse_http_response", and ":all".

=head1 AUTHOR

mala E<lt>cpan@ma.laE<gt>

=head1 THANKS TO

kazuho oku, tokuhirom

=head1 SEE ALSO

L<HTTP::Response>, L<HTTP::Parser>, L<HTTP::Parser::XS>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut



( run in 0.549 second using v1.01-cache-2.11-cpan-71847e10f99 )