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';
17 use File::Temp 'tempfile';
19 use namespace::clean -except => 'meta';
23 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
31 our $VERSION = '0.021';
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. See L</DESCRIPTION> for a discussion on how
156 CGI actions are named.
158 A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
159 C<foo/CGI_hlagh__bar_cgi>, for controller Foo, with the C</>s converted to C<__>
160 and prepended with C<CGI_>, as well as all non-word characters converted to
161 C<_>s. This is because L<Catalyst> action names can't have non-word characters
164 This means that C<foo/bar.cgi> and C<foo__bar.cgi> for example will both map to
165 the action C<CGI_foo__bar_cgi> so B<DON'T DO THAT>.
170 my ($self, $cgi) = @_;
172 my $action_name = 'CGI_' . join '__' => split '/' => $cgi;
173 $action_name =~ s/\W/_/g;
180 C<< $self->cgi_path($cgi) >>
182 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
183 the public path it should be registered under.
185 The default is to prefix with C<$cgi_root_path/>, using the C<cgi_root_path>
186 config setting, above.
191 my ($self, $cgi) = @_;
193 my $root = $self->cgi_root_path;
200 C<< $self->is_perl_cgi($path) >>
202 Tries to figure out whether the CGI is Perl or not.
204 If it's Perl, it will be inlined into a sub instead of being forked off, see
210 my ($self, $cgi) = @_;
212 my (undef, $tempfile) = tempfile;
215 die "Cannot fork: $!" unless defined $pid;
219 my $errors = IO::File->new($tempfile)->getline;
221 return $errors ? 0 : 1;
226 open NULL, '>', File::Spec->devnull;
227 open STDOUT, '>&', \*NULL;
228 open STDERR, '>&', \*NULL;
232 IO::File->new(">$tempfile")->print($@);
238 C<< $self->wrap_perl_cgi($path, $action_name) >>
240 Takes the path to a Perl CGI and returns a coderef suitable for passing to
241 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
243 C<$action_name> is the generated name for the action representing the CGI file
246 This is similar to how L<ModPerl::Registry> works, but will only work for
247 well-written CGIs. Otherwise, you may have to override this method to do
248 something more involved (see L<ModPerl::PerlRun>.)
250 Scripts with C<__DATA__> sections now work too, as well as scripts that call
256 my ($self, $cgi, $action_name) = @_;
258 my $code = slurp $cgi;
260 $code =~ s/^__DATA__(?:\r?\n|\r\n?)(.*)//ms;
265 # catch exit() and turn it into (effectively) a return
266 # we *must* eval STRING because the code needs to be compiled with the
267 # overridden CORE::GLOBAL::exit in view
269 # set $0 to the name of the cgi file in case it's used there
271 my $cgi_exited = "EXIT\n";
272 BEGIN { *CORE::GLOBAL::exit = sub (;$) {
273 die [ $cgi_exited, $_[0] || 0 ];
275 package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
278 . q{open DATA, '<', \$data;}
279 . qq{local \$0 = "\Q$cgi\E";}
284 return $rv unless $@;
285 die $@ if $@ and not (
286 ref($@) eq 'ARRAY' and
287 $@->[0] eq $cgi_exited
289 die "exited nonzero: $@->[1]" if $@->[1] != 0;
295 croak __PACKAGE__ . ": Could not compile $cgi to coderef: $@" if $@;
300 =head2 wrap_nonperl_cgi
302 C<< $self->wrap_nonperl_cgi($path, $action_name) >>
304 Takes the path to a non-Perl CGI and returns a coderef for executing it.
306 C<$action_name> is the generated name for the action representing the CGI file.
314 sub wrap_nonperl_cgi {
315 my ($self, $cgi, $action_name) = @_;
320 __PACKAGE__->meta->make_immutable;
324 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
325 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
329 Rafael Kitover, C<< <rkitover at cpan.org> >>
333 Hans Dieter Pearcey, C<< <hdp at cpan.org> >>
337 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
338 rt.cpan.org>, or through the web interface at
339 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
340 I will be notified, and then you'll automatically be notified of progress on
341 your bug as I make changes.
349 =item * RT: CPAN's request tracker
351 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
353 =item * AnnoCPAN: Annotated CPAN documentation
355 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
359 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
363 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
367 =head1 COPYRIGHT & LICENSE
369 Copyright (c) 2008 Rafael Kitover
371 This program is free software; you can redistribute it and/or modify it
372 under the same terms as Perl itself.
376 1; # End of Catalyst::Controller::CGIBin
378 # vim: expandtab shiftwidth=4 ts=4 tw=80: