use strict;
-use Carp qw[ ];
-use List::Util qw[ first ];
-use Scalar::Util qw[ blessed ];
-
-our $PARSERS = {
- 'application/octet-stream' => 'HTTP::Body::Octetstream',
- 'application/x-www-form-urlencoded' => 'HTTP::Body::Urlencoded',
- 'multipart/form-data' => 'HTTP::Body::Multipart'
+use Carp qw[ ];
+use List::Util qw[ first ];
+
+our $TYPES = {
+ 'application/octet-stream' => 'HTTP::Body::OctetStream',
+ 'application/x-www-form-urlencoded' => 'HTTP::Body::UrlEncoded',
+ 'multipart/form-data' => 'HTTP::Body::MultiPart'
};
sub new {
unless ( @_ == 3 ) {
Carp::croak( $class, '->new( $content_type, $content_length )' );
}
-
- my $type = first { index( lc($content_type), $_ ) >= 0 } keys %{ $PARSERS };
- my $body = $PARSERS->{ $type || 'application/octet-stream' };
-
+
+ my $type = first { index( lc($content_type), $_ ) >= 0 } keys %{$TYPES};
+ my $body = $TYPES->{ $type || 'application/octet-stream' };
+
eval "require $body";
-
- if ( $@ ) {
+
+ if ($@) {
die $@;
}
-
+
my $self = {
buffer => '',
content_length => $content_length,
content_type => $content_type,
- param => { },
- upload => { }
+ length => 0,
+ param => {},
+ state => 'buffering',
+ upload => {}
};
bless( $self, $body );
-
+
return $self->init;
}
sub add {
- Carp::croak('Define abstract method add() in implementation');
-}
+ my $self = shift;
-sub init {
- return $_[0];
+ if ( defined $_[0] ) {
+ $self->{buffer} .= $_[0];
+ $self->{length} += length( $_[0] );
+ }
+
+ unless ( $self->state eq 'done' ) {
+ $self->spin;
+ }
+
+ return ( $self->length - $self->content_length );
}
sub body {
return $self->{body};
}
+sub buffer {
+ return shift->{buffer};
+}
+
sub content_length {
return shift->{content_length};
}
return shift->{content_type};
}
+sub init {
+ return $_[0];
+}
+
+sub length {
+ return shift->{length};
+}
+
+sub spin {
+ Carp::croak('Define abstract method spin() in implementation');
+}
+
+sub state {
+ my $self = shift;
+ $self->{state} = shift if @_;
+ return $self->{state};
+}
+
sub param {
my $self = shift;