WrapCGI - file uploads support
Rafael Kitover [Wed, 29 Apr 2009 02:57:29 +0000 (02:57 +0000)]
Changes
lib/Catalyst/Controller/CGIBin.pm
lib/Catalyst/Controller/WrapCGI.pm
t/lib/TestApp/Controller/Root.pm
t/wrap-cgi.t

diff --git a/Changes b/Changes
index 9d2c50d..765aa55 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for Catalyst-Controller-WrapCGI
 
+    - file uploads support (caelum)
+    - test for file uploads (hdp)
+
 0.0032  2009-04-27 18:43:25
     - fix deps, does not work in cat 5.7 (caelum)
 
index 0d6ff40..d38397b 100644 (file)
@@ -14,7 +14,7 @@ use Symbol 'gensym';
 use List::MoreUtils 'any';
 use IO::File ();
 use Carp;
-
 use namespace::clean -except => 'meta';
 
 =head1 NAME
@@ -23,11 +23,11 @@ Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
 
 =head1 VERSION
 
-Version 0.011
+Version 0.012
 
 =cut
 
-our $VERSION = '0.011';
+our $VERSION = '0.012';
 
 =head1 SYNOPSIS
 
index 5055a9c..2f757de 100644 (file)
@@ -10,6 +10,7 @@ use HTTP::Request ();
 use URI ();
 use Catalyst::Exception ();
 use URI::Escape;
+use HTTP::Request::Common;
 
 use namespace::clean -except => 'meta';
 
@@ -19,11 +20,11 @@ Catalyst::Controller::WrapCGI - Run CGIs in Catalyst
 
 =head1 VERSION
 
-Version 0.0032
+Version 0.0033
 
 =cut
 
-our $VERSION = '0.0032';
+our $VERSION = '0.0033';
 
 =head1 SYNOPSIS
 
@@ -156,7 +157,29 @@ sub wrap_cgi {
     local $/; $body_content = <$body>;
   } else {
     my $body_params = $c->req->body_parameters;
-    if (%$body_params) {
+
+    if (my %uploads = %{ $c->req->uploads }) {
+      my $post = POST 'http://localhost/',
+        Content_Type => 'form-data',
+        Content => [
+          %$body_params,
+          map {
+            my $upl = $uploads{$_};
+            $_ => [
+              undef,
+              $upl->filename,
+              Content => $upl->slurp,
+              'Content-Type' => $upl->type || 'application/octet-stream',
+              map (
+                $_ => $upl->headers->header($_)
+              ), grep !/^Content-(?:Type|Disposition)$/,
+                            $upl->headers->header_field_names
+            ]
+          } keys %uploads
+        ];
+      $body_content = $post->content;
+      $req->content_type($post->header('Content-Type'));
+    } elsif (%$body_params) {
       my $encoder = URI->new;
       $encoder->query_form(%$body_params);
       $body_content = $encoder->query;
index a0557a7..7d82a37 100644 (file)
@@ -10,8 +10,10 @@ my $cgi = sub {
     print $cgi->header;
     print 'foo:',$cgi->param('foo'),' bar:',$cgi->param('bar');
     if (my $fh = $cgi->param('baz')) {
-      print 'baz:',<$fh>;
+      local $/;
+      print ' baz:',<$fh>;
     }
+    die $cgi->cgi_error if $cgi->cgi_error;
 };
 
 sub handle_cgi : Path('/cgi-bin/test.cgi') {
index 7c29988..098ff5e 100644 (file)
@@ -27,14 +27,19 @@ is($response->content, 'foo:bar bar:baz', 'POST to CGI (form-data)');
 
 $response = request POST '/cgi-bin/test.cgi',
   Content => [
-    foo => 1, bar => 2, baz => [ undef, 'baz', Content => 3 ],
+    foo => 1,
+    bar => 2,
+    baz => [
+        undef,
+        'baz',
+        'Some-Header' => 'blah',
+        'Content-Type' => 'text/plain',
+        Content => 3
+    ],
   ],
   'Content-Type' => 'form-data';
 
-{
-  local $TODO = 'WrapCGI does not yet construct multipart/form-data requests';
-  is($response->content, 'foo:1 bar:2 baz:3', 'POST with file upload');
-}
+is($response->content, 'foo:1 bar:2 baz:3', 'POST with file upload');
 
 $response = request '/cgi-bin/test_pathinfo.cgi/path/%2Finfo';
 is($response->content, '/path/%2Finfo', 'PATH_INFO is correct');