allow overriding of public cgi-bin paths
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
index c911905..5859ad8 100644 (file)
@@ -3,7 +3,8 @@ package Catalyst::Controller::CGIBin;
 use strict;
 use warnings;
 
-use Class::C3;
+use MRO::Compat;
+use mro 'c3';
 use File::Slurp 'slurp';
 use File::Find::Rule ();
 use Catalyst::Exception ();
@@ -12,6 +13,7 @@ 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.004
+Version 0.006
 
 =cut
 
-our $VERSION = '0.004';
+our $VERSION = '0.006';
 
 =head1 SYNOPSIS
 
@@ -89,8 +91,9 @@ sub register_actions {
 
         my $path        = join '/' => splitdir($cgi_path);
         my $action_name = $self->cgi_action($path);
+        my $public_path = $self->cgi_path($path);
         my $reverse     = $namespace ? "$namespace/$action_name" : $action_name;
-        my $attrs       = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
+        my $attrs       = { Path => [ $public_path ], Args => [ 0 ] };
 
         my ($cgi, $type);
 
@@ -132,7 +135,7 @@ sub register_actions {
 
 =head1 METHODS
 
-=head2 $self->cgi_action($cgi_path)
+=head2 $self->cgi_action($cgi)
 
 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
 the action name it is registered as. See L</DESCRIPTION> for a discussion on how
@@ -149,6 +152,20 @@ sub cgi_action {
     $action_name
 }
 
+=head2 $self->cgi_path($cgi)
+
+Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
+the public path it should be registered under.
+
+The default is C<cgi-bin/$cgi>.
+
+=cut
+
+sub cgi_path {
+    my ($self, $cgi) = @_;
+    return "cgi-bin/$cgi";
+}
+
 =head2 $self->is_perl_cgi($path)
 
 Tries to figure out whether the CGI is Perl or not.
@@ -194,13 +211,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)