10 'application/octet-stream' => 'HTTP::Body::OctetStream',
11 'application/x-www-form-urlencoded' => 'HTTP::Body::UrlEncoded',
12 'multipart/form-data' => 'HTTP::Body::MultiPart'
17 HTTP::Body - HTTP Body Parser
23 sub handler : method {
24 my ( $class, $r ) = @_;
26 my $content_type = $r->headers_in->get('Content-Type');
27 my $content_length = $r->headers_in->get('Content-Length');
29 my $body = HTTP::Body->new( $content_type, $content_length );
30 my $length = $content_length;
34 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
36 $length -= length($buffer);
41 my $uploads = $body->upload; # hashref
42 my $params = $body->param; # hashref
43 my $body = $body->body; # IO::Handle
56 Constructor. Takes content type and content length as parameters,
57 returns a L<HTTP::Body> object.
62 my ( $class, $content_type, $content_length ) = @_;
65 Carp::croak( $class, '->new( $content_type, $content_length )' );
69 foreach my $supported ( keys %{$TYPES} ) {
70 if ( index( lc($content_type), $supported ) >= 0 ) {
75 my $body = $TYPES->{ $type || 'application/octet-stream' };
86 content_length => $content_length,
87 content_type => $content_type,
94 bless( $self, $body );
101 Add string to internal buffer. Will call spin unless done. returns
102 length before adding self.
109 if ( defined $_[0] ) {
110 $self->{buffer} .= $_[0];
111 $self->{length} += length( $_[0] );
114 unless ( $self->state eq 'done' ) {
118 return ( $self->length - $self->content_length );
123 accessor for the body.
129 $self->{body} = shift if @_;
130 return $self->{body};
135 read only accessor for the buffer.
140 return shift->{buffer};
145 read only accessor for content length
150 return shift->{content_length};
155 ready only accessor for the content type
160 return shift->{content_type};
175 read only accessor for body length.
180 return shift->{length};
185 Abstract method to spin the io handle.
190 Carp::croak('Define abstract method spin() in implementation');
195 accessor for body state.
201 $self->{state} = shift if @_;
202 return $self->{state};
207 accesor for http parameters.
216 my ( $name, $value ) = @_;
218 if ( exists $self->{param}->{$name} ) {
219 for ( $self->{param}->{$name} ) {
220 $_ = [$_] unless ref($_) eq "ARRAY";
225 $self->{param}->{$name} = $value;
229 return $self->{param};
241 my ( $name, $upload ) = @_;
243 if ( exists $self->{upload}->{$name} ) {
244 for ( $self->{upload}->{$name} ) {
245 $_ = [$_] unless ref($_) eq "ARRAY";
246 push( @$_, $upload );
250 $self->{upload}->{$name} = $upload;
254 return $self->{upload};
261 Chunked requests are currently not supported.
265 Christian Hansen, C<ch@ngmedia.com>
267 Sebastian Riedel, C<sri@cpan.org>
271 This library is free software. You can redistribute it and/or modify
272 it under the same terms as perl itself.