C::C::CGIBin: add root/cgi-bin to $c->config->{static}{ignore_dirs}
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
CommitLineData
21a20b7e 1package Catalyst::Controller::CGIBin;
2
3use strict;
4use warnings;
5
6use Class::C3;
21a20b7e 7use File::Slurp 'slurp';
8use File::Find::Rule ();
21a20b7e 9use Catalyst::Exception ();
c264816e 10use File::Spec::Functions qw/splitdir abs2rel/;
11use IPC::Open3;
12use Symbol 'gensym';
13use List::MoreUtils 'any';
14use IO::File ();
2340af9d 15use namespace::clean -except => 'meta';
21a20b7e 16
17use parent 'Catalyst::Controller::WrapCGI';
18
19=head1 NAME
20
21Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
22
23=head1 VERSION
24
2340af9d 25Version 0.004
21a20b7e 26
27=cut
28
2340af9d 29our $VERSION = '0.004';
21a20b7e 30
31=head1 SYNOPSIS
32
33In your controller:
34
35 package MyApp::Controller::Foo;
36
37 use parent qw/Catalyst::Controller::CGIBin/;
38
39 # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
40 sub dongs : Local Args(0) {
41 my ($self, $c) = @_;
42 $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
43 }
44
45In your .conf:
46
47 <Controller::Foo>
48 <CGI>
49 username_field username # used for REMOTE_USER env var
50 pass_env PERL5LIB
51 pass_env PATH
52 pass_env /^MYAPP_/
53 </CGI>
54 </Controller::Foo>
55
56=head1 DESCRIPTION
57
c264816e 58Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
59
60Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
61for every invocation. If this is something you need, let me know.
21a20b7e 62
ee75330f 63CGI paths are converted into action names using cgi_action (below.)
64
21a20b7e 65A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
66C<foo/CGI_hlagh_bar_cgi>, for controller Foo, with the C</>s converted to C<_>s
67and prepended with C<CGI_>, as well as all non-word characters converted to
68C<_>s. This is because L<Catalyst> action names can't have non-word characters
69in them.
70
71Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
72module for configuration information.
73
74=cut
75
76sub register_actions {
ee75330f 77 my ($self, $app) = @_;
21a20b7e 78
ee75330f 79 my $cgi_bin = $app->path_to('root', 'cgi-bin');
21a20b7e 80
ee75330f 81 my $namespace = $self->action_namespace($app);
21a20b7e 82
83 my $class = ref $self || $self;
84
c264816e 85 for my $file (File::Find::Rule->file->in($cgi_bin)) {
86 my $cgi_path = abs2rel($file, $cgi_bin);
87
88 next if any { $_ eq '.svn' } splitdir $cgi_path;
89
12d29ebf 90 my $path = join '/' => splitdir($cgi_path);
91 my $action_name = $self->cgi_action($path);
92 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
93 my $attrs = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
94
21a20b7e 95 my ($cgi, $type);
21a20b7e 96
c264816e 97 if ($self->is_perl_cgi($file)) { # syntax check passed
21a20b7e 98 $type = 'Perl';
12d29ebf 99 $cgi = $self->wrap_perl_cgi($file, $action_name);
21a20b7e 100 } else {
21a20b7e 101 $type = 'Non-Perl';
12d29ebf 102 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
21a20b7e 103 }
104
c264816e 105 $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
ee75330f 106 if $app->debug;
21a20b7e 107
c264816e 108 my $code = sub {
21a20b7e 109 my ($controller, $context) = @_;
110 $controller->cgi_to_response($context, $cgi)
111 };
112
113 my $action = $self->create_action(
114 name => $action_name,
115 code => $code,
116 reverse => $reverse,
117 namespace => $namespace,
118 class => $class,
119 attributes => $attrs
120 );
121
ee75330f 122 $app->dispatcher->register($app, $action);
21a20b7e 123 }
124
ee75330f 125 $self->next::method($app, @_);
2340af9d 126
127# Tell Static::Simple to ignore the cgi-bin dir.
128 if (!any{ $_ eq 'cgi-bin' } @{ $app->config->{static}{ignore_dirs}||[] }) {
129 push @{ $app->config->{static}{ignore_dirs} }, 'cgi-bin';
130 }
21a20b7e 131}
132
133=head1 METHODS
134
135=head2 $self->cgi_action($cgi_path)
136
137Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
c264816e 138the action name it is registered as. See L</DESCRIPTION> for a discussion on how
139CGI actions are named.
21a20b7e 140
141=cut
142
143sub cgi_action {
144 my ($self, $cgi) = @_;
145
c264816e 146 my $action_name = 'CGI_' . join '_' => split '/' => $cgi;
21a20b7e 147 $action_name =~ s/\W/_/g;
148
149 $action_name
150}
151
c264816e 152=head2 $self->is_perl_cgi($path)
153
154Tries to figure out whether the CGI is Perl or not.
155
156If it's Perl, it will be inlined into a sub instead of being forked off, see
157wrap_perl_cgi (below.)
158
159If it's not doing what you expect, you might want to override it, and let me
160know as well!
161
162=cut
163
164sub is_perl_cgi {
165 my ($self, $cgi) = @_;
166
167 my $shebang = IO::File->new($cgi)->getline;
168
2369e5a4 169 return 0 if $shebang !~ /perl/ && $cgi !~ /\.pl\z/;
c264816e 170
171 my $taint_check = $shebang =~ /-T/ ? '-T' : '';
172
173 open NULL, '>', File::Spec->devnull;
174 my $pid = open3(gensym, '&>NULL', '&>NULL', "$^X $taint_check -c $cgi");
175 close NULL;
176 waitpid $pid, 0;
177
178 $? >> 8 == 0
179}
180
12d29ebf 181=head2 $self->wrap_perl_cgi($path, $action_name)
c264816e 182
183Takes the path to a Perl CGI and returns a coderef suitable for passing to
184cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
185
12d29ebf 186C<$action_name> is the generated name for the action representing the CGI file.
c264816e 187
188This is similar to how L<ModPerl::Registry> works, but will only work for
189well-written CGIs. Otherwise, you may have to override this method to do
190something more involved (see L<ModPerl::PerlRun>.)
191
192=cut
193
194sub wrap_perl_cgi {
12d29ebf 195 my ($self, $cgi, $action_name) = @_;
196
197 do {
198 no warnings;
199# CGIs import stuff, so putting them into this package breaks Cat 5.8
200 eval '
201 package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
202 sub {' . slurp($cgi) . '}'
203 }
c264816e 204}
205
12d29ebf 206=head2 $self->wrap_nonperl_cgi($path, $action_name)
c264816e 207
208Takes the path to a non-Perl CGI and returns a coderef for executing it.
209
12d29ebf 210C<$action_name> is the generated name for the action representing the CGI file.
211
c264816e 212By default returns:
213
214 sub { system $path }
215
216=cut
217
218sub wrap_nonperl_cgi {
12d29ebf 219 my ($self, $cgi, $action_name) = @_;
c264816e 220
221 sub { system $cgi }
222}
223
224=head1 SEE ALSO
225
226L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
227L<Catalyst::Controller>, L<CGI>, L<Catalyst>
228
21a20b7e 229=head1 AUTHOR
230
231Rafael Kitover, C<< <rkitover at cpan.org> >>
232
233=head1 BUGS
234
235Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
236rt.cpan.org>, or through the web interface at
237L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
238I will be notified, and then you'll automatically be notified of progress on
239your bug as I make changes.
240
241=head1 SUPPORT
242
243More information at:
244
245=over 4
246
247=item * RT: CPAN's request tracker
248
249L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
250
251=item * AnnoCPAN: Annotated CPAN documentation
252
253L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
254
255=item * CPAN Ratings
256
257L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
258
259=item * Search CPAN
260
261L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
262
263=back
264
265=head1 COPYRIGHT & LICENSE
266
267Copyright (c) 2008 Rafael Kitover
268
269This program is free software; you can redistribute it and/or modify it
270under the same terms as Perl itself.
271
272=cut
273
2741; # End of Catalyst::Controller::CGIBin
275
276# vim: expandtab shiftwidth=4 ts=4 tw=80: