1 package Catalyst::Controller::CGIBin;
6 extends 'Catalyst::Controller::WrapCGI';
8 use File::Find::Rule ();
9 use Catalyst::Exception ();
10 use File::Spec::Functions qw/splitdir abs2rel/;
13 use List::MoreUtils 'any';
15 use File::Temp 'tempfile';
19 use namespace::clean -except => 'meta';
23 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
31 our $VERSION = '0.027';
37 package MyApp::Controller::Foo;
39 use parent qw/Catalyst::Controller::CGIBin/;
46 cgi_chain_root /optional/private/path/to/Chained/root
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 By default L<Path|Catalyst::DispatchType::Path> actions are created for CGIs,
76 but if you specify this option, the actions will be created as
77 L<Chained|Catalyst::DispatchType::Chained> end-points, chaining off the
78 specified private path.
80 If this option is used, the L</cgi_root_path> option is ignored. The root path
81 will be determined by your chain.
83 The L<PathPart|Catalyst::DispatchType::Chained/PathPart> of the action will be
84 the path to the CGI file.
88 Path from which to read CGI files. Can be relative to C<$MYAPP_HOME/root> or
89 absolute. Defaults to C<$MYAPP_HOME/root/cgi-bin>.
93 has cgi_root_path => (is => 'ro', isa => 'Str', default => 'cgi-bin');
94 has cgi_chain_root => (is => 'ro', isa => 'Str');
95 has cgi_dir => (is => 'ro', isa => 'Str', default => 'cgi-bin');
97 sub register_actions {
98 my ($self, $app) = @_;
100 my $cgi_bin = File::Spec->file_name_is_absolute($self->cgi_dir) ?
102 : $app->path_to('root', $self->cgi_dir);
104 my $namespace = $self->action_namespace($app);
106 my $class = ref $self || $self;
108 for my $file (File::Find::Rule->file->in($cgi_bin)) {
109 my $cgi_path = abs2rel($file, $cgi_bin);
111 next if any { $_ eq '.svn' } splitdir $cgi_path;
112 next if $cgi_path =~ /\.swp\z/;
114 my $path = join '/' => splitdir($cgi_path);
115 my $action_name = $self->cgi_action($path);
116 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
119 if (my $chain_root = $self->cgi_chain_root) {
120 { Chained => [ $chain_root ], PathPart => [ $path ], Args => [] };
123 { Path => [ $self->cgi_path($path) ] };
129 if ($self->is_perl_cgi($file)) { # syntax check passed
131 $cgi = $self->wrap_perl_cgi($file, $action_name);
134 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
137 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
141 my ($controller, $context) = @_;
142 $controller->cgi_to_response($context, $cgi)
145 my $action = $self->create_action(
146 name => $action_name,
149 namespace => $namespace,
154 $app->dispatcher->register($app, $action);
157 $self->next::method($app, @_);
159 # Tell Static::Simple to ignore cgi_dir
160 if ($cgi_bin =~ /^@{[ $app->path_to('root') ]}/) {
161 my $rel = File::Spec->abs2rel($cgi_bin, $app->path_to('root'));
163 if (!any { $_ eq $rel }
164 @{ $app->config->{static}{ignore_dirs}||[] }) {
165 push @{ $app->config->{static}{ignore_dirs} }, $rel;
174 C<< $self->cgi_action($cgi) >>
176 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
177 the action name it is registered as.
182 my ($self, $cgi) = @_;
184 my $action_name = 'CGI_' . $cgi;
185 $action_name =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
192 C<< $self->cgi_path($cgi) >>
194 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
195 the public path it should be registered under.
197 The default is to prefix with C<$cgi_root_path/>, using the C<cgi_root_path>
198 config setting, above.
203 my ($self, $cgi) = @_;
205 my $root = $self->cgi_root_path;
212 C<< $self->is_perl_cgi($path) >>
214 Tries to figure out whether the CGI is Perl or not.
216 If it's Perl, it will be inlined into a sub instead of being forked off, see
222 my ($self, $cgi) = @_;
224 my (undef, $tempfile) = tempfile;
227 die "Cannot fork: $!" unless defined $pid;
231 my $errors = IO::File->new($tempfile)->getline;
233 return $errors ? 0 : 1;
238 open NULL, '>', File::Spec->devnull;
239 open STDOUT, '>&', \*NULL;
240 open STDERR, '>&', \*NULL;
243 eval { $self->wrap_perl_cgi($cgi, '__DUMMY__') };
245 IO::File->new(">$tempfile")->print($@);
252 C<< $self->wrap_perl_cgi($path, $action_name) >>
254 Takes the path to a Perl CGI and returns a coderef suitable for passing to
255 cgi_to_response (from L<Catalyst::Controller::WrapCGI>) using L<CGI::Compile>.
257 C<$action_name> is the generated name for the action representing the CGI file
260 This is similar to how L<ModPerl::Registry> works, but will only work for
261 well-written CGIs. Otherwise, you may have to override this method to do
262 something more involved (see L<ModPerl::PerlRun>.)
264 Scripts with C<__DATA__> sections now work too, as well as scripts that call
270 my ($self, $cgi, $action_name) = @_;
272 return CGI::Compile->compile($cgi,
273 "Catalyst::Controller::CGIBin::_CGIs_::$action_name");
276 =head2 wrap_nonperl_cgi
278 C<< $self->wrap_nonperl_cgi($path, $action_name) >>
280 Takes the path to a non-Perl CGI and returns a coderef for executing it.
282 C<$action_name> is the generated name for the action representing the CGI file.
284 By default returns something like:
290 sub wrap_nonperl_cgi {
291 my ($self, $cgi, $action_name) = @_;
297 die "failed to execute CGI '$cgi': $!";
300 die sprintf "CGI '$cgi' died with signal %d, %s coredump",
301 ($? & 127), ($? & 128) ? 'with' : 'without';
304 my $exit_code = $? >> 8;
306 return 0 if $exit_code == 0;
308 die "CGI '$cgi' exited non-zero with: $exit_code";
313 __PACKAGE__->meta->make_immutable;
317 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
318 L<Catalyst::Controller>, L<CGI>, L<CGI::Compile>, L<Catalyst>
322 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
323 rt.cpan.org>, or through the web interface at
324 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
325 I will be notified, and then you'll automatically be notified of progress on
326 your bug as I make changes.
334 =item * RT: CPAN's request tracker
336 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
338 =item * AnnoCPAN: Annotated CPAN documentation
340 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
344 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
348 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
354 See L<Catalyst::Controller::WrapCGI/AUTHOR> and
355 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
357 =head1 COPYRIGHT & LICENSE
359 Copyright (c) 2008-2009 L<Catalyst::Controller::WrapCGI/AUTHOR> and
360 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
362 This program is free software; you can redistribute it and/or modify it
363 under the same terms as Perl itself.
367 1; # End of Catalyst::Controller::CGIBin
369 # vim: expandtab shiftwidth=4 ts=4 tw=80: