use strict;
use Carp qw[ ];
-use List::Util qw[ first ];
-our $VERSION = '0.201';
+our $VERSION = 0.9;
our $TYPES = {
'application/octet-stream' => 'HTTP::Body::OctetStream',
'multipart/form-data' => 'HTTP::Body::MultiPart'
};
+require HTTP::Body::OctetStream;
+require HTTP::Body::UrlEncoded;
+require HTTP::Body::MultiPart;
+
=head1 NAME
HTTP::Body - HTTP Body Parser
=head1 DESCRIPTION
-HTTP Body Parser.
+HTTP::Body parses chunks of HTTP POST data and supports
+application/octet-stream, application/x-www-form-urlencoded, and
+multipart/form-data.
+
+It is currently used by L<Catalyst> to parse POST bodies.
=head1 METHODS
Carp::croak( $class, '->new( $content_type, $content_length )' );
}
- my $type = first { index( lc($content_type), $_ ) >= 0 } keys %{$TYPES};
+ my $type;
+ foreach my $supported ( keys %{$TYPES} ) {
+ if ( index( lc($content_type), $supported ) >= 0 ) {
+ $type = $supported;
+ }
+ }
+
my $body = $TYPES->{ $type || 'application/octet-stream' };
eval "require $body";
sub add {
my $self = shift;
+
+ my $cl = $self->content_length;
if ( defined $_[0] ) {
- $self->{buffer} .= $_[0];
$self->{length} += length( $_[0] );
+
+ # Don't allow buffer data to exceed content-length
+ if ( $self->{length} > $cl ) {
+ $_[0] = substr $_[0], 0, $cl - $self->{length};
+ $self->{length} = $cl;
+ }
+
+ $self->{buffer} .= $_[0];
}
unless ( $self->state eq 'done' ) {
$self->spin;
}
- return ( $self->length - $self->content_length );
+ return ( $self->length - $cl );
}
=item body