- Updated POST upload handling patch from miyagawa
Matt S Trout [Sat, 28 Jan 2006 15:53:52 +0000 (15:53 +0000)]
lib/Catalyst/Engine.pm
t/live_engine_request_uploads.t

index 44176bc..963ac5a 100644 (file)
@@ -312,6 +312,13 @@ sub prepare_body {
         while ( my $buffer = $self->read($c) ) {
             $c->prepare_body_chunk($buffer);
         }
+
+        # paranoia against wrong Content-Length header
+        my $remaining = $self->read_length - $self->read_position;
+        if ($remaining > 0) {
+            $self->finalize_read($c);
+            Catalyst::Exception->throw("Wrong Content-Length value: ". $self->read_length);
+        }
     }
 }
 
index 48a215d..bc43716 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 use FindBin;
 use lib "$FindBin::Bin/lib";
 
-use Test::More tests => 52;
+use Test::More tests => 59;
 use Catalyst::Test 'TestApp';
 
 use Catalyst::Request;
@@ -146,3 +146,32 @@ use HTTP::Request::Common;
     is( $response->content_type, 'text/plain', 'Response Content-Type' );
     is( $response->content, ( $request->parts )[0]->content, 'Content' );
 }
+
+{
+    my $request = POST(
+        'http://localhost/dump/request',
+        'Content-Type' => 'multipart/form-data',
+        'Content'      =>
+          [ 'file' => ["$FindBin::Bin/catalyst_130pix.gif"], ]
+    );
+
+    # Sending wrong Content-Length here and see if subequent requests fail
+    $request->header('Content-Length' => $request->header('Content-Length') + 1);
+
+    ok( my $response = request($request), 'Request' );
+    ok( !$response->is_success, 'Response Error' );
+
+    $request = POST(
+        'http://localhost/dump/request',
+        'Content-Type' => 'multipart/form-data',
+        'Content'      =>
+          [ 'file1' => ["$FindBin::Bin/catalyst_130pix.gif"],
+            'file2' => ["$FindBin::Bin/catalyst_130pix.gif"], ]
+    );
+
+    ok( $response = request($request), 'Request' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->content_type, 'text/plain', 'Response Content-Type' );
+    like( $response->content, qr/file1 => bless/, 'Upload with name file1');
+    like( $response->content, qr/file2 => bless/, 'Upload with name file2');
+}