extends 'Catalyst::Controller::WrapCGI';
-use File::Slurp 'slurp';
use File::Find::Rule ();
use Catalyst::Exception ();
use File::Spec::Functions qw/splitdir abs2rel/;
use IO::File ();
use File::Temp 'tempfile';
use File::pushd;
-#use CGI::Compile;
+use CGI::Compile;
use namespace::clean -except => 'meta';
=head1 VERSION
-Version 0.024
+Version 0.027
=cut
-our $VERSION = '0.024';
+our $VERSION = '0.027';
=head1 SYNOPSIS
C<< $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>.)
+cgi_to_response (from L<Catalyst::Controller::WrapCGI>) using L<CGI::Compile>.
C<$action_name> is the generated name for the action representing the CGI file
from C<cgi_action>.
sub wrap_perl_cgi {
my ($self, $cgi, $action_name) = @_;
- my $code = slurp $cgi;
- my $dir = File::Basename::dirname($cgi);
-
- $code =~ s/^__DATA__\n(.*)//ms;
- my $data = $1;
-
- my $orig_exit = \*CORE::GLOBAL::exit;
- my %orig_sig = %SIG;
-
- my $coderef = do {
- no warnings;
- # catch exit() and turn it into (effectively) a return
- # we *must* eval STRING because the code needs to be compiled with the
- # overridden CORE::GLOBAL::exit in view
- #
- # set $0 to the name of the cgi file in case it's used there
- my $source = '
- my $cgi_exited = "EXIT\n";
- BEGIN { *CORE::GLOBAL::exit = sub (;$) {
- die [ $cgi_exited, $_[0] || 0 ];
- } }
- package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
- sub {'."\n"
- . 'local *DATA;'."\n"
- . q{open DATA, '<', \$data;}."\n"
- . qq{local \$0 = '$cgi';}."\n"
- . "my \$_dir = File::pushd::pushd '$dir';"."\n"
- . "CGI::initialize_globals() "."\n"
- . " if defined &CGI::initialize_globals;"."\n"
- . q/my $rv = eval {/."\n"
- . 'local %SIG;'."\n"
- . "#line 1 $cgi"."\n"
- . $code."\n"
- . q/};/
- . q{
- return $rv unless $@;
- die $@ if $@ and not (
- ref($@) eq 'ARRAY' and
- $@->[0] eq $cgi_exited
- );
- die "exited nonzero: $@->[1]" if $@->[1] != 0;
- return $rv;
- }
- . '}';
- eval $source;
- };
-
- # clean up
- *CORE::GLOBAL::exit = $orig_exit;
- %SIG = %orig_sig;
-
- die "Could not compile $cgi to coderef: $@" if $@;
-
- return $coderef;
+ return CGI::Compile->compile($cgi,
+ "Catalyst::Controller::CGIBin::_CGIs_::$action_name");
}
-# Once CGI::Compile is updated, we can use this:
-
-#sub wrap_perl_cgi {
-# my ($self, $cgi, $action_name) = @_;
-#
-# return CGI::Compile->compile($cgi,
-# "Catalyst::Controller::CGIBin::_CGIs_::$action_name");
-#}
-
=head2 wrap_nonperl_cgi
C<< $self->wrap_nonperl_cgi($path, $action_name) >>
C<$action_name> is the generated name for the action representing the CGI file.
-By default returns:
+By default returns something like:
sub { system $path }
sub wrap_nonperl_cgi {
my ($self, $cgi, $action_name) = @_;
- sub { system $cgi }
+ return sub {
+ system $cgi;
+
+ if ($? == -1) {
+ die "failed to execute CGI '$cgi': $!";
+ }
+ elsif ($? & 127) {
+ die sprintf "CGI '$cgi' died with signal %d, %s coredump",
+ ($? & 127), ($? & 128) ? 'with' : 'without';
+ }
+ else {
+ my $exit_code = $? >> 8;
+
+ return 0 if $exit_code == 0;
+
+ die "CGI '$cgi' exited non-zero with: $exit_code";
+ }
+ };
}
__PACKAGE__->meta->make_immutable;