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
|
=head1 NAME
Apache::Upload - Methods for dealing with file uploads.
=for testing
use Apache2;
use APR::Pool;
use Apache::Upload;
$r = APR::Pool->new;
$req = Apache::Request->new($r);
$u = Apache::Upload->new($r, name => "foo", file => __FILE__);
$req->body_status(0);
$req->parse;
$req->body->add($u);
open(my $fh, __FILE__) or die $!;
binmode $fh;
{
local $/;
$data = <$fh>;
}
close $fh;
ok length $data == -s __FILE__;
$data =~ s{\r}{}g;
=head1 SYNOPSIS
=for example begin
use Apache::Upload;
$req = Apache::Request->new($r);
$upload = $req->upload("foo");
$size = $upload->size;
# three methods to get at the upload's contents ... slurp, fh, io
$upload->slurp($slurp_data);
read $upload->fh, $fh_data, $size;
ok $slurp_data eq $fh_data;
$io = $upload->io;
print while <$io>;
=for example end
=for example_testing
ok $upload->bb->length == $size;
is $_STDOUT_, $data;
is $fh_data, $data;
is $slurp_data, $data;
=head1 DESCRIPTION
Apache::Upload is a new module based on the original package included
in Apache::Request 1.X. Users requiring the upload API must now
C<use Apache::Upload>, which adds the C<upload> method to Apache::Request.
Apache::Upload is largely backwards-compatible with the original 1.X API.
See the L<PORTING from 1.X> section below for a list of known issues.
This manpage documents the Apache::Upload and Apache::Upload::Brigade packages.
Apache::Upload::Table and Apache::Upload::Error are also provided by this module,
but are documented elsewhere. For a list of related manpages, read the L<SEE ALSO>
section below.
=head1 Apache::Upload
=head2 name
$upload->name()
The name of the HTML form element which generated the upload.
=for example_testing
is $upload->name, "foo";
=head2 filename
$upload->filename()
The (client-side) filename as submitted in the HTML form. Note:
some agents will submit the file's full pathname, while others
may submit just the basename.
=for example_testing
is $upload->filename, __FILE__;
=head2 fh
$upload->fh()
Creates filehandle reference to the upload's spooled tempfile,
which contains the full contents of the upload.
=head2 io
$upload->io()
Creates a tied IO handle. This method is a more efficient version
of C<fh>, but with C<io> the handle ref returned is not seekable.
It is tied to an Apache::Upload::Brigade object, so you may use the
brigade API on the tied object if you want to manipulate the IO stream
(beyond simply reading from it).
The returned reference is actually an object which has C<read> and
C<readline> methods available. However these methods are just
syntactic sugar for the underlying C<READ> and C<READLINE> methods from
Apache::Upload::Brigade.
=for example begin
$io = $upload->io;
print while $io->read($_); # equivalent to: tied(*$io)->READ($_)
=for example end
=for example_testing
is $_STDOUT_, $data;
See L<READ|read> and L<READLINE|readline> below for additional notes
on their usage.
=head2 bb
$upload->bb()
$upload->bb($set)
Get/set the APR::Brigade which represents the upload's contents.
=head2 size
$upload->size()
Returns the size of the upload in bytes.
=for example_testing
is $upload->size, -s __FILE__;
=head2 info
$upload->info()
$upload->info($set)
Get/set the additional header information table for the
uploaded file.
Returns a hash reference tied to the I<APR::Table> class.
An optional C<$table> argument can be passed to reassign
the upload's internal (apr_table_t) info table to the one
C<$table> represents.
=for example begin
my $info = $upload->info;
while (my($hdr_name, $hdr_value) = each %$info) {
# ...
}
# fetch upload's Content-Type header
my $type = $upload->info->{"Content-type"};
=for example end
=for example_testing
is $type, "application/octet-stream";
=head2 type
$upload->type()
Returns the MIME type of the given I<Apache::Upload> object.
=for example begin
my $type = $upload->type;
#same as
my $content_type = $upload->info->{"Content-Type"};
$content_type =~ s/;.*$//ms;
=for example end
=for example_testing
is $type, $content_type;
=head2 link
$upload->link()
To avoid recopying the upload's internal tempfile brigade on a
*nix-like system, I<link> will create a hard link to it:
=for example begin
my $upload = $req->upload('foo');
$upload->link("/path/to/newfile") or
die sprintf "link from '%s' failed: $!", $upload->tempname;
=for example end
Typically the new name must lie on the same device and partition
as the brigade's tempfile. If this or any other reason prevents
the OS from linking the files, C<link()> will instead
copy the temporary file to the specified location.
=head2 slurp
$upload->slurp($contents)
Reads the full contents of a file upload into the scalar argument.
The return value is the length of the file.
=for example begin
my $size = $upload->slurp($contents);
=for example end
=for example_testing
is $size, length $contents;
$contents =~ s{\r}{}g;
is $contents, $data;
=head2 tempname
$upload->tempname()
Provides the name of the spool file.
=for example begin
my $tempname = $upload->tempname;
=for example end
=for example_testing
like $tempname, qr/apreq.{6}$/;
=head1 Apache::Upload::Brigade
This class is derived from APR::Brigade, providing additional
methods for TIEHANDLE, READ and READLINE. To be memory efficient,
these methods delete buckets from the brigade as soon as their
data is actually read, so you cannot C<seek> on handles tied to
this class. Such handles have semantics similar to that of a
read-only socket.
=head2 TIEHANDLE
Apache::Upload::Brigade->TIEHANDLE($bb)
Creates a copy of the bucket brigade represented by $bb, and
blesses that copy into the Apache::Upload::Brigade class. This
provides syntactic sugar for using perl's builtin C<read>, C<readline>,
and C<< <> >> operations on handles tied to this package:
use Symbol;
$fh = gensym;
tie *$fh, "Apache::Upload:Brigade", $bb;
print while <$fh>;
=head2 READ
$bb->READ($contents)
$bb->READ($contents, $length)
$bb->READ($contents, $length, $offset)
Reads data from the brigade $bb into $contents. When omitted
$length defaults to C<-1>, which reads the first bucket into $contents.
A positive $length will read in $length bytes, or the remainder of the
brigade, whichever is greater. $offset represents the index in $context
to read the new data.
=head2 READLINE
$bb->READLINE()
Returns the first line of data from the bride. Lines are terminated by
linefeeds (the '\012' character), but we may eventually use C<$/> instead.
=head1 PORTING from 1.X
=over 4
=item * C<info($header_name)> is replaced by C<info($set)>.
=item * C<type()> returns only the MIME-type portion of the Content-Type header.
=back
=head1 SEE ALSO
L<Apache::Upload::Table>, L<Apache::Upload::Error>, L<Apache::Request>,
APR::Table(3)
=head1 COPYRIGHT
Copyright 2003-2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
|