fix typo in synoposis (RT#100492)
[catagits/HTTP-Body.git] / lib / HTTP / Body / MultiPart.pm
index 5ff3970..c9273d4 100644 (file)
@@ -6,6 +6,7 @@ use bytes;
 
 use IO::File;
 use File::Temp 0.14;
+use File::Spec;
 
 =head1 NAME
 
@@ -13,7 +14,7 @@ HTTP::Body::MultiPart - HTTP Body Multipart Parser
 
 =head1 SYNOPSIS
 
-    use HTTP::Body::Multipart;
+    use HTTP::Body::MultiPart;
 
 =head1 DESCRIPTION
 
@@ -30,7 +31,7 @@ HTTP Body Multipart Parser.
 sub init {
     my $self = shift;
 
-    unless ( $self->content_type =~ /boundary=\"?([^\";,]+)\"?/ ) {
+    unless ( $self->content_type =~ /boundary=\"?([^\";]+)\"?/ ) {
         my $content_type = $self->content_type;
         Carp::croak("Invalid boundary in content_type: '$content_type'");
     }
@@ -254,6 +255,9 @@ sub parse_body {
 
 =cut
 
+our $basename_regexp = qr/[^.]+(\.[^\\\/]+)$/;
+#our $basename_regexp = qr/(\.\w+(?:\.\w+)*)$/;
+
 sub handler {
     my ( $self, $part ) = @_;
 
@@ -270,7 +274,10 @@ sub handler {
             $part->{filename} = $filename;
 
             if ( $filename ne "" ) {
-                my $fh = File::Temp->new( UNLINK => 0 );
+                my $basename = (File::Spec->splitpath($filename))[2];
+                my $suffix = $basename =~ $basename_regexp ? $1 : q{};
+
+                my $fh = File::Temp->new( UNLINK => 0, DIR => $self->tmpdir, SUFFIX => $suffix );
 
                 $part->{fh}       = $fh;
                 $part->{tempname} = $fh->filename;
@@ -286,15 +293,18 @@ sub handler {
 
         if ( exists $part->{filename} ) {
             if ( $part->{filename} ne "" ) {
-                $part->{fh}->close;
+                $part->{fh}->close if defined $part->{fh};
 
                 delete @{$part}{qw[ data done fh ]};
 
                 $self->upload( $part->{name}, $part );
             }
         }
+        # If we have more than the content-disposition, we need to create a
+        # data key so that we don't waste the headers.
         else {
             $self->param( $part->{name}, $part->{data} );
+            $self->part_data( $part->{name}, $part )
         }
     }
 }