1 package Catalyst::Controller::CGIBin;
8 use File::Slurp 'slurp';
9 use File::Find::Rule ();
11 use Catalyst::Exception ();
12 use File::Spec::Functions qw/splitdir abs2rel/;
15 use List::MoreUtils 'any';
18 use parent 'Catalyst::Controller::WrapCGI';
22 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
30 our $VERSION = '0.001';
36 package MyApp::Controller::Foo;
38 use parent qw/Catalyst::Controller::CGIBin/;
40 # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
41 sub dongs : Local Args(0) {
43 $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 A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
67 C<foo/CGI_hlagh_bar_cgi>, for controller Foo, with the C</>s converted to C<_>s
68 and prepended with C<CGI_>, as well as all non-word characters converted to
69 C<_>s. This is because L<Catalyst> action names can't have non-word characters
72 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
73 module for configuration information.
77 sub register_actions {
78 my ($self, $app) = @_;
80 my $cgi_bin = $app->path_to('root', 'cgi-bin');
82 my $namespace = $self->action_namespace($app);
84 my $class = ref $self || $self;
86 for my $file (File::Find::Rule->file->in($cgi_bin)) {
87 my $cgi_path = abs2rel($file, $cgi_bin);
89 next if any { $_ eq '.svn' } splitdir $cgi_path;
93 if ($self->is_perl_cgi($file)) { # syntax check passed
95 $cgi = $self->wrap_perl_cgi($file);
98 $cgi = $self->wrap_nonperl_cgi($file);
101 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
104 my $path = join '/' => splitdir($cgi_path);
105 my $action_name = $self->cgi_action($path);
106 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
107 my $attrs = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
110 my ($controller, $context) = @_;
111 $controller->cgi_to_response($context, $cgi)
114 my $action = $self->create_action(
115 name => $action_name,
118 namespace => $namespace,
123 $app->dispatcher->register($app, $action);
126 $self->next::method($app, @_);
131 =head2 $self->cgi_action($cgi_path)
133 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
134 the action name it is registered as. See L</DESCRIPTION> for a discussion on how
135 CGI actions are named.
140 my ($self, $cgi) = @_;
142 my $action_name = 'CGI_' . join '_' => split '/' => $cgi;
143 $action_name =~ s/\W/_/g;
148 =head2 $self->is_perl_cgi($path)
150 Tries to figure out whether the CGI is Perl or not.
152 If it's Perl, it will be inlined into a sub instead of being forked off, see
153 wrap_perl_cgi (below.)
155 If it's not doing what you expect, you might want to override it, and let me
161 my ($self, $cgi) = @_;
163 my $shebang = IO::File->new($cgi)->getline;
165 return 0 if $shebang !~ /perl/ || $cgi !~ /\.pl\z/;
167 my $taint_check = $shebang =~ /-T/ ? '-T' : '';
169 open NULL, '>', File::Spec->devnull;
170 my $pid = open3(gensym, '&>NULL', '&>NULL', "$^X $taint_check -c $cgi");
177 =head2 $self->wrap_perl_cgi($path)
179 Takes the path to a Perl CGI and returns a coderef suitable for passing to
180 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
184 eval 'sub {' . slurp($path) . '}'
186 This is similar to how L<ModPerl::Registry> works, but will only work for
187 well-written CGIs. Otherwise, you may have to override this method to do
188 something more involved (see L<ModPerl::PerlRun>.)
193 my ($self, $cgi) = @_;
195 do { no warnings; eval 'sub {' . slurp($cgi) . '}' }
198 =head2 $self->wrap_nonperl_cgi($path)
200 Takes the path to a non-Perl CGI and returns a coderef for executing it.
208 sub wrap_nonperl_cgi {
209 my ($self, $cgi) = @_;
216 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
217 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
221 Rafael Kitover, C<< <rkitover at cpan.org> >>
225 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
226 rt.cpan.org>, or through the web interface at
227 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
228 I will be notified, and then you'll automatically be notified of progress on
229 your bug as I make changes.
237 =item * RT: CPAN's request tracker
239 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
241 =item * AnnoCPAN: Annotated CPAN documentation
243 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
247 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
251 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
255 =head1 COPYRIGHT & LICENSE
257 Copyright (c) 2008 Rafael Kitover
259 This program is free software; you can redistribute it and/or modify it
260 under the same terms as Perl itself.
264 1; # End of Catalyst::Controller::CGIBin
266 # vim: expandtab shiftwidth=4 ts=4 tw=80: