[Catalyst-commits] r10049 - in trunk/HTTP-Body: . lib/HTTP t
andyg at dev.catalyst.perl.org
andyg at dev.catalyst.perl.org
Thu May 7 16:35:26 GMT 2009
Author: andyg
Date: 2009-05-07 16:35:25 +0000 (Thu, 07 May 2009)
New Revision: 10049
Modified:
trunk/HTTP-Body/Changes
trunk/HTTP-Body/Makefile.PL
trunk/HTTP-Body/lib/HTTP/Body.pm
trunk/HTTP-Body/t/04multipart.t
Log:
Added cleanup flag to auto-delete temp files
Modified: trunk/HTTP-Body/Changes
===================================================================
--- trunk/HTTP-Body/Changes 2009-05-07 15:41:55 UTC (rev 10048)
+++ trunk/HTTP-Body/Changes 2009-05-07 16:35:25 UTC (rev 10049)
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension HTTP::Body.
1.06
+ - Added $body->cleanup(1) flag to enable auto-deletion of temporary files
+ during DESTROY. (Vany Serezhkin)
- Fixed parsing of multipart bodies with boundaries that contain commas.
(Tomas Doran, http://rt.cpan.org/Public/Bug/Display.html?id=41407)
Modified: trunk/HTTP-Body/Makefile.PL
===================================================================
--- trunk/HTTP-Body/Makefile.PL 2009-05-07 15:41:55 UTC (rev 10048)
+++ trunk/HTTP-Body/Makefile.PL 2009-05-07 16:35:25 UTC (rev 10049)
@@ -10,6 +10,7 @@
File::Temp => '0.14',
HTTP::Headers => 0,
IO::File => 0,
+ Test::Deep => 0,
YAML => '0.39'
}
);
Modified: trunk/HTTP-Body/lib/HTTP/Body.pm
===================================================================
--- trunk/HTTP-Body/lib/HTTP/Body.pm 2009-05-07 15:41:55 UTC (rev 10048)
+++ trunk/HTTP-Body/lib/HTTP/Body.pm 2009-05-07 16:35:25 UTC (rev 10049)
@@ -68,7 +68,8 @@
When parsing multipart bodies, temporary files are created to store any
uploaded files. You must delete these temporary files yourself after
-processing them.
+processing them, or set $body->cleanup(1) to automatically delete them
+at DESTROY-time.
=head1 METHODS
@@ -98,6 +99,7 @@
my $body = $TYPES->{ $type || 'application/octet-stream' };
my $self = {
+ cleanup => 0,
buffer => '',
chunk_buffer => '',
body => undef,
@@ -116,6 +118,20 @@
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
@@ -220,6 +236,18 @@
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.
Modified: trunk/HTTP-Body/t/04multipart.t
===================================================================
--- trunk/HTTP-Body/t/04multipart.t 2009-05-07 15:41:55 UTC (rev 10048)
+++ trunk/HTTP-Body/t/04multipart.t 2009-05-07 16:35:25 UTC (rev 10049)
@@ -3,7 +3,8 @@
use strict;
use warnings;
-use Test::More tests => 102;
+use Test::More tests => 140;
+use Test::Deep;
use Cwd;
use HTTP::Body;
@@ -32,6 +33,11 @@
$body->add($buffer);
}
+ # Tests >= 10 use auto-cleanup
+ if ( $i >= 10 ) {
+ $body->cleanup(1);
+ }
+
# Save tempnames for later deletion
my @temps;
@@ -41,17 +47,36 @@
for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) {
like($_->{tempname}, qr{$regex_tempdir}, "has tmpdir $tempdir");
- push @temps, delete $_->{tempname};
+ push @temps, $_->{tempname};
}
+
+ # Tell Test::Deep to ignore tempname values
+ if ( ref $value eq 'ARRAY' ) {
+ for ( @{ $results->{upload}->{$field} } ) {
+ $_->{tempname} = ignore();
+ }
+ }
+ else {
+ $results->{upload}->{$field}->{tempname} = ignore();
+ }
}
- is_deeply( $body->body, $results->{body}, "$test MultiPart body" );
- is_deeply( $body->param, $results->{param}, "$test MultiPart param" );
- is_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" )
+ cmp_deeply( $body->body, $results->{body}, "$test MultiPart body" );
+ cmp_deeply( $body->param, $results->{param}, "$test MultiPart param" );
+ cmp_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" )
if $results->{upload};
cmp_ok( $body->state, 'eq', 'done', "$test MultiPart state" );
cmp_ok( $body->length, '==', $body->content_length, "$test MultiPart length" );
- # Clean up temp files created
- unlink map { $_ } grep { -e $_ } @temps;
-}
+ if ( $i < 10 ) {
+ # Clean up temp files created
+ unlink map { $_ } grep { -e $_ } @temps;
+ }
+
+ undef $body;
+
+ # Ensure temp files were deleted
+ for my $temp ( @temps ) {
+ ok( !-e $temp, "Temp file $temp was deleted" );
+ }
+}
More information about the Catalyst-commits
mailing list