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';
18 use namespace::clean -except => 'meta';
22 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
30 our $VERSION = '0.010';
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 serve_cgi : Local Args(0) {
43 $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
52 username_field username # used for REMOTE_USER env var
61 Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
63 Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
64 for every invocation. This may be supported in the future if there's interest.
66 CGI paths are converted into action names using L</cgi_action>.
68 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
69 module for other configuration information.
75 The global URI path prefix for CGIs, defaults to C<cgi-bin>.
79 Path from which to read CGI files. Can be relative to C<$MYAPP_HOME/root> or
80 absolute. Defaults to C<$MYAPP_HOME/root/cgi-bin>.
84 has cgi_root_path => (is => 'ro', isa => 'Str', default => 'cgi-bin');
85 has cgi_dir => (is => 'ro', isa => 'Str', default => 'cgi-bin');
87 sub register_actions {
88 my ($self, $app) = @_;
90 my $cgi_bin = File::Spec->file_name_is_absolute($self->cgi_dir) ?
92 : $app->path_to('root', $self->cgi_dir);
94 my $namespace = $self->action_namespace($app);
96 my $class = ref $self || $self;
98 for my $file (File::Find::Rule->file->in($cgi_bin)) {
99 my $cgi_path = abs2rel($file, $cgi_bin);
101 next if any { $_ eq '.svn' } splitdir $cgi_path;
102 next if $cgi_path =~ /\.swp\z/;
104 my $path = join '/' => splitdir($cgi_path);
105 my $action_name = $self->cgi_action($path);
106 my $public_path = $self->cgi_path($path);
107 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
108 my $attrs = { Path => [ $public_path ] };
112 if ($self->is_perl_cgi($file)) { # syntax check passed
114 $cgi = $self->wrap_perl_cgi($file, $action_name);
117 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
120 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
124 my ($controller, $context) = @_;
125 $controller->cgi_to_response($context, $cgi)
128 my $action = $self->create_action(
129 name => $action_name,
132 namespace => $namespace,
137 $app->dispatcher->register($app, $action);
140 $self->next::method($app, @_);
142 # Tell Static::Simple to ignore cgi_dir
143 if ($cgi_bin =~ /^@{[ $app->path_to('root') ]}/) {
144 my $rel = File::Spec->abs2rel($cgi_bin, $app->path_to('root'));
146 if (!any { $_ eq $rel }
147 @{ $app->config->{static}{ignore_dirs}||[] }) {
148 push @{ $app->config->{static}{ignore_dirs} }, $rel;
157 C<<$self->cgi_action($cgi)>>
159 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
160 the action name it is registered as. See L</DESCRIPTION> for a discussion on how
161 CGI actions are named.
163 A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
164 C<foo/CGI_hlagh__bar_cgi>, for controller Foo, with the C</>s converted to C<__>
165 and prepended with C<CGI_>, as well as all non-word characters converted to
166 C<_>s. This is because L<Catalyst> action names can't have non-word characters
169 This means that C<foo/bar.cgi> and C<foo__bar.cgi> for example will both map to
170 the action C<CGI_foo__bar_cgi> so B<DON'T DO THAT>.
175 my ($self, $cgi) = @_;
177 my $action_name = 'CGI_' . join '__' => split '/' => $cgi;
178 $action_name =~ s/\W/_/g;
185 C<<$self->cgi_path($cgi)>>
187 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
188 the public path it should be registered under.
190 The default is to prefix with C<$cgi_root_path/>, using the C<cgi_root_path>
191 config setting, above.
196 my ($self, $cgi) = @_;
198 my $root = $self->cgi_root_path;
205 C<<$self->is_perl_cgi($path)>>
207 Tries to figure out whether the CGI is Perl or not.
209 If it's Perl, it will be inlined into a sub instead of being forked off, see
215 my ($self, $cgi) = @_;
217 my $shebang = IO::File->new($cgi)->getline;
219 return 0 if $shebang !~ /perl/ && $cgi !~ /\.pl\z/;
221 my $taint_check = $shebang =~ /-T/ ? '-T' : '';
223 open NULL, '>', File::Spec->devnull;
224 my $pid = open3(gensym, '&>NULL', '&>NULL', "$^X $taint_check -c $cgi");
233 C<<$self->wrap_perl_cgi($path, $action_name)>>
235 Takes the path to a Perl CGI and returns a coderef suitable for passing to
236 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
238 C<$action_name> is the generated name for the action representing the CGI file
241 This is similar to how L<ModPerl::Registry> works, but will only work for
242 well-written CGIs. Otherwise, you may have to override this method to do
243 something more involved (see L<ModPerl::PerlRun>.)
245 Scripts with C<__DATA__> sections now work too, as well as scripts that call
251 my ($self, $cgi, $action_name) = @_;
253 my $code = slurp $cgi;
255 $code =~ s/^__DATA__(?:\r?\n|\r\n?)(.*)//ms;
260 # catch exit() and turn it into (effectively) a return
261 # we *must* eval STRING because the code needs to be compiled with the
262 # overridden CORE::GLOBAL::exit in view
264 # set $0 to the name of the cgi file in case it's used there
266 my $cgi_exited = "EXIT\n";
267 BEGIN { *CORE::GLOBAL::exit = sub (;$) {
268 die [ $cgi_exited, $_[0] || 0 ];
270 package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
273 . q{open DATA, '<', \$data;}
274 . qq{local \$0 = "\Q$cgi\E";}
279 return $rv unless $@;
280 die $@ if $@ and not (
281 ref($@) eq 'ARRAY' and
282 $@->[0] eq $cgi_exited
284 die "exited nonzero: $@->[1]" if $@->[1] != 0;
290 croak __PACKAGE__ . ": Could not compile $cgi to coderef: $@" if $@;
295 =head2 wrap_nonperl_cgi
297 C<<$self->wrap_nonperl_cgi($path, $action_name)>>
299 Takes the path to a non-Perl CGI and returns a coderef for executing it.
301 C<$action_name> is the generated name for the action representing the CGI file.
309 sub wrap_nonperl_cgi {
310 my ($self, $cgi, $action_name) = @_;
315 __PACKAGE__->meta->make_immutable;
319 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
320 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
324 Rafael Kitover, C<< <rkitover at cpan.org> >>
326 Hans Dieter Pearcey, C<< <hdp at cpan.org> >>
330 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
331 rt.cpan.org>, or through the web interface at
332 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
333 I will be notified, and then you'll automatically be notified of progress on
334 your bug as I make changes.
342 =item * RT: CPAN's request tracker
344 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
346 =item * AnnoCPAN: Annotated CPAN documentation
348 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
352 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
356 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
360 =head1 COPYRIGHT & LICENSE
362 Copyright (c) 2008 Rafael Kitover
364 This program is free software; you can redistribute it and/or modify it
365 under the same terms as Perl itself.
369 1; # End of Catalyst::Controller::CGIBin
371 # vim: expandtab shiftwidth=4 ts=4 tw=80: