6 use List::Util qw[ first ];
8 our $VERSION = '0.201';
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
57 Constructor. Takes content type and content length as parameters,
58 returns a L<HTTP::Body> object.
63 my ( $class, $content_type, $content_length ) = @_;
66 Carp::croak( $class, '->new( $content_type, $content_length )' );
69 my $type = first { index( lc($content_type), $_ ) >= 0 } keys %{$TYPES};
70 my $body = $TYPES->{ $type || 'application/octet-stream' };
81 content_length => $content_length,
82 content_type => $content_type,
89 bless( $self, $body );
96 Add string to internal buffer. Will call spin unless done. returns
97 length before adding self.
104 if ( defined $_[0] ) {
105 $self->{buffer} .= $_[0];
106 $self->{length} += length( $_[0] );
109 unless ( $self->state eq 'done' ) {
113 return ( $self->length - $self->content_length );
118 accessor for the body.
124 $self->{body} = shift if @_;
125 return $self->{body};
130 read only accessor for the buffer.
135 return shift->{buffer};
140 read only accessor for content length
145 return shift->{content_length};
150 ready only accessor for the content type
155 return shift->{content_type};
170 read only accessor for body length.
175 return shift->{length};
180 Abstract method to spin the io handle.
185 Carp::croak('Define abstract method spin() in implementation');
190 accessor for body state.
196 $self->{state} = shift if @_;
197 return $self->{state};
202 accesor for http parameters.
211 my ( $name, $value ) = @_;
213 if ( exists $self->{param}->{$name} ) {
214 for ( $self->{param}->{$name} ) {
215 $_ = [$_] unless ref($_) eq "ARRAY";
220 $self->{param}->{$name} = $value;
224 return $self->{param};
236 my ( $name, $upload ) = @_;
238 if ( exists $self->{upload}->{$name} ) {
239 for ( $self->{upload}->{$name} ) {
240 $_ = [$_] unless ref($_) eq "ARRAY";
241 push( @$_, $upload );
245 $self->{upload}->{$name} = $upload;
249 return $self->{upload};
256 Chunked requests are currently not supported.
260 Christian Hansen, C<ch@ngmedia.com>
262 Sebastian Riedel, C<sri@cpan.org>
266 This library is free software. You can redistribute it and/or modify
267 it under the same terms as perl itself.