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
27 our $VERSION = '0.029';
33 package MyApp::Controller::Foo;
35 use parent qw/Catalyst::Controller::CGIBin/;
42 cgi_chain_root /optional/private/path/to/Chained/root
43 cgi_file_pattern *.cgi
45 cgi_file_pattern /\.pl\z/
47 username_field username # used for REMOTE_USER env var
56 Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
58 Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
59 for every invocation. This may be supported in the future if there's interest.
61 CGI paths are converted into action names using L</cgi_action>.
63 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
64 module for other configuration information.
70 The global URI path prefix for CGIs, defaults to C<cgi-bin>.
74 By default L<Path|Catalyst::DispatchType::Path> actions are created for CGIs,
75 but if you specify this option, the actions will be created as
76 L<Chained|Catalyst::DispatchType::Chained> end-points, chaining off the
77 specified private path.
79 If this option is used, the L</cgi_root_path> option is ignored. The root path
80 will be determined by your chain.
82 The L<PathPart|Catalyst::DispatchType::Chained/PathPart> of the action will be
83 the path to the CGI file.
87 Path from which to read CGI files. Can be relative to C<$MYAPP_HOME/root> or
88 absolute. Defaults to C<$MYAPP_HOME/root/cgi-bin>.
90 =head2 cgi_file_pattern
92 By default all files in L</cgi_dir> will be loaded as CGIs, however, with this
93 option you can specify either a glob or a regex to match the names of files you
96 Can be an array of globs/regexes as well.
100 has cgi_root_path => (is => 'ro', isa => 'Str', default => 'cgi-bin');
101 has cgi_chain_root => (is => 'ro', isa => 'Str');
102 has cgi_dir => (is => 'ro', isa => 'Str', default => 'cgi-bin');
103 has cgi_file_pattern => (is => 'rw', default => sub { ['*'] });
105 sub register_actions {
106 my ($self, $app) = @_;
108 my $cgi_bin = File::Spec->file_name_is_absolute($self->cgi_dir) ?
110 : $app->path_to('root', $self->cgi_dir);
112 my $namespace = $self->action_namespace($app);
114 my $class = ref $self || $self;
116 my $patterns = $self->cgi_file_pattern;
117 $patterns = [ $patterns ] if not ref $patterns;
118 for my $pat (@$patterns) {
119 if ($pat =~ m{^/(.*)/\z}) {
123 $self->cgi_file_pattern($patterns);
125 for my $file (File::Find::Rule->file->name(@$patterns)->in($cgi_bin)) {
126 my $cgi_path = abs2rel($file, $cgi_bin);
128 next if any { $_ eq '.svn' } splitdir $cgi_path;
129 next if $cgi_path =~ /\.swp\z/;
131 my $path = join '/' => splitdir($cgi_path);
132 my $action_name = $self->cgi_action($path);
133 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
136 if (my $chain_root = $self->cgi_chain_root) {
137 { Chained => [ $chain_root ], PathPart => [ $path ], Args => [] };
140 { Path => [ $self->cgi_path($path) ] };
146 if ($self->is_perl_cgi($file)) { # syntax check passed
148 $cgi = $self->wrap_perl_cgi($file, $action_name);
151 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
154 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
158 my ($controller, $context) = @_;
159 $controller->cgi_to_response($context, $cgi)
162 my $action = $self->create_action(
163 name => $action_name,
166 namespace => $namespace,
171 $app->dispatcher->register($app, $action);
174 $self->next::method($app, @_);
176 # Tell Static::Simple to ignore cgi_dir
177 if ($cgi_bin =~ /^@{[ $app->path_to('root') ]}/) {
178 my $rel = File::Spec->abs2rel($cgi_bin, $app->path_to('root'));
180 if (!any { $_ eq $rel }
181 @{ $app->config->{static}{ignore_dirs}||[] }) {
182 push @{ $app->config->{static}{ignore_dirs} }, $rel;
191 C<< $self->cgi_action($cgi) >>
193 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
194 the action name it is registered as.
199 my ($self, $cgi) = @_;
201 my $action_name = 'CGI_' . $cgi;
202 $action_name =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
209 C<< $self->cgi_path($cgi) >>
211 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
212 the public path it should be registered under.
214 The default is to prefix with C<$cgi_root_path/>, using the C<cgi_root_path>
215 config setting, above.
220 my ($self, $cgi) = @_;
222 my $root = $self->cgi_root_path;
229 C<< $self->is_perl_cgi($path) >>
231 Tries to figure out whether the CGI is Perl or not.
233 If it's Perl, it will be inlined into a sub instead of being forked off, see
239 my ($self, $cgi) = @_;
241 my (undef, $tempfile) = tempfile;
244 die "Cannot fork: $!" unless defined $pid;
248 my $errors = IO::File->new($tempfile)->getline;
250 return $errors ? 0 : 1;
255 open NULL, '>', File::Spec->devnull;
256 open STDOUT, '>&', \*NULL;
257 open STDERR, '>&', \*NULL;
260 eval { $self->wrap_perl_cgi($cgi, '__DUMMY__') };
262 IO::File->new(">$tempfile")->print($@);
269 C<< $self->wrap_perl_cgi($path, $action_name) >>
271 Takes the path to a Perl CGI and returns a coderef suitable for passing to
272 cgi_to_response (from L<Catalyst::Controller::WrapCGI>) using L<CGI::Compile>.
274 C<$action_name> is the generated name for the action representing the CGI file
277 This is similar to how L<ModPerl::Registry> works, but will only work for
278 well-written CGIs. Otherwise, you may have to override this method to do
279 something more involved (see L<ModPerl::PerlRun>.)
281 Scripts with C<__DATA__> sections now work too, as well as scripts that call
287 my ($self, $cgi, $action_name) = @_;
289 return CGI::Compile->compile($cgi,
290 "Catalyst::Controller::CGIBin::_CGIs_::$action_name");
293 =head2 wrap_nonperl_cgi
295 C<< $self->wrap_nonperl_cgi($path, $action_name) >>
297 Takes the path to a non-Perl CGI and returns a coderef for executing it.
299 C<$action_name> is the generated name for the action representing the CGI file.
301 By default returns something like:
307 sub wrap_nonperl_cgi {
308 my ($self, $cgi, $action_name) = @_;
314 die "failed to execute CGI '$cgi': $!";
317 die sprintf "CGI '$cgi' died with signal %d, %s coredump",
318 ($? & 127), ($? & 128) ? 'with' : 'without';
321 my $exit_code = $? >> 8;
323 return 0 if $exit_code == 0;
325 die "CGI '$cgi' exited non-zero with: $exit_code";
330 __PACKAGE__->meta->make_immutable;
334 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
335 L<Catalyst::Controller>, L<CGI>, L<CGI::Compile>, L<Catalyst>
339 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
340 rt.cpan.org>, or through the web interface at
341 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
342 I will be notified, and then you'll automatically be notified of progress on
343 your bug as I make changes.
351 =item * RT: CPAN's request tracker
353 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
355 =item * AnnoCPAN: Annotated CPAN documentation
357 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
361 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
365 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
371 See L<Catalyst::Controller::WrapCGI/AUTHOR> and
372 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
374 =head1 COPYRIGHT & LICENSE
376 Copyright (c) 2008-2009 L<Catalyst::Controller::WrapCGI/AUTHOR> and
377 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
379 This program is free software; you can redistribute it and/or modify it
380 under the same terms as Perl itself.
384 1; # End of Catalyst::Controller::CGIBin
385 # vim:et sw=4 sts=4 tw=0: