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 namespace::clean -except => 'meta';
21 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
29 our $VERSION = '0.007';
35 package MyApp::Controller::Foo;
37 use parent qw/Catalyst::Controller::CGIBin/;
39 # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
40 sub serve_cgi : Local Args(0) {
42 $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
50 username_field username # used for REMOTE_USER env var
59 Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
61 Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
62 for every invocation. If this is something you need, let me know.
64 CGI paths are converted into action names using cgi_action (below.)
66 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
67 module for configuration information.
71 has cgi_root_path => (is => 'ro', isa => 'Str', default => 'cgi-bin');
73 sub register_actions {
74 my ($self, $app) = @_;
76 my $cgi_bin = $app->path_to('root', 'cgi-bin');
78 my $namespace = $self->action_namespace($app);
80 my $class = ref $self || $self;
82 for my $file (File::Find::Rule->file->in($cgi_bin)) {
83 my $cgi_path = abs2rel($file, $cgi_bin);
85 next if any { $_ eq '.svn' } splitdir $cgi_path;
87 my $path = join '/' => splitdir($cgi_path);
88 my $action_name = $self->cgi_action($path);
89 my $public_path = $self->cgi_path($path);
90 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
91 my $attrs = { Path => [ $public_path ], Args => [ 0 ] };
95 if ($self->is_perl_cgi($file)) { # syntax check passed
97 $cgi = $self->wrap_perl_cgi($file, $action_name);
100 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
103 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
107 my ($controller, $context) = @_;
108 $controller->cgi_to_response($context, $cgi)
111 my $action = $self->create_action(
112 name => $action_name,
115 namespace => $namespace,
120 $app->dispatcher->register($app, $action);
123 $self->next::method($app, @_);
125 # Tell Static::Simple to ignore the cgi-bin dir.
126 if (!any{ $_ eq 'cgi-bin' } @{ $app->config->{static}{ignore_dirs}||[] }) {
127 push @{ $app->config->{static}{ignore_dirs} }, 'cgi-bin';
133 =head2 $self->cgi_action($cgi)
135 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
136 the action name it is registered as. See L</DESCRIPTION> for a discussion on how
137 CGI actions are named.
139 A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
140 C<foo/CGI_hlagh__bar_cgi>, for controller Foo, with the C</>s converted to C<__>
141 and prepended with C<CGI_>, as well as all non-word characters converted to
142 C<_>s. This is because L<Catalyst> action names can't have non-word characters
145 This means that C<foo/bar.cgi> and C<foo__bar.cgi> for example will both map to
146 the action C<CGI_foo__bar_cgi> so B<DON'T DO THAT>.
151 my ($self, $cgi) = @_;
153 my $action_name = 'CGI_' . join '__' => split '/' => $cgi;
154 $action_name =~ s/\W/_/g;
159 =head2 $self->cgi_path($cgi)
161 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
162 the public path it should be registered under.
164 The default is to prefix with the C<cgi_root_path> config setting, or if not set
165 uses C<cgi-bin/$cgi>.
170 my ($self, $cgi) = @_;
172 my $root = $self->cgi_root_path;
177 =head2 $self->is_perl_cgi($path)
179 Tries to figure out whether the CGI is Perl or not.
181 If it's Perl, it will be inlined into a sub instead of being forked off, see
182 wrap_perl_cgi (below.)
184 If it's not doing what you expect, you might want to override it, and let me
190 my ($self, $cgi) = @_;
192 my $shebang = IO::File->new($cgi)->getline;
194 return 0 if $shebang !~ /perl/ && $cgi !~ /\.pl\z/;
196 my $taint_check = $shebang =~ /-T/ ? '-T' : '';
198 open NULL, '>', File::Spec->devnull;
199 my $pid = open3(gensym, '&>NULL', '&>NULL', "$^X $taint_check -c $cgi");
206 =head2 $self->wrap_perl_cgi($path, $action_name)
208 Takes the path to a Perl CGI and returns a coderef suitable for passing to
209 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
211 C<$action_name> is the generated name for the action representing the CGI file
214 This is similar to how L<ModPerl::Registry> works, but will only work for
215 well-written CGIs. Otherwise, you may have to override this method to do
216 something more involved (see L<ModPerl::PerlRun>.)
218 Scripts with C<__DATA__> sections now work too.
223 my ($self, $cgi, $action_name) = @_;
225 my $code = slurp $cgi;
227 $code =~ s/^__DATA__(?:\r?\n|\r\n?)(.*)//ms;
232 # catch exit() and turn it into (effectively) a return
233 # we *must* eval STRING because the code needs to be compiled with the
234 # overridden CORE::GLOBAL::exit in view
236 # set $0 to the name of the cgi file in case it's used there
238 my $cgi_exited = "EXIT\n";
239 BEGIN { *CORE::GLOBAL::exit = sub (;$) {
240 die [ $cgi_exited, $_[0] || 0 ];
242 package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
245 . q{open DATA, '<', \$data;}
246 . qq{local \$0 = "\Q$cgi\E";}
251 return $rv unless $@;
252 die $@ if $@ and not (
253 ref($@) eq 'ARRAY' and
254 $@->[0] eq $cgi_exited
256 die "exited nonzero: $@->[1]" if $@->[1] != 0;
262 croak __PACKAGE__ . ": Could not compile $cgi to coderef: $@" if $@;
267 =head2 $self->wrap_nonperl_cgi($path, $action_name)
269 Takes the path to a non-Perl CGI and returns a coderef for executing it.
271 C<$action_name> is the generated name for the action representing the CGI file.
279 sub wrap_nonperl_cgi {
280 my ($self, $cgi, $action_name) = @_;
287 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
288 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
292 Rafael Kitover, C<< <rkitover at cpan.org> >>
296 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
297 rt.cpan.org>, or through the web interface at
298 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
299 I will be notified, and then you'll automatically be notified of progress on
300 your bug as I make changes.
308 =item * RT: CPAN's request tracker
310 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
312 =item * AnnoCPAN: Annotated CPAN documentation
314 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
318 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
322 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
326 =head1 COPYRIGHT & LICENSE
328 Copyright (c) 2008 Rafael Kitover
330 This program is free software; you can redistribute it and/or modify it
331 under the same terms as Perl itself.
335 1; # End of Catalyst::Controller::CGIBin
337 # vim: expandtab shiftwidth=4 ts=4 tw=80: