C::C::CGIBin: add support for __DATA__ sections in CGIs
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
index 88dec28..3ffc19b 100644 (file)
@@ -3,8 +3,8 @@ package Catalyst::Controller::CGIBin;
 use strict;
 use warnings;
 
-use Class::C3;
-use URI::Escape;
+use MRO::Compat;
+use mro 'c3';
 use File::Slurp 'slurp';
 use File::Find::Rule ();
 use Catalyst::Exception ();
@@ -13,6 +13,8 @@ use IPC::Open3;
 use Symbol 'gensym';
 use List::MoreUtils 'any';
 use IO::File ();
+use Carp;
+use namespace::clean -except => 'meta';
 
 use parent 'Catalyst::Controller::WrapCGI';
 
@@ -22,11 +24,11 @@ Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
 
 =head1 VERSION
 
-Version 0.003
+Version 0.006
 
 =cut
 
-our $VERSION = '0.003';
+our $VERSION = '0.006';
 
 =head1 SYNOPSIS
 
@@ -123,6 +125,11 @@ sub register_actions {
     }
 
     $self->next::method($app, @_);
+
+# Tell Static::Simple to ignore the cgi-bin dir.
+    if (!any{ $_ eq 'cgi-bin' } @{ $app->config->{static}{ignore_dirs}||[] }) {
+        push @{ $app->config->{static}{ignore_dirs} }, 'cgi-bin';
+    }
 }
 
 =head1 METHODS
@@ -189,13 +196,25 @@ something more involved (see L<ModPerl::PerlRun>.)
 sub wrap_perl_cgi {
     my ($self, $cgi, $action_name) = @_;
 
-    do {
+    my $code = slurp $cgi;
+
+    $code =~ s/^__DATA__\r?\n(.*)//ms;
+    my $data = $1;
+
+    my $coderef = do {
         no warnings;
-# CGIs import stuff, so putting them into this package breaks Cat 5.8
         eval ' 
             package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
-            sub {' . slurp($cgi) . '}'
-    }
+            sub {'
+                . 'local *DATA;'
+                . q{open DATA, '<', \$data;}
+                . $code
+         . '}';
+    };
+
+    croak __PACKAGE__ . ": Could not compile $cgi to coderef: $@" if $@;
+
+    $coderef
 }
 
 =head2 $self->wrap_nonperl_cgi($path, $action_name)