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 my $cl = $self->content_length;
115 if ( defined $_[0] ) {
116 $self->{length} += length( $_[0] );
118 # Don't allow buffer data to exceed content-length
119 if ( $self->{length} > $cl ) {
120 $_[0] = substr $_[0], 0, $cl - $self->{length};
121 $self->{length} = $cl;
124 $self->{buffer} .= $_[0];
127 unless ( $self->state eq 'done' ) {
131 return ( $self->length - $cl );
136 accessor for the body.
142 $self->{body} = shift if @_;
143 return $self->{body};
148 read only accessor for the buffer.
153 return shift->{buffer};
158 read only accessor for content length
163 return shift->{content_length};
168 ready only accessor for the content type
173 return shift->{content_type};
188 read only accessor for body length.
193 return shift->{length};
198 Abstract method to spin the io handle.
203 Carp::croak('Define abstract method spin() in implementation');
208 accessor for body state.
214 $self->{state} = shift if @_;
215 return $self->{state};
220 accesor for http parameters.
229 my ( $name, $value ) = @_;
231 if ( exists $self->{param}->{$name} ) {
232 for ( $self->{param}->{$name} ) {
233 $_ = [$_] unless ref($_) eq "ARRAY";
238 $self->{param}->{$name} = $value;
242 return $self->{param};
254 my ( $name, $upload ) = @_;
256 if ( exists $self->{upload}->{$name} ) {
257 for ( $self->{upload}->{$name} ) {
258 $_ = [$_] unless ref($_) eq "ARRAY";
259 push( @$_, $upload );
263 $self->{upload}->{$name} = $upload;
267 return $self->{upload};
274 Chunked requests are currently not supported.
278 Christian Hansen, C<ch@ngmedia.com>
280 Sebastian Riedel, C<sri@cpan.org>
284 This library is free software. You can redistribute it and/or modify
285 it under the same terms as perl itself.