/usr/share/perl5/HTTP/Body.pm is in libhttp-body-perl 1.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | package HTTP::Body;
BEGIN {
$HTTP::Body::VERSION = '1.11';
}
use strict;
use Carp qw[ ];
our $TYPES = {
'application/octet-stream' => 'HTTP::Body::OctetStream',
'application/x-www-form-urlencoded' => 'HTTP::Body::UrlEncoded',
'multipart/form-data' => 'HTTP::Body::MultiPart',
'multipart/related' => 'HTTP::Body::XFormsMultipart',
'application/xml' => 'HTTP::Body::XForms'
};
require HTTP::Body::OctetStream;
require HTTP::Body::UrlEncoded;
require HTTP::Body::MultiPart;
require HTTP::Body::XFormsMultipart;
require HTTP::Body::XForms;
use HTTP::Headers;
use HTTP::Message;
=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 $param_order = $body->param_order # arrayref
my $body = $body->body; # IO::Handle
}
=head1 DESCRIPTION
HTTP::Body parses chunks of HTTP POST data and supports
application/octet-stream, application/x-www-form-urlencoded, and
multipart/form-data.
Chunked bodies are supported by not passing a length value to new().
It is currently used by L<Catalyst> to parse POST bodies.
=head1 NOTES
When parsing multipart bodies, temporary files are created to store any
uploaded files. You must delete these temporary files yourself after
processing them, or set $body->cleanup(1) to automatically delete them
at DESTROY-time.
=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 ( @_ >= 2 ) {
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' };
my $self = {
cleanup => 0,
buffer => '',
chunk_buffer => '',
body => undef,
chunked => !defined $content_length,
content_length => defined $content_length ? $content_length : -1,
content_type => $content_type,
length => 0,
param => {},
param_order => [],
state => 'buffering',
upload => {},
tmpdir => File::Spec->tmpdir(),
};
bless( $self, $body );
return $self->init;
}
sub DESTROY {
my $self = shift;
if ( $self->{cleanup} ) {
my @temps = ();
for my $upload ( values %{ $self->{upload} } ) {
push @temps, map { $_->{tempname} || () }
( ref $upload eq 'ARRAY' ? @{$upload} : $upload );
}
unlink map { $_ } grep { -e $_ } @temps;
}
}
=item add
Add string to internal buffer. Will call spin unless done. returns
length before adding self.
=cut
sub add {
my $self = shift;
if ( $self->{chunked} ) {
$self->{chunk_buffer} .= $_[0];
while ( $self->{chunk_buffer} =~ m/^([\da-fA-F]+).*\x0D\x0A/ ) {
my $chunk_len = hex($1);
if ( $chunk_len == 0 ) {
# Strip chunk len
$self->{chunk_buffer} =~ s/^([\da-fA-F]+).*\x0D\x0A//;
# End of data, there may be trailing headers
if ( my ($headers) = $self->{chunk_buffer} =~ m/(.*)\x0D\x0A/s ) {
if ( my $message = HTTP::Message->parse( $headers ) ) {
$self->{trailing_headers} = $message->headers;
}
}
$self->{chunk_buffer} = '';
# Set content_length equal to the amount of data we read,
# so the spin methods can finish up.
$self->{content_length} = $self->{length};
}
else {
# Make sure we have the whole chunk in the buffer (+CRLF)
if ( length( $self->{chunk_buffer} ) >= $chunk_len ) {
# Strip chunk len
$self->{chunk_buffer} =~ s/^([\da-fA-F]+).*\x0D\x0A//;
# Pull chunk data out of chunk buffer into real buffer
$self->{buffer} .= substr $self->{chunk_buffer}, 0, $chunk_len, '';
# Strip remaining CRLF
$self->{chunk_buffer} =~ s/^\x0D\x0A//;
$self->{length} += $chunk_len;
}
else {
# Not enough data for this chunk, wait for more calls to add()
return;
}
}
unless ( $self->{state} eq 'done' ) {
$self->spin;
}
}
return;
}
my $cl = $self->content_length;
if ( defined $_[0] ) {
$self->{length} += length( $_[0] );
# Don't allow buffer data to exceed content-length
if ( $self->{length} > $cl ) {
$_[0] = substr $_[0], 0, $cl - $self->{length};
$self->{length} = $cl;
}
$self->{buffer} .= $_[0];
}
unless ( $self->state eq 'done' ) {
$self->spin;
}
return ( $self->length - $cl );
}
=item body
accessor for the body.
=cut
sub body {
my $self = shift;
$self->{body} = shift if @_;
return $self->{body};
}
=item chunked
Returns 1 if the request is chunked.
=cut
sub chunked {
return shift->{chunked};
}
=item cleanup
Set to 1 to enable automatic deletion of temporary files at DESTROY-time.
=cut
sub cleanup {
my $self = shift;
$self->{cleanup} = shift if @_;
return $self->{cleanup};
}
=item content_length
Returns the content-length for the body data if known.
Returns -1 if the request is chunked.
=cut
sub content_length {
return shift->{content_length};
}
=item content_type
Returns the content-type of the body data.
=cut
sub content_type {
return shift->{content_type};
}
=item init
return self.
=cut
sub init {
return $_[0];
}
=item length
Returns the total length of data we expect to read if known.
In the case of a chunked request, returns the amount of data
read so far.
=cut
sub length {
return shift->{length};
}
=item trailing_headers
If a chunked request body had trailing headers, trailing_headers will
return an HTTP::Headers object populated with those headers.
=cut
sub trailing_headers {
return shift->{trailing_headers};
}
=item spin
Abstract method to spin the io handle.
=cut
sub spin {
Carp::croak('Define abstract method spin() in implementation');
}
=item state
Returns the current state of the parser.
=cut
sub state {
my $self = shift;
$self->{state} = shift if @_;
return $self->{state};
}
=item param
Get/set body 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;
}
push @{$self->{param_order}}, $name;
}
return $self->{param};
}
=item upload
Get/set file uploads.
=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};
}
=item tmpdir
Specify a different path for temporary files. Defaults to the system temporary path.
=cut
sub tmpdir {
my $self = shift;
$self->{tmpdir} = shift if @_;
return $self->{tmpdir};
}
=item param_order
Returns the array ref of the param keys in the order how they appeared on the body
=cut
sub param_order {
return shift->{param_order};
}
=back
=head1 SUPPORT
Since its original creation this module has been taken over by the Catalyst
development team. If you want to contribute patches, these will be your
primary contact points:
IRC:
Join #catalyst-dev on irc.perl.org.
Mailing Lists:
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
=head1 AUTHOR
Christian Hansen, C<chansen@cpan.org>
Sebastian Riedel, C<sri@cpan.org>
Andy Grundman, C<andy@hybridized.org>
=head1 CONTRIBUTORS
Simon Elliott C<cpan@papercreatures.com>
Kent Fredric <kentnl@cpan.org>
Christian Walde
Torsten Raudssus <torsten@raudssus.de>
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as perl itself.
=cut
1;
|