The temp file name now preserves the uploaded file's suffix.
Dave Rolsky [Sat, 5 Jun 2010 17:11:57 +0000 (17:11 +0000)]
Added tests and updated Changes.

Also fixed a typo in PAML.pm

Changes
lib/HTTP/Body/MultiPart.pm
t/08multipart-suffix.t [new file with mode: 0644]
t/data/multipart/014-content.dat [new file with mode: 0644]
t/data/multipart/014-headers.pml [new file with mode: 0644]
t/lib/PAML.pm

diff --git a/Changes b/Changes
index bbce579..0aefd95 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,11 @@
 This file documents the revision history for Perl extension HTTP::Body.
 
+1.08
+        - Temp files now preserve the suffix of the uploaded file. This makes
+          it possible to feed the file directly into a mime-type-determing
+          module that may rely on this suffix as part of its heuristic. (Dave
+          Rolsky)
+
 1.07    2010-01-24 20:40:00
         - Up IO::File dependency.
 
index 1c185ab..16f008e 100644 (file)
@@ -270,7 +270,9 @@ sub handler {
             $part->{filename} = $filename;
 
             if ( $filename ne "" ) {
-                my $fh = File::Temp->new( UNLINK => 0, DIR => $self->tmpdir );
+                my $suffix = $filename =~ /[^.]+(\..+)$/ ? $1 : q{};
+
+                my $fh = File::Temp->new( UNLINK => 0, DIR => $self->tmpdir, SUFFIX => $suffix );
 
                 $part->{fh}       = $fh;
                 $part->{tempname} = $fh->filename;
diff --git a/t/08multipart-suffix.t b/t/08multipart-suffix.t
new file mode 100644 (file)
index 0000000..7374223
--- /dev/null
@@ -0,0 +1,71 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 4;
+use Test::Deep;
+
+use Cwd;
+use HTTP::Body;
+use File::Spec::Functions;
+use IO::File;
+use PAML;
+use File::Temp qw/ tempdir /;
+
+my $path = catdir( getcwd(), 't', 'data', 'multipart' );
+
+{
+    my $uploads = uploads_for('001');
+
+    like(
+        $uploads->{upload2}{tempname}, qr/\.pl$/,
+        'tempname preserves .pl suffix'
+    );
+
+    unlike(
+        $uploads->{upload4}{tempname}, qr/\..+$/,
+        'tempname for upload4 has no suffix'
+    );
+}
+
+{
+    my $uploads = uploads_for('006');
+
+    like(
+        $uploads->{upload2}{tempname}, qr/\.pl$/,
+        'tempname preserves .pl suffix with Windows filename'
+    );
+}
+
+{
+    my $uploads = uploads_for('014');
+
+    like(
+        $uploads->{upload}{tempname}, qr/\.foo\.txt$/,
+        'tempname preserves .foo.txt suffix'
+    );
+}
+
+sub uploads_for {
+    my $number = shift;
+
+    my $headers = PAML::LoadFile( catfile( $path, "$number-headers.pml" ) );
+    my $content = IO::File->new( catfile( $path, "$number-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);
+
+    binmode $content, ':raw';
+
+    while ( $content->read( my $buffer, 1024 ) ) {
+        $body->add($buffer);
+    }
+
+    $body->cleanup(1);
+
+    return $body->upload;
+}
diff --git a/t/data/multipart/014-content.dat b/t/data/multipart/014-content.dat
new file mode 100644 (file)
index 0000000..8db2516
--- /dev/null
@@ -0,0 +1,7 @@
+------------0xKhTmLbOuNdArY\r
+Content-Disposition: form-data; name="upload"; filename="hello.foo.txt"\r
+Content-Type: text/plain\r
+\r
+Some random junk\r
+\r
+------------0xKhTmLbOuNdArY--\r
diff --git a/t/data/multipart/014-headers.pml b/t/data/multipart/014-headers.pml
new file mode 100644 (file)
index 0000000..177c64a
--- /dev/null
@@ -0,0 +1,5 @@
+{
+  "User-Agent" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312",
+  "Content-Length" => 181,
+  "Content-Type" => "multipart/form-data; boundary=----------0xKhTmLbOuNdArY"
+}
index f24ba5d..8c3df3d 100644 (file)
@@ -50,7 +50,7 @@ sub LoadFile ($) {
     my $data = do {
 
         my $io = IO::File->new($path, '<')
-          || corak(qq[Couldn't open path '$path' in read mode: $!]);
+          || croak(qq[Couldn't open path '$path' in read mode: $!]);
 
         $io->binmode
           || croak(qq[Couldn't binmode filehandle: $!]);