1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
package HTTP::Body;
use strict;
use Carp qw[ ];
our $VERSION = 0.6;
our $TYPES = {
'application/octet-stream' => 'HTTP::Body::OctetStream',
'application/x-www-form-urlencoded' => 'HTTP::Body::UrlEncoded',
'multipart/form-data' => 'HTTP::Body::MultiPart'
};
require HTTP::Body::OctetStream;
require HTTP::Body::UrlEncoded;
require HTTP::Body::MultiPart;
=head1 NAME
HTTP::Body - HTTP Body Parser
=head1 SYNOPSIS
use HTTP::Body;
sub handler : method {
my ( $class, $r ) = @_;
my $content_type = $r->headers_in->get('Content-Type');
my $content_length = $r->headers_in->get('Content-Length');
my $body = HTTP::Body->new( $content_type, $content_length );
my $length = $content_length;
while ( $length ) {
$r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
$length -= length($buffer);
$body->add($buffer);
}
my $uploads = $body->upload; # hashref
my $params = $body->param; # hashref
my $body = $body->body; # IO::Handle
}
=head1 DESCRIPTION
HTTP Body Parser.
=head1 METHODS
=over 4
=item new
Constructor. Takes content type and content length as parameters,
returns a L<HTTP::Body> object.
=cut
sub new {
my ( $class, $content_type, $content_length ) = @_;
unless ( @_ == 3 ) {
Carp::croak( $class, '->new( $content_type, $content_length )' );
}
my $type;
foreach my $supported ( keys %{$TYPES} ) {
if ( index( lc($content_type), $supported ) >= 0 ) {
$type = $supported;
}
}
my $body = $TYPES->{ $type || 'application/octet-stream' };
eval "require $body";
if ($@) {
die $@;
}
my $self = {
buffer => '',
body => undef,
content_length => $content_length,
content_type => $content_type,
length => 0,
param => {},
state => 'buffering',
upload => {}
};
bless( $self, $body );
return $self->init;
}
=item add
Add string to internal buffer. Will call spin unless done. returns
length before adding self.
=cut
sub add {
my $self = shift;
if ( defined $_[0] ) {
$self->{buffer} .= $_[0];
$self->{length} += length( $_[0] );
}
unless ( $self->state eq 'done' ) {
$self->spin;
}
return ( $self->length - $self->content_length );
}
=item body
accessor for the body.
=cut
sub body {
my $self = shift;
$self->{body} = shift if @_;
return $self->{body};
}
=item buffer
read only accessor for the buffer.
=cut
sub buffer {
return shift->{buffer};
}
=item content_length
read only accessor for content length
=cut
sub content_length {
return shift->{content_length};
}
=item content_type
ready only accessor for the content type
=cut
sub content_type {
return shift->{content_type};
}
=item init
return self.
=cut
sub init {
return $_[0];
}
=item length
read only accessor for body length.
=cut
sub length {
return shift->{length};
}
=item spin
Abstract method to spin the io handle.
=cut
sub spin {
Carp::croak('Define abstract method spin() in implementation');
}
=item state
accessor for body state.
=cut
sub state {
my $self = shift;
$self->{state} = shift if @_;
return $self->{state};
}
=item param
accesor for http parameters.
=cut
sub param {
my $self = shift;
if ( @_ == 2 ) {
my ( $name, $value ) = @_;
if ( exists $self->{param}->{$name} ) {
for ( $self->{param}->{$name} ) {
$_ = [$_] unless ref($_) eq "ARRAY";
push( @$_, $value );
}
}
else {
$self->{param}->{$name} = $value;
}
}
return $self->{param};
}
=item upload
=cut
sub upload {
my $self = shift;
if ( @_ == 2 ) {
my ( $name, $upload ) = @_;
if ( exists $self->{upload}->{$name} ) {
for ( $self->{upload}->{$name} ) {
$_ = [$_] unless ref($_) eq "ARRAY";
push( @$_, $upload );
}
}
else {
$self->{upload}->{$name} = $upload;
}
}
return $self->{upload};
}
=back
=head1 BUGS
Chunked requests are currently not supported.
=head1 AUTHOR
Christian Hansen, C<ch@ngmedia.com>
Sebastian Riedel, C<sri@cpan.org>
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as perl itself.
=cut
1;
|