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/;
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 Path from which to read CGI files. Can be relative to C<$MYAPP_HOME/root> or
75 absolute. Defaults to C<$MYAPP_HOME/root/cgi-bin>.
79 has cgi_root_path => (is => 'ro', isa => 'Str', default => 'cgi-bin');
80 has cgi_dir => (is => 'ro', isa => 'Str', default => 'cgi-bin');
82 sub register_actions {
83 my ($self, $app) = @_;
85 my $cgi_bin = File::Spec->file_name_is_absolute($self->cgi_dir) ?
87 : $app->path_to('root', $self->cgi_dir);
89 my $namespace = $self->action_namespace($app);
91 my $class = ref $self || $self;
93 for my $file (File::Find::Rule->file->in($cgi_bin)) {
94 my $cgi_path = abs2rel($file, $cgi_bin);
96 next if any { $_ eq '.svn' } splitdir $cgi_path;
97 next if $cgi_path =~ /\.swp\z/;
99 my $path = join '/' => splitdir($cgi_path);
100 my $action_name = $self->cgi_action($path);
101 my $public_path = $self->cgi_path($path);
102 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
103 my $attrs = { Path => [ $public_path ] };
107 if ($self->is_perl_cgi($file)) { # syntax check passed
109 $cgi = $self->wrap_perl_cgi($file, $action_name);
112 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
115 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
119 my ($controller, $context) = @_;
120 $controller->cgi_to_response($context, $cgi)
123 my $action = $self->create_action(
124 name => $action_name,
127 namespace => $namespace,
132 $app->dispatcher->register($app, $action);
135 $self->next::method($app, @_);
137 # Tell Static::Simple to ignore cgi_dir
138 if ($cgi_bin =~ /^@{[ $app->path_to('root') ]}/) {
139 my $rel = File::Spec->abs2rel($cgi_bin, $app->path_to('root'));
141 if (!any { $_ eq $rel }
142 @{ $app->config->{static}{ignore_dirs}||[] }) {
143 push @{ $app->config->{static}{ignore_dirs} }, $rel;
152 C<< $self->cgi_action($cgi) >>
154 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
155 the action name it is registered as.
160 my ($self, $cgi) = @_;
162 my $action_name = 'CGI_' . $cgi;
163 $action_name =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
170 C<< $self->cgi_path($cgi) >>
172 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
173 the public path it should be registered under.
175 The default is to prefix with C<$cgi_root_path/>, using the C<cgi_root_path>
176 config setting, above.
181 my ($self, $cgi) = @_;
183 my $root = $self->cgi_root_path;
190 C<< $self->is_perl_cgi($path) >>
192 Tries to figure out whether the CGI is Perl or not.
194 If it's Perl, it will be inlined into a sub instead of being forked off, see
200 my ($self, $cgi) = @_;
202 my (undef, $tempfile) = tempfile;
205 die "Cannot fork: $!" unless defined $pid;
209 my $errors = IO::File->new($tempfile)->getline;
211 return $errors ? 0 : 1;
216 open NULL, '>', File::Spec->devnull;
217 open STDOUT, '>&', \*NULL;
218 open STDERR, '>&', \*NULL;
221 eval { $self->wrap_perl_cgi($cgi, '__DUMMY__') };
223 IO::File->new(">$tempfile")->print($@);
230 C<< $self->wrap_perl_cgi($path, $action_name) >>
232 Takes the path to a Perl CGI and returns a coderef suitable for passing to
233 cgi_to_response (from L<Catalyst::Controller::WrapCGI>) using L<CGI::Compile>.
235 C<$action_name> is the generated name for the action representing the CGI file
238 This is similar to how L<ModPerl::Registry> works, but will only work for
239 well-written CGIs. Otherwise, you may have to override this method to do
240 something more involved (see L<ModPerl::PerlRun>.)
242 Scripts with C<__DATA__> sections now work too, as well as scripts that call
248 my ($self, $cgi, $action_name) = @_;
250 return CGI::Compile->compile($cgi,
251 "Catalyst::Controller::CGIBin::_CGIs_::$action_name");
254 =head2 wrap_nonperl_cgi
256 C<< $self->wrap_nonperl_cgi($path, $action_name) >>
258 Takes the path to a non-Perl CGI and returns a coderef for executing it.
260 C<$action_name> is the generated name for the action representing the CGI file.
262 By default returns something like:
268 sub wrap_nonperl_cgi {
269 my ($self, $cgi, $action_name) = @_;
275 die "failed to execute CGI '$cgi': $!";
278 die sprintf "CGI '$cgi' died with signal %d, %s coredump",
279 ($? & 127), ($? & 128) ? 'with' : 'without';
282 my $exit_code = $? >> 8;
284 return 0 if $exit_code == 0;
286 die "CGI '$cgi' exited non-zero with: $exit_code";
291 __PACKAGE__->meta->make_immutable;
295 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
296 L<Catalyst::Controller>, L<CGI>, L<CGI::Compile>, L<Catalyst>
300 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
301 rt.cpan.org>, or through the web interface at
302 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
303 I will be notified, and then you'll automatically be notified of progress on
304 your bug as I make changes.
312 =item * RT: CPAN's request tracker
314 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
316 =item * AnnoCPAN: Annotated CPAN documentation
318 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
322 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
326 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
332 See L<Catalyst::Controller::WrapCGI/AUTHOR> and
333 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
335 =head1 COPYRIGHT & LICENSE
337 Copyright (c) 2008-2009 L<Catalyst::Controller::WrapCGI/AUTHOR> and
338 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
340 This program is free software; you can redistribute it and/or modify it
341 under the same terms as Perl itself.
345 1; # End of Catalyst::Controller::CGIBin
347 # vim: expandtab shiftwidth=4 ts=4 tw=80: