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 parent 'Catalyst::Controller::WrapCGI';
21 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
29 our $VERSION = '0.001';
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 dongs : Local Args(0) {
42 $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
49 username_field username # used for REMOTE_USER env var
58 Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
60 Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
61 for every invocation. If this is something you need, let me know.
63 CGI paths are converted into action names using cgi_action (below.)
65 A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
66 C<foo/CGI_hlagh_bar_cgi>, for controller Foo, with the C</>s converted to C<_>s
67 and prepended with C<CGI_>, as well as all non-word characters converted to
68 C<_>s. This is because L<Catalyst> action names can't have non-word characters
71 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
72 module for configuration information.
76 sub register_actions {
77 my ($self, $app) = @_;
79 my $cgi_bin = $app->path_to('root', 'cgi-bin');
81 my $namespace = $self->action_namespace($app);
83 my $class = ref $self || $self;
85 for my $file (File::Find::Rule->file->in($cgi_bin)) {
86 my $cgi_path = abs2rel($file, $cgi_bin);
88 next if any { $_ eq '.svn' } splitdir $cgi_path;
92 if ($self->is_perl_cgi($file)) { # syntax check passed
94 $cgi = $self->wrap_perl_cgi($file);
97 $cgi = $self->wrap_nonperl_cgi($file);
100 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
103 my $path = join '/' => splitdir($cgi_path);
104 my $action_name = $self->cgi_action($path);
105 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
106 my $attrs = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
109 my ($controller, $context) = @_;
110 $controller->cgi_to_response($context, $cgi)
113 my $action = $self->create_action(
114 name => $action_name,
117 namespace => $namespace,
122 $app->dispatcher->register($app, $action);
125 $self->next::method($app, @_);
130 =head2 $self->cgi_action($cgi_path)
132 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
133 the action name it is registered as. See L</DESCRIPTION> for a discussion on how
134 CGI actions are named.
139 my ($self, $cgi) = @_;
141 my $action_name = 'CGI_' . join '_' => split '/' => $cgi;
142 $action_name =~ s/\W/_/g;
147 =head2 $self->is_perl_cgi($path)
149 Tries to figure out whether the CGI is Perl or not.
151 If it's Perl, it will be inlined into a sub instead of being forked off, see
152 wrap_perl_cgi (below.)
154 If it's not doing what you expect, you might want to override it, and let me
160 my ($self, $cgi) = @_;
162 my $shebang = IO::File->new($cgi)->getline;
164 return 0 if $shebang !~ /perl/ || $cgi !~ /\.pl\z/;
166 my $taint_check = $shebang =~ /-T/ ? '-T' : '';
168 open NULL, '>', File::Spec->devnull;
169 my $pid = open3(gensym, '&>NULL', '&>NULL', "$^X $taint_check -c $cgi");
176 =head2 $self->wrap_perl_cgi($path)
178 Takes the path to a Perl CGI and returns a coderef suitable for passing to
179 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
183 eval 'sub {' . slurp($path) . '}'
185 This is similar to how L<ModPerl::Registry> works, but will only work for
186 well-written CGIs. Otherwise, you may have to override this method to do
187 something more involved (see L<ModPerl::PerlRun>.)
192 my ($self, $cgi) = @_;
194 do { no warnings; eval 'sub {' . slurp($cgi) . '}' }
197 =head2 $self->wrap_nonperl_cgi($path)
199 Takes the path to a non-Perl CGI and returns a coderef for executing it.
207 sub wrap_nonperl_cgi {
208 my ($self, $cgi) = @_;
215 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
216 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
220 Rafael Kitover, C<< <rkitover at cpan.org> >>
224 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
225 rt.cpan.org>, or through the web interface at
226 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
227 I will be notified, and then you'll automatically be notified of progress on
228 your bug as I make changes.
236 =item * RT: CPAN's request tracker
238 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
240 =item * AnnoCPAN: Annotated CPAN documentation
242 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
246 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
250 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
254 =head1 COPYRIGHT & LICENSE
256 Copyright (c) 2008 Rafael Kitover
258 This program is free software; you can redistribute it and/or modify it
259 under the same terms as Perl itself.
263 1; # End of Catalyst::Controller::CGIBin
265 # vim: expandtab shiftwidth=4 ts=4 tw=80: