6 use List::Util qw[ first ];
11 'application/octet-stream' => 'HTTP::Body::OctetStream',
12 'application/x-www-form-urlencoded' => 'HTTP::Body::UrlEncoded',
13 'multipart/form-data' => 'HTTP::Body::MultiPart'
18 HTTP::Body - HTTP Body Parser
24 sub handler : method {
25 my ( $class, $r ) = @_;
27 my $content_type = $r->headers_in->get('Content-Type');
28 my $content_length = $r->headers_in->get('Content-Length');
30 my $body = HTTP::Body->new( $content_type, $content_length );
31 my $length = $content_length;
35 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
37 $length -= length($buffer);
42 my $uploads = $body->upload; # hashref
43 my $params = $body->param; # hashref
44 my $body = $body->body; # IO::Handle
58 my ( $class, $content_type, $content_length ) = @_;
61 Carp::croak( $class, '->new( $content_type, $content_length )' );
64 my $type = first { index( lc($content_type), $_ ) >= 0 } keys %{$TYPES};
65 my $body = $TYPES->{ $type || 'application/octet-stream' };
76 content_length => $content_length,
77 content_type => $content_type,
84 bless( $self, $body );
96 if ( defined $_[0] ) {
97 $self->{buffer} .= $_[0];
98 $self->{length} += length( $_[0] );
101 unless ( $self->state eq 'done' ) {
105 return ( $self->length - $self->content_length );
114 $self->{body} = shift if @_;
115 return $self->{body};
123 return shift->{buffer};
131 return shift->{content_length};
139 return shift->{content_type};
155 return shift->{length};
163 Carp::croak('Define abstract method spin() in implementation');
172 $self->{state} = shift if @_;
173 return $self->{state};
185 my ( $name, $value ) = @_;
187 if ( exists $self->{param}->{$name} ) {
188 for ( $self->{param}->{$name} ) {
189 $_ = [$_] unless ref($_) eq "ARRAY";
194 $self->{param}->{$name} = $value;
198 return $self->{param};
210 my ( $name, $upload ) = @_;
212 if ( exists $self->{upload}->{$name} ) {
213 for ( $self->{upload}->{$name} ) {
214 $_ = [$_] unless ref($_) eq "ARRAY";
215 push( @$_, $upload );
219 $self->{upload}->{$name} = $upload;
223 return $self->{upload};
230 Christian Hansen, C<ch@ngmedia.com>
232 Sebastian Riedel, C<sri@cpan.org>
236 This library is free software. You can redistribute it and/or modify
237 it under the same terms as perl itself.