10 'application/octet-stream' => 'HTTP::Body::OctetStream',
11 'application/x-www-form-urlencoded' => 'HTTP::Body::UrlEncoded',
12 'multipart/form-data' => 'HTTP::Body::MultiPart'
15 require HTTP::Body::OctetStream;
16 require HTTP::Body::UrlEncoded;
17 require HTTP::Body::MultiPart;
21 HTTP::Body - HTTP Body Parser
27 sub handler : method {
28 my ( $class, $r ) = @_;
30 my $content_type = $r->headers_in->get('Content-Type');
31 my $content_length = $r->headers_in->get('Content-Length');
33 my $body = HTTP::Body->new( $content_type, $content_length );
34 my $length = $content_length;
38 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
40 $length -= length($buffer);
45 my $uploads = $body->upload; # hashref
46 my $params = $body->param; # hashref
47 my $body = $body->body; # IO::Handle
60 Constructor. Takes content type and content length as parameters,
61 returns a L<HTTP::Body> object.
66 my ( $class, $content_type, $content_length ) = @_;
69 Carp::croak( $class, '->new( $content_type, $content_length )' );
73 foreach my $supported ( keys %{$TYPES} ) {
74 if ( index( lc($content_type), $supported ) >= 0 ) {
79 my $body = $TYPES->{ $type || 'application/octet-stream' };
90 content_length => $content_length,
91 content_type => $content_type,
98 bless( $self, $body );
105 Add string to internal buffer. Will call spin unless done. returns
106 length before adding self.
113 if ( defined $_[0] ) {
114 $self->{buffer} .= $_[0];
115 $self->{length} += length( $_[0] );
118 unless ( $self->state eq 'done' ) {
122 return ( $self->length - $self->content_length );
127 accessor for the body.
133 $self->{body} = shift if @_;
134 return $self->{body};
139 read only accessor for the buffer.
144 return shift->{buffer};
149 read only accessor for content length
154 return shift->{content_length};
159 ready only accessor for the content type
164 return shift->{content_type};
179 read only accessor for body length.
184 return shift->{length};
189 Abstract method to spin the io handle.
194 Carp::croak('Define abstract method spin() in implementation');
199 accessor for body state.
205 $self->{state} = shift if @_;
206 return $self->{state};
211 accesor for http parameters.
220 my ( $name, $value ) = @_;
222 if ( exists $self->{param}->{$name} ) {
223 for ( $self->{param}->{$name} ) {
224 $_ = [$_] unless ref($_) eq "ARRAY";
229 $self->{param}->{$name} = $value;
233 return $self->{param};
245 my ( $name, $upload ) = @_;
247 if ( exists $self->{upload}->{$name} ) {
248 for ( $self->{upload}->{$name} ) {
249 $_ = [$_] unless ref($_) eq "ARRAY";
250 push( @$_, $upload );
254 $self->{upload}->{$name} = $upload;
258 return $self->{upload};
265 Chunked requests are currently not supported.
269 Christian Hansen, C<ch@ngmedia.com>
271 Sebastian Riedel, C<sri@cpan.org>
275 This library is free software. You can redistribute it and/or modify
276 it under the same terms as perl itself.