1 package HTTP::Body::Compat;
7 use Params::Validate qw[];
8 use HTTP::Body::Context qw[];
12 HTTP::Body::Compat - Backwards compataible HTTP Body Parser for versions <= 0.6
18 sub handler : method {
19 my ( $class, $r ) = @_;
21 my $content_type = $r->headers_in->get('Content-Type');
22 my $content_length = $r->headers_in->get('Content-Length');
24 # Calling HTTP::Body->new this way will go into pre 0.7 compat mode
25 my $body = HTTP::Body->new( $content_type, $content_length );
26 my $length = $content_length;
30 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
32 $length -= length($buffer);
37 my $uploads = $body->upload; # hashref
38 my $params = $body->param; # hashref
39 my $body = $body->body; # IO::Handle
52 Constructor. Takes content type and content length as parameters,
53 returns a L<HTTP::Body::Compat> object.
58 my $class = ref $_[0] ? ref shift : shift;
59 my ( $content_type, $content_length ) = Params::Validate::validate_with(
63 type => Params::Validate::SCALAR,
67 type => Params::Validate::SCALAR,
71 called => "$class\::new"
74 my $context = HTTP::Body::Context->new(
76 'Content-Type' => $content_type,
77 'Content-Length' => $content_length
81 return bless( {}, $class )->initialize( { context => $context } );
86 Add string to internal buffer. Returns length before adding string.
93 if ( defined $_[0] ) {
94 $self->{length} += bytes::length $_[0];
99 if ( $self->length == $self->content_length ) {
104 return ( $self->length - $self->content_length );
109 accessor for the body
114 return $_[0]->context->content;
123 Read-only accessor for content legnth
128 return $_[0]->context->content_length;
133 Read-only accessor for content type
138 return $_[0]->context->content_type;
142 return $_[0]->{length};
151 Accessor for HTTP parameters
159 return $self->context->param->add(@_);
162 return scalar $self->context->param->as_hash;
173 return $self->context->upload->add(@_);
176 return scalar $self->context->upload->as_hash;
183 Christian Hansen, C<ch@ngmedia.com>
185 This pod written by Ash Berlin, C<ash@cpan.org>.
189 This library is free software. You can redistribute it and/or modify
190 it under the same terms as perl itself.