1 package Catalyst::Controller::CGIBin;
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';
19 use parent 'Catalyst::Controller::WrapCGI';
23 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
31 our $VERSION = '0.006';
37 package MyApp::Controller::Foo;
39 use parent qw/Catalyst::Controller::CGIBin/;
41 # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
42 sub dongs : Local Args(0) {
44 $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
51 username_field username # used for REMOTE_USER env var
60 Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
62 Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
63 for every invocation. If this is something you need, let me know.
65 CGI paths are converted into action names using cgi_action (below.)
67 A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
68 C<foo/CGI_hlagh_bar_cgi>, for controller Foo, with the C</>s converted to C<_>s
69 and prepended with C<CGI_>, as well as all non-word characters converted to
70 C<_>s. This is because L<Catalyst> action names can't have non-word characters
73 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
74 module for configuration information.
78 sub register_actions {
79 my ($self, $app) = @_;
81 my $cgi_bin = $app->path_to('root', 'cgi-bin');
83 my $namespace = $self->action_namespace($app);
85 my $class = ref $self || $self;
87 for my $file (File::Find::Rule->file->in($cgi_bin)) {
88 my $cgi_path = abs2rel($file, $cgi_bin);
90 next if any { $_ eq '.svn' } splitdir $cgi_path;
92 my $path = join '/' => splitdir($cgi_path);
93 my $action_name = $self->cgi_action($path);
94 my $public_path = $self->cgi_path($path);
95 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
96 my $attrs = { Path => [ $public_path ], Args => [ 0 ] };
100 if ($self->is_perl_cgi($file)) { # syntax check passed
102 $cgi = $self->wrap_perl_cgi($file, $action_name);
105 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
108 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
112 my ($controller, $context) = @_;
113 $controller->cgi_to_response($context, $cgi)
116 my $action = $self->create_action(
117 name => $action_name,
120 namespace => $namespace,
125 $app->dispatcher->register($app, $action);
128 $self->next::method($app, @_);
130 # Tell Static::Simple to ignore the cgi-bin dir.
131 if (!any{ $_ eq 'cgi-bin' } @{ $app->config->{static}{ignore_dirs}||[] }) {
132 push @{ $app->config->{static}{ignore_dirs} }, 'cgi-bin';
138 =head2 $self->cgi_action($cgi)
140 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
141 the action name it is registered as. See L</DESCRIPTION> for a discussion on how
142 CGI actions are named.
147 my ($self, $cgi) = @_;
149 my $action_name = 'CGI_' . join '_' => split '/' => $cgi;
150 $action_name =~ s/\W/_/g;
155 =head2 $self->cgi_path($cgi)
157 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
158 the public path it should be registered under.
160 The default is C<cgi-bin/$cgi>.
165 my ($self, $cgi) = @_;
166 return "cgi-bin/$cgi";
169 =head2 $self->is_perl_cgi($path)
171 Tries to figure out whether the CGI is Perl or not.
173 If it's Perl, it will be inlined into a sub instead of being forked off, see
174 wrap_perl_cgi (below.)
176 If it's not doing what you expect, you might want to override it, and let me
182 my ($self, $cgi) = @_;
184 my $shebang = IO::File->new($cgi)->getline;
186 return 0 if $shebang !~ /perl/ && $cgi !~ /\.pl\z/;
188 my $taint_check = $shebang =~ /-T/ ? '-T' : '';
190 open NULL, '>', File::Spec->devnull;
191 my $pid = open3(gensym, '&>NULL', '&>NULL', "$^X $taint_check -c $cgi");
198 =head2 $self->wrap_perl_cgi($path, $action_name)
200 Takes the path to a Perl CGI and returns a coderef suitable for passing to
201 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
203 C<$action_name> is the generated name for the action representing the CGI file.
205 This is similar to how L<ModPerl::Registry> works, but will only work for
206 well-written CGIs. Otherwise, you may have to override this method to do
207 something more involved (see L<ModPerl::PerlRun>.)
212 my ($self, $cgi, $action_name) = @_;
214 my $code = slurp $cgi;
216 $code =~ s/^__DATA__\r?\n(.*)//ms;
221 # catch exit() and turn it into (effectively) a return
222 # we *must* eval STRING because the code needs to be compiled with the
223 # overridden CORE::GLOBAL::exit in view
225 # set $0 to the name of the cgi file in case it's used there
227 my $cgi_exited = "EXIT\n";
228 BEGIN { *CORE::GLOBAL::exit = sub (;$) {
229 die [ $cgi_exited, $_[0] || 0 ];
231 package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
234 . q{open DATA, '<', \$data;}
235 . qq{local \$0 = "\Q$cgi\E";}
240 return $rv unless $@;
241 die $@ if $@ and not (
242 ref($@) eq 'ARRAY' and
243 $@->[0] eq $cgi_exited
245 die "exited nonzero: $@->[1]" if $@->[1] != 0;
251 croak __PACKAGE__ . ": Could not compile $cgi to coderef: $@" if $@;
256 =head2 $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.
268 sub wrap_nonperl_cgi {
269 my ($self, $cgi, $action_name) = @_;
276 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
277 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
281 Rafael Kitover, C<< <rkitover at cpan.org> >>
285 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
286 rt.cpan.org>, or through the web interface at
287 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
288 I will be notified, and then you'll automatically be notified of progress on
289 your bug as I make changes.
297 =item * RT: CPAN's request tracker
299 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
301 =item * AnnoCPAN: Annotated CPAN documentation
303 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
307 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
311 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
315 =head1 COPYRIGHT & LICENSE
317 Copyright (c) 2008 Rafael Kitover
319 This program is free software; you can redistribute it and/or modify it
320 under the same terms as Perl itself.
324 1; # End of Catalyst::Controller::CGIBin
326 # vim: expandtab shiftwidth=4 ts=4 tw=80: