From: Andy Grundman Date: Mon, 23 Jun 2008 19:41:32 +0000 (+0000) Subject: HTTP::Body 1.04, patch from jgoulah for tmpdir() accessor X-Git-Tag: v1.04^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Ftags%2Fv1.04;p=catagits%2FHTTP-Body.git HTTP::Body 1.04, patch from jgoulah for tmpdir() accessor --- diff --git a/Changes b/Changes index 5c95dc4..f65b6ed 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ This file documents the revision history for Perl extension HTTP::Body. +1.04 2008-06-23 16:00:00 + - Added tmpdir() accessor to specify an alternate directory for temp files. + (jgoulah) + 1.03 2008-04-07 08:20:00 - Set body value for XForms data. (Daniel Ruoso) diff --git a/README b/README index a49c543..aeb552d 100644 --- a/README +++ b/README @@ -84,6 +84,10 @@ METHODS upload Get/set file uploads. + tmpdir + Specify a different path for temporary files. Defaults to the system + temporary path. + AUTHOR Christian Hansen, "ch@ngmedia.com" diff --git a/lib/HTTP/Body.pm b/lib/HTTP/Body.pm index 7c1d5e0..12216d6 100644 --- a/lib/HTTP/Body.pm +++ b/lib/HTTP/Body.pm @@ -4,7 +4,7 @@ use strict; use Carp qw[ ]; -our $VERSION = '1.03'; +our $VERSION = '1.04'; our $TYPES = { 'application/octet-stream' => 'HTTP::Body::OctetStream', @@ -113,7 +113,8 @@ sub new { length => 0, param => {}, state => 'buffering', - upload => {} + upload => {}, + tmpdir => File::Spec->tmpdir(), }; bless( $self, $body ); @@ -355,6 +356,18 @@ sub 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}; +} + =back =head1 AUTHOR diff --git a/lib/HTTP/Body/MultiPart.pm b/lib/HTTP/Body/MultiPart.pm index 763838b..78212cf 100644 --- a/lib/HTTP/Body/MultiPart.pm +++ b/lib/HTTP/Body/MultiPart.pm @@ -270,7 +270,7 @@ sub handler { $part->{filename} = $filename; if ( $filename ne "" ) { - my $fh = File::Temp->new( UNLINK => 0 ); + my $fh = File::Temp->new( UNLINK => 0, DIR => $self->tmpdir ); $part->{fh} = $fh; $part->{tempname} = $fh->filename; diff --git a/lib/HTTP/Body/OctetStream.pm b/lib/HTTP/Body/OctetStream.pm index 05c3cd2..00efce0 100644 --- a/lib/HTTP/Body/OctetStream.pm +++ b/lib/HTTP/Body/OctetStream.pm @@ -30,7 +30,7 @@ sub spin { my $self = shift; unless ( $self->body ) { - $self->body( File::Temp->new ); + $self->body( File::Temp->new( DIR => $self->tmpdir ) ); } if ( my $length = length( $self->{buffer} ) ) { diff --git a/t/04multipart.t b/t/04multipart.t index 1783429..1b27ebd 100644 --- a/t/04multipart.t +++ b/t/04multipart.t @@ -3,13 +3,14 @@ use strict; use warnings; -use Test::More tests => 60; +use Test::More tests => 98; use Cwd; use HTTP::Body; use File::Spec::Functions; use IO::File; use YAML; +use File::Temp qw/ tempdir /; my $path = catdir( getcwd(), 't', 'data', 'multipart' ); @@ -20,6 +21,10 @@ for ( my $i = 1; $i <= 12; $i++ ) { my $results = YAML::LoadFile( catfile( $path, "$test-results.yml" ) ); my $content = IO::File->new( catfile( $path, "$test-content.dat" ) ); my $body = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} ); + my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() ); + $body->tmpdir($tempdir); + + my $regex_tempdir = quotemeta($tempdir); binmode $content, ':raw'; @@ -35,6 +40,7 @@ for ( my $i = 1; $i <= 12; $i++ ) { my $value = $body->upload->{$field}; for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) { + like($_->{tempname}, qr{$regex_tempdir}, "has tmpdir $tempdir"); push @temps, delete $_->{tempname}; } }