C::C::CGIBin: add root/cgi-bin to $c->config->{static}{ignore_dirs}
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
index db21f50..c911905 100644 (file)
@@ -4,16 +4,15 @@ use strict;
 use warnings;
 
 use Class::C3;
-use URI::Escape;
 use File::Slurp 'slurp';
 use File::Find::Rule ();
-use Cwd;
 use Catalyst::Exception ();
 use File::Spec::Functions qw/splitdir abs2rel/;
 use IPC::Open3;
 use Symbol 'gensym';
 use List::MoreUtils 'any';
 use IO::File ();
+use namespace::clean -except => 'meta';
 
 use parent 'Catalyst::Controller::WrapCGI';
 
@@ -23,11 +22,11 @@ Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
 
 =head1 VERSION
 
-Version 0.001
+Version 0.004
 
 =cut
 
-our $VERSION = '0.001';
+our $VERSION = '0.004';
 
 =head1 SYNOPSIS
 
@@ -88,24 +87,24 @@ sub register_actions {
 
         next if any { $_ eq '.svn' } splitdir $cgi_path;
 
+        my $path        = join '/' => splitdir($cgi_path);
+        my $action_name = $self->cgi_action($path);
+        my $reverse     = $namespace ? "$namespace/$action_name" : $action_name;
+        my $attrs       = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
+
         my ($cgi, $type);
 
         if ($self->is_perl_cgi($file)) { # syntax check passed
             $type = 'Perl';
-            $cgi  = $self->wrap_perl_cgi($file);
+            $cgi  = $self->wrap_perl_cgi($file, $action_name);
         } else {
             $type = 'Non-Perl';
-            $cgi  = $self->wrap_nonperl_cgi($file);
+            $cgi  = $self->wrap_nonperl_cgi($file, $action_name);
         }
 
         $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
             if $app->debug;
 
-        my $path        = join '/' => splitdir($cgi_path);
-        my $action_name = $self->cgi_action($path);
-        my $reverse     = $namespace ? "$namespace/$action_name" : $action_name;
-        my $attrs       = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
-
         my $code = sub {
             my ($controller, $context) = @_;
             $controller->cgi_to_response($context, $cgi)
@@ -124,6 +123,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
@@ -162,7 +166,7 @@ sub is_perl_cgi {
 
     my $shebang = IO::File->new($cgi)->getline;
 
-    return 0 if $shebang !~ /perl/ || $cgi !~ /\.pl\z/;
+    return 0 if $shebang !~ /perl/ && $cgi !~ /\.pl\z/;
 
     my $taint_check = $shebang =~ /-T/ ?  '-T' : '';
 
@@ -174,14 +178,12 @@ sub is_perl_cgi {
     $? >> 8 == 0
 }
 
-=head2 $self->wrap_perl_cgi($path)
+=head2 $self->wrap_perl_cgi($path, $action_name)
 
 Takes the path to a Perl CGI and returns a coderef suitable for passing to
 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
 
-By default returns:
-
-    eval 'sub {' . slurp($path) . '}'
+C<$action_name> is the generated name for the action representing the CGI file.
 
 This is similar to how L<ModPerl::Registry> works, but will only work for
 well-written CGIs. Otherwise, you may have to override this method to do
@@ -190,15 +192,23 @@ something more involved (see L<ModPerl::PerlRun>.)
 =cut
 
 sub wrap_perl_cgi {
-    my ($self, $cgi) = @_;
-
-    do { no warnings; eval 'sub {' . slurp($cgi) . '}' }
+    my ($self, $cgi, $action_name) = @_;
+
+    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) . '}'
+    }
 }
 
-=head2 $self->wrap_nonperl_cgi($path)
+=head2 $self->wrap_nonperl_cgi($path, $action_name)
 
 Takes the path to a non-Perl CGI and returns a coderef for executing it.
 
+C<$action_name> is the generated name for the action representing the CGI file.
+
 By default returns:
 
     sub { system $path }
@@ -206,7 +216,7 @@ By default returns:
 =cut
 
 sub wrap_nonperl_cgi {
-    my ($self, $cgi) = @_;
+    my ($self, $cgi, $action_name) = @_;
 
     sub { system $cgi }
 }