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
52 HTTP::Body parses chunks of HTTP POST data and supports
53 application/octet-stream, application/x-www-form-urlencoded, and
56 It is currently used by L<Catalyst> to parse POST bodies.
64 Constructor. Takes content type and content length as parameters,
65 returns a L<HTTP::Body> object.
70 my ( $class, $content_type, $content_length ) = @_;
73 Carp::croak( $class, '->new( $content_type, $content_length )' );
77 foreach my $supported ( keys %{$TYPES} ) {
78 if ( index( lc($content_type), $supported ) >= 0 ) {
83 my $body = $TYPES->{ $type || 'application/octet-stream' };
94 content_length => $content_length,
95 content_type => $content_type,
102 bless( $self, $body );
109 Add string to internal buffer. Will call spin unless done. returns
110 length before adding self.
117 my $cl = $self->content_length;
119 if ( defined $_[0] ) {
120 $self->{length} += length( $_[0] );
122 # Don't allow buffer data to exceed content-length
123 if ( $self->{length} > $cl ) {
124 $_[0] = substr $_[0], 0, $cl - $self->{length};
125 $self->{length} = $cl;
128 $self->{buffer} .= $_[0];
131 unless ( $self->state eq 'done' ) {
135 return ( $self->length - $cl );
140 accessor for the body.
146 $self->{body} = shift if @_;
147 return $self->{body};
152 read only accessor for the buffer.
157 return shift->{buffer};
162 read only accessor for content length
167 return shift->{content_length};
172 ready only accessor for the content type
177 return shift->{content_type};
192 read only accessor for body length.
197 return shift->{length};
202 Abstract method to spin the io handle.
207 Carp::croak('Define abstract method spin() in implementation');
212 accessor for body state.
218 $self->{state} = shift if @_;
219 return $self->{state};
224 accesor for http parameters.
233 my ( $name, $value ) = @_;
235 if ( exists $self->{param}->{$name} ) {
236 for ( $self->{param}->{$name} ) {
237 $_ = [$_] unless ref($_) eq "ARRAY";
242 $self->{param}->{$name} = $value;
246 return $self->{param};
258 my ( $name, $upload ) = @_;
260 if ( exists $self->{upload}->{$name} ) {
261 for ( $self->{upload}->{$name} ) {
262 $_ = [$_] unless ref($_) eq "ARRAY";
263 push( @$_, $upload );
267 $self->{upload}->{$name} = $upload;
271 return $self->{upload};
278 Chunked requests are currently not supported.
282 Christian Hansen, C<ch@ngmedia.com>
284 Sebastian Riedel, C<sri@cpan.org>
288 This library is free software. You can redistribute it and/or modify
289 it under the same terms as perl itself.