/usr/share/perl5/HTTP/Body/MultiPart.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 | package HTTP::Body::MultiPart;
BEGIN {
$HTTP::Body::MultiPart::VERSION = '1.11';
}
use strict;
use base 'HTTP::Body';
use bytes;
use IO::File;
use File::Temp 0.14;
use File::Spec;
=head1 NAME
HTTP::Body::MultiPart - HTTP Body Multipart Parser
=head1 SYNOPSIS
use HTTP::Body::Multipart;
=head1 DESCRIPTION
HTTP Body Multipart Parser.
=head1 METHODS
=over 4
=item init
=cut
sub init {
my $self = shift;
unless ( $self->content_type =~ /boundary=\"?([^\";]+)\"?/ ) {
my $content_type = $self->content_type;
Carp::croak("Invalid boundary in content_type: '$content_type'");
}
$self->{boundary} = $1;
$self->{state} = 'preamble';
return $self;
}
=item spin
=cut
sub spin {
my $self = shift;
while (1) {
if ( $self->{state} =~ /^(preamble|boundary|header|body)$/ ) {
my $method = "parse_$1";
return unless $self->$method;
}
else {
Carp::croak('Unknown state');
}
}
}
=item boundary
=cut
sub boundary {
return shift->{boundary};
}
=item boundary_begin
=cut
sub boundary_begin {
return "--" . shift->boundary;
}
=item boundary_end
=cut
sub boundary_end {
return shift->boundary_begin . "--";
}
=item crlf
=cut
sub crlf () {
return "\x0d\x0a";
}
=item delimiter_begin
=cut
sub delimiter_begin {
my $self = shift;
return $self->crlf . $self->boundary_begin;
}
=item delimiter_end
=cut
sub delimiter_end {
my $self = shift;
return $self->crlf . $self->boundary_end;
}
=item parse_preamble
=cut
sub parse_preamble {
my $self = shift;
my $index = index( $self->{buffer}, $self->boundary_begin );
unless ( $index >= 0 ) {
return 0;
}
# replace preamble with CRLF so we can match dash-boundary as delimiter
substr( $self->{buffer}, 0, $index, $self->crlf );
$self->{state} = 'boundary';
return 1;
}
=item parse_boundary
=cut
sub parse_boundary {
my $self = shift;
if ( index( $self->{buffer}, $self->delimiter_begin . $self->crlf ) == 0 ) {
substr( $self->{buffer}, 0, length( $self->delimiter_begin ) + 2, '' );
$self->{part} = {};
$self->{state} = 'header';
return 1;
}
if ( index( $self->{buffer}, $self->delimiter_end . $self->crlf ) == 0 ) {
substr( $self->{buffer}, 0, length( $self->delimiter_end ) + 2, '' );
$self->{part} = {};
$self->{state} = 'done';
return 0;
}
return 0;
}
=item parse_header
=cut
sub parse_header {
my $self = shift;
my $crlf = $self->crlf;
my $index = index( $self->{buffer}, $crlf . $crlf );
unless ( $index >= 0 ) {
return 0;
}
my $header = substr( $self->{buffer}, 0, $index );
substr( $self->{buffer}, 0, $index + 4, '' );
my @headers;
for ( split /$crlf/, $header ) {
if (s/^[ \t]+//) {
$headers[-1] .= $_;
}
else {
push @headers, $_;
}
}
my $token = qr/[^][\x00-\x1f\x7f()<>@,;:\\"\/?={} \t]+/;
for my $header (@headers) {
$header =~ s/^($token):[\t ]*//;
( my $field = $1 ) =~ s/\b(\w)/uc($1)/eg;
if ( exists $self->{part}->{headers}->{$field} ) {
for ( $self->{part}->{headers}->{$field} ) {
$_ = [$_] unless ref($_) eq "ARRAY";
push( @$_, $header );
}
}
else {
$self->{part}->{headers}->{$field} = $header;
}
}
$self->{state} = 'body';
return 1;
}
=item parse_body
=cut
sub parse_body {
my $self = shift;
my $index = index( $self->{buffer}, $self->delimiter_begin );
if ( $index < 0 ) {
# make sure we have enough buffer to detect end delimiter
my $length = length( $self->{buffer} ) - ( length( $self->delimiter_end ) + 2 );
unless ( $length > 0 ) {
return 0;
}
$self->{part}->{data} .= substr( $self->{buffer}, 0, $length, '' );
$self->{part}->{size} += $length;
$self->{part}->{done} = 0;
$self->handler( $self->{part} );
return 0;
}
$self->{part}->{data} .= substr( $self->{buffer}, 0, $index, '' );
$self->{part}->{size} += $index;
$self->{part}->{done} = 1;
$self->handler( $self->{part} );
$self->{state} = 'boundary';
return 1;
}
=item handler
=cut
sub handler {
my ( $self, $part ) = @_;
unless ( exists $part->{name} ) {
my $disposition = $part->{headers}->{'Content-Disposition'};
my ($name) = $disposition =~ / name="?([^\";]+)"?/;
my ($filename) = $disposition =~ / filename="?([^\"]*)"?/;
# Need to match empty filenames above, so this part is flagged as an upload type
$part->{name} = $name;
if ( defined $filename ) {
$part->{filename} = $filename;
if ( $filename ne "" ) {
my $basename = (File::Spec->splitpath($filename))[2];
my $suffix = $basename =~ /[^.]+(\.[^\\\/]+)$/ ? $1 : q{};
my $fh = File::Temp->new( UNLINK => 0, DIR => $self->tmpdir, SUFFIX => $suffix );
$part->{fh} = $fh;
$part->{tempname} = $fh->filename;
}
}
}
if ( $part->{fh} && ( my $length = length( $part->{data} ) ) ) {
$part->{fh}->write( substr( $part->{data}, 0, $length, '' ), $length );
}
if ( $part->{done} ) {
if ( exists $part->{filename} ) {
if ( $part->{filename} ne "" ) {
$part->{fh}->close if defined $part->{fh};
delete @{$part}{qw[ data done fh ]};
$self->upload( $part->{name}, $part );
}
}
else {
$self->param( $part->{name}, $part->{data} );
}
}
}
=back
=head1 AUTHOR
Christian Hansen, C<ch@ngmedia.com>
=head1 CONTRIBUTORS
Torsten Raudssus, C<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;
|