HTTP::Body 1.04, patch from jgoulah for tmpdir() accessor v1.04
Andy Grundman [Mon, 23 Jun 2008 19:41:32 +0000 (19:41 +0000)]
Changes
README
lib/HTTP/Body.pm
lib/HTTP/Body/MultiPart.pm
lib/HTTP/Body/OctetStream.pm
t/04multipart.t

diff --git a/Changes b/Changes
index 5c95dc4..f65b6ed 100644 (file)
--- 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 (file)
--- 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"
 
index 7c1d5e0..12216d6 100644 (file)
@@ -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
index 763838b..78212cf 100644 (file)
@@ -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;
index 05c3cd2..00efce0 100644 (file)
@@ -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} ) ) {
index 1783429..1b27ebd 100644 (file)
@@ -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};
         }
     }