Added SYNOPSIS
[catagits/HTTP-Body.git] / lib / HTTP / Body.pm
index e89cb20..f0a75e1 100644 (file)
@@ -5,12 +5,55 @@ use strict;
 use Carp       qw[ ];
 use List::Util qw[ first ];
 
+our $VERSION = '0.01';
+
 our $TYPES = {
-    'application/octet-stream'          => 'HTTP::Body::Octetstream',
-    'application/x-www-form-urlencoded' => 'HTTP::Body::Urlencoded',
-    'multipart/form-data'               => 'HTTP::Body::Multipart'
+    'application/octet-stream'          => 'HTTP::Body::OctetStream',
+    'application/x-www-form-urlencoded' => 'HTTP::Body::UrlEncoded',
+    'multipart/form-data'               => 'HTTP::Body::MultiPart'
 };
 
+=head1 NAME
+
+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
+
+HTTP Body Parser.
+
+=head1 METHODS
+
+=over 4
+
+=cut
+
 sub new {
     my ( $class, $content_type, $content_length ) = @_;
 
@@ -29,6 +72,7 @@ sub new {
 
     my $self = {
         buffer         => '',
+        body           => undef,
         content_length => $content_length,
         content_type   => $content_type,
         length         => 0,
@@ -42,6 +86,10 @@ sub new {
     return $self->init;
 }
 
+=item add
+
+=cut
+
 sub add {
     my $self = shift;
 
@@ -49,7 +97,7 @@ sub add {
         $self->{buffer} .= $_[0];
         $self->{length} += length( $_[0] );
     }
-    
+
     unless ( $self->state eq 'done' ) {
         $self->spin;
     }
@@ -57,42 +105,78 @@ sub add {
     return ( $self->length - $self->content_length );
 }
 
+=item body
+
+=cut
+
 sub body {
     my $self = shift;
     $self->{body} = shift if @_;
     return $self->{body};
 }
 
+=item buffer
+
+=cut
+
 sub buffer {
     return shift->{buffer};
 }
 
+=item content_length
+
+=cut
+
 sub content_length {
     return shift->{content_length};
 }
 
+=item content_type
+
+=cut
+
 sub content_type {
     return shift->{content_type};
 }
 
+=item init
+
+=cut
+
 sub init {
     return $_[0];
 }
 
+=item length
+
+=cut
+
 sub length {
     return shift->{length};
 }
 
+=item spin
+
+=cut
+
 sub spin {
     Carp::croak('Define abstract method spin() in implementation');
 }
 
+=item state
+
+=cut
+
 sub state {
     my $self = shift;
     $self->{state} = shift if @_;
-    return $self->{state};    
+    return $self->{state};
 }
 
+=item param
+
+=cut
+
 sub param {
     my $self = shift;
 
@@ -114,6 +198,10 @@ sub param {
     return $self->{param};
 }
 
+=item upload
+
+=cut
+
 sub upload {
     my $self = shift;
 
@@ -135,4 +223,19 @@ sub upload {
     return $self->{upload};
 }
 
+=back
+
+=head1 AUTHOR
+
+Christian Hansen, C<ch@ngmedia.com>
+
+Sebastian Riedel, C<sri@cpan.org>
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify 
+it under the same terms as perl itself.
+
+=cut
+
 1;