Added benchmark
[catagits/HTTP-Body.git] / lib / HTTP / Body.pm
index 9af71ee..12944df 100644 (file)
@@ -2,12 +2,10 @@ package HTTP::Body;
 
 use strict;
 
-use Carp qw[ ];
+use Carp       qw[ ];
 use List::Util qw[ first ];
 
-use overload ( q/""/ => 'stringify', fallback => 1 );
-
-our $VERSION = '0.01';
+our $VERSION = '0.201';
 
 our $TYPES = {
     'application/octet-stream'          => 'HTTP::Body::OctetStream',
@@ -22,6 +20,29 @@ HTTP::Body - HTTP Body Parser
 =head1 SYNOPSIS
 
     use HTTP::Body;
+    
+    sub handler : method {
+        my ( $class, $r ) = @_;
+
+        my $content_type   = $r->headers_in->get('Content-Type');
+        my $content_length = $r->headers_in->get('Content-Length');
+        
+        my $body   = HTTP::Body->new( $content_type, $content_length );
+        my $length = $content_length;
+
+        while ( $length ) {
+
+            $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
+
+            $length -= length($buffer);
+            
+            $body->add($buffer);
+        }
+        
+        my $uploads = $body->upload; # hashref
+        my $params  = $body->param;  # hashref
+        my $body    = $body->body;   # IO::Handle
+    }
 
 =head1 DESCRIPTION
 
@@ -29,7 +50,12 @@ HTTP Body Parser.
 
 =head1 METHODS
 
-=over 4
+=over 4 
+
+=item new 
+
+Constructor. Takes content type and content length as parameters,
+returns a L<HTTP::Body> object.
 
 =cut
 
@@ -51,7 +77,7 @@ sub new {
 
     my $self = {
         buffer         => '',
-        body           => '',
+        body           => undef,
         content_length => $content_length,
         content_type   => $content_type,
         length         => 0,
@@ -67,6 +93,9 @@ sub new {
 
 =item add
 
+Add string to internal buffer. Will call spin unless done. returns
+length before adding self.
+
 =cut
 
 sub add {
@@ -74,7 +103,6 @@ sub add {
 
     if ( defined $_[0] ) {
         $self->{buffer} .= $_[0];
-        $self->{body}   .= $_[0];
         $self->{length} += length( $_[0] );
     }
 
@@ -87,6 +115,8 @@ sub add {
 
 =item body
 
+accessor for the body.
+
 =cut
 
 sub body {
@@ -97,6 +127,8 @@ sub body {
 
 =item buffer
 
+read only accessor for the buffer.
+
 =cut
 
 sub buffer {
@@ -105,6 +137,8 @@ sub buffer {
 
 =item content_length
 
+read only accessor for content length
+
 =cut
 
 sub content_length {
@@ -113,6 +147,8 @@ sub content_length {
 
 =item content_type
 
+ready only accessor for the content type
+
 =cut
 
 sub content_type {
@@ -121,6 +157,8 @@ sub content_type {
 
 =item init
 
+return self.
+
 =cut
 
 sub init {
@@ -129,6 +167,8 @@ sub init {
 
 =item length
 
+read only accessor for body length.
+
 =cut
 
 sub length {
@@ -137,6 +177,8 @@ sub length {
 
 =item spin
 
+Abstract method to spin the io handle.
+
 =cut
 
 sub spin {
@@ -145,6 +187,8 @@ sub spin {
 
 =item state
 
+accessor for body state.
+
 =cut
 
 sub state {
@@ -153,16 +197,10 @@ sub state {
     return $self->{state};
 }
 
-=item stringify
-
-=cut
-
-sub stringify {
-    return shift->{body};
-}
-
 =item param
 
+accesor for http parameters.
+
 =cut
 
 sub param {
@@ -213,14 +251,19 @@ sub upload {
 
 =back
 
+=head1 BUGS
+
+Chunked requests are currently not supported.
+
 =head1 AUTHOR
 
 Christian Hansen, C<ch@ngmedia.com>
-Messed up by Sebastian Riedel
+
+Sebastian Riedel, C<sri@cpan.org>
 
 =head1 LICENSE
 
-This library is free software . You can redistribute it and/or modify 
+This library is free software. You can redistribute it and/or modify 
 it under the same terms as perl itself.
 
 =cut