1 package Catalyst::Controller::CGIBin;
6 extends 'Catalyst::Controller::WrapCGI';
8 use File::Slurp 'slurp';
9 use File::Find::Rule ();
10 use Catalyst::Exception ();
11 use File::Spec::Functions qw/splitdir abs2rel/;
14 use List::MoreUtils 'any';
16 use File::Temp 'tempfile';
20 use namespace::clean -except => 'meta';
24 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
32 our $VERSION = '0.023';
38 package MyApp::Controller::Foo;
40 use parent qw/Catalyst::Controller::CGIBin/;
48 username_field username # used for REMOTE_USER env var
57 Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
59 Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
60 for every invocation. This may be supported in the future if there's interest.
62 CGI paths are converted into action names using L</cgi_action>.
64 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
65 module for other configuration information.
71 The global URI path prefix for CGIs, defaults to C<cgi-bin>.
75 Path from which to read CGI files. Can be relative to C<$MYAPP_HOME/root> or
76 absolute. Defaults to C<$MYAPP_HOME/root/cgi-bin>.
80 has cgi_root_path => (is => 'ro', isa => 'Str', default => 'cgi-bin');
81 has cgi_dir => (is => 'ro', isa => 'Str', default => 'cgi-bin');
83 sub register_actions {
84 my ($self, $app) = @_;
86 my $cgi_bin = File::Spec->file_name_is_absolute($self->cgi_dir) ?
88 : $app->path_to('root', $self->cgi_dir);
90 my $namespace = $self->action_namespace($app);
92 my $class = ref $self || $self;
94 for my $file (File::Find::Rule->file->in($cgi_bin)) {
95 my $cgi_path = abs2rel($file, $cgi_bin);
97 next if any { $_ eq '.svn' } splitdir $cgi_path;
98 next if $cgi_path =~ /\.swp\z/;
100 my $path = join '/' => splitdir($cgi_path);
101 my $action_name = $self->cgi_action($path);
102 my $public_path = $self->cgi_path($path);
103 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
104 my $attrs = { Path => [ $public_path ] };
108 if ($self->is_perl_cgi($file)) { # syntax check passed
110 $cgi = $self->wrap_perl_cgi($file, $action_name);
113 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
116 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
120 my ($controller, $context) = @_;
121 $controller->cgi_to_response($context, $cgi)
124 my $action = $self->create_action(
125 name => $action_name,
128 namespace => $namespace,
133 $app->dispatcher->register($app, $action);
136 $self->next::method($app, @_);
138 # Tell Static::Simple to ignore cgi_dir
139 if ($cgi_bin =~ /^@{[ $app->path_to('root') ]}/) {
140 my $rel = File::Spec->abs2rel($cgi_bin, $app->path_to('root'));
142 if (!any { $_ eq $rel }
143 @{ $app->config->{static}{ignore_dirs}||[] }) {
144 push @{ $app->config->{static}{ignore_dirs} }, $rel;
153 C<< $self->cgi_action($cgi) >>
155 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
156 the action name it is registered as.
161 my ($self, $cgi) = @_;
163 my $action_name = 'CGI_' . $cgi;
164 $action_name =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
171 C<< $self->cgi_path($cgi) >>
173 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
174 the public path it should be registered under.
176 The default is to prefix with C<$cgi_root_path/>, using the C<cgi_root_path>
177 config setting, above.
182 my ($self, $cgi) = @_;
184 my $root = $self->cgi_root_path;
191 C<< $self->is_perl_cgi($path) >>
193 Tries to figure out whether the CGI is Perl or not.
195 If it's Perl, it will be inlined into a sub instead of being forked off, see
201 my ($self, $cgi) = @_;
203 my (undef, $tempfile) = tempfile;
206 die "Cannot fork: $!" unless defined $pid;
210 my $errors = IO::File->new($tempfile)->getline;
212 return $errors ? 0 : 1;
217 open NULL, '>', File::Spec->devnull;
218 open STDOUT, '>&', \*NULL;
219 open STDERR, '>&', \*NULL;
222 eval { $self->wrap_perl_cgi($cgi, '__DUMMY__') };
224 IO::File->new(">$tempfile")->print($@);
231 C<< $self->wrap_perl_cgi($path, $action_name) >>
233 Takes the path to a Perl CGI and returns a coderef suitable for passing to
234 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
236 C<$action_name> is the generated name for the action representing the CGI file
239 This is similar to how L<ModPerl::Registry> works, but will only work for
240 well-written CGIs. Otherwise, you may have to override this method to do
241 something more involved (see L<ModPerl::PerlRun>.)
243 Scripts with C<__DATA__> sections now work too, as well as scripts that call
249 my ($self, $cgi, $action_name) = @_;
251 my $code = slurp $cgi;
252 my $dir = File::Basename::dirname($cgi);
254 $code =~ s/^__DATA__\n(.*)//ms;
257 my $orig_exit = \*CORE::GLOBAL::exit;
262 # catch exit() and turn it into (effectively) a return
263 # we *must* eval STRING because the code needs to be compiled with the
264 # overridden CORE::GLOBAL::exit in view
266 # set $0 to the name of the cgi file in case it's used there
268 my $cgi_exited = "EXIT\n";
269 BEGIN { *CORE::GLOBAL::exit = sub (;$) {
270 die [ $cgi_exited, $_[0] || 0 ];
272 package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
274 . 'local *DATA;'."\n"
275 . q{open DATA, '<', \$data;}."\n"
276 . qq{local \$0 = '$cgi';}."\n"
277 . "my \$_dir = File::pushd::pushd '$dir';"."\n"
278 . "CGI::initialize_globals() "."\n"
279 . " if defined &CGI::initialize_globals;"."\n"
280 . q/my $rv = eval {/."\n"
282 . "#line 1 $cgi"."\n"
286 return $rv unless $@;
287 die $@ if $@ and not (
288 ref($@) eq 'ARRAY' and
289 $@->[0] eq $cgi_exited
291 die "exited nonzero: $@->[1]" if $@->[1] != 0;
299 *CORE::GLOBAL::exit = $orig_exit;
302 die "Could not compile $cgi to coderef: $@" if $@;
307 # Once CGI::Compile is updated, we can use this:
310 # my ($self, $cgi, $action_name) = @_;
312 # return CGI::Compile->compile($cgi,
313 # "Catalyst::Controller::CGIBin::_CGIs_::$action_name");
316 =head2 wrap_nonperl_cgi
318 C<< $self->wrap_nonperl_cgi($path, $action_name) >>
320 Takes the path to a non-Perl CGI and returns a coderef for executing it.
322 C<$action_name> is the generated name for the action representing the CGI file.
330 sub wrap_nonperl_cgi {
331 my ($self, $cgi, $action_name) = @_;
336 __PACKAGE__->meta->make_immutable;
340 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
341 L<Catalyst::Controller>, L<CGI>, L<CGI::Compile>, L<Catalyst>
345 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
346 rt.cpan.org>, or through the web interface at
347 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
348 I will be notified, and then you'll automatically be notified of progress on
349 your bug as I make changes.
357 =item * RT: CPAN's request tracker
359 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
361 =item * AnnoCPAN: Annotated CPAN documentation
363 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
367 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
371 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
377 See L<Catalyst::Controller::WrapCGI/AUTHOR> and
378 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
380 =head1 COPYRIGHT & LICENSE
382 Copyright (c) 2008-2009 L<Catalyst::Controller::WrapCGI/AUTHOR> and
383 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
385 This program is free software; you can redistribute it and/or modify it
386 under the same terms as Perl itself.
390 1; # End of Catalyst::Controller::CGIBin
392 # vim: expandtab shiftwidth=4 ts=4 tw=80: