C::C::CGIBin: add root/cgi-bin to $c->config->{static}{ignore_dirs}
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
1 package Catalyst::Controller::CGIBin;
2
3 use strict;
4 use warnings;
5
6 use Class::C3;
7 use File::Slurp 'slurp';
8 use File::Find::Rule ();
9 use Catalyst::Exception ();
10 use File::Spec::Functions qw/splitdir abs2rel/;
11 use IPC::Open3;
12 use Symbol 'gensym';
13 use List::MoreUtils 'any';
14 use IO::File ();
15 use namespace::clean -except => 'meta';
16
17 use parent 'Catalyst::Controller::WrapCGI';
18
19 =head1 NAME
20
21 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
22
23 =head1 VERSION
24
25 Version 0.004
26
27 =cut
28
29 our $VERSION = '0.004';
30
31 =head1 SYNOPSIS
32
33 In 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
45 In 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
58 Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
59
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.
62
63 CGI paths are converted into action names using cgi_action (below.)
64
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
69 in them.
70
71 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
72 module for configuration information.
73
74 =cut
75
76 sub register_actions {
77     my ($self, $app) = @_;
78
79     my $cgi_bin = $app->path_to('root', 'cgi-bin');
80
81     my $namespace = $self->action_namespace($app);
82
83     my $class = ref $self || $self;
84
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
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
95         my ($cgi, $type);
96
97         if ($self->is_perl_cgi($file)) { # syntax check passed
98             $type = 'Perl';
99             $cgi  = $self->wrap_perl_cgi($file, $action_name);
100         } else {
101             $type = 'Non-Perl';
102             $cgi  = $self->wrap_nonperl_cgi($file, $action_name);
103         }
104
105         $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
106             if $app->debug;
107
108         my $code = sub {
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
122         $app->dispatcher->register($app, $action);
123     }
124
125     $self->next::method($app, @_);
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     }
131 }
132
133 =head1 METHODS
134
135 =head2 $self->cgi_action($cgi_path)
136
137 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
138 the action name it is registered as. See L</DESCRIPTION> for a discussion on how
139 CGI actions are named.
140
141 =cut
142
143 sub cgi_action {
144     my ($self, $cgi) = @_;
145
146     my $action_name = 'CGI_' . join '_' => split '/' => $cgi;
147     $action_name    =~ s/\W/_/g;
148
149     $action_name
150 }
151
152 =head2 $self->is_perl_cgi($path)
153
154 Tries to figure out whether the CGI is Perl or not.
155
156 If it's Perl, it will be inlined into a sub instead of being forked off, see
157 wrap_perl_cgi (below.)
158
159 If it's not doing what you expect, you might want to override it, and let me
160 know as well!
161
162 =cut
163
164 sub is_perl_cgi {
165     my ($self, $cgi) = @_;
166
167     my $shebang = IO::File->new($cgi)->getline;
168
169     return 0 if $shebang !~ /perl/ && $cgi !~ /\.pl\z/;
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
181 =head2 $self->wrap_perl_cgi($path, $action_name)
182
183 Takes the path to a Perl CGI and returns a coderef suitable for passing to
184 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
185
186 C<$action_name> is the generated name for the action representing the CGI file.
187
188 This is similar to how L<ModPerl::Registry> works, but will only work for
189 well-written CGIs. Otherwise, you may have to override this method to do
190 something more involved (see L<ModPerl::PerlRun>.)
191
192 =cut
193
194 sub wrap_perl_cgi {
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     }
204 }
205
206 =head2 $self->wrap_nonperl_cgi($path, $action_name)
207
208 Takes the path to a non-Perl CGI and returns a coderef for executing it.
209
210 C<$action_name> is the generated name for the action representing the CGI file.
211
212 By default returns:
213
214     sub { system $path }
215
216 =cut
217
218 sub wrap_nonperl_cgi {
219     my ($self, $cgi, $action_name) = @_;
220
221     sub { system $cgi }
222 }
223
224 =head1 SEE ALSO
225
226 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
227 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
228
229 =head1 AUTHOR
230
231 Rafael Kitover, C<< <rkitover at cpan.org> >>
232
233 =head1 BUGS
234
235 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
236 rt.cpan.org>, or through the web interface at
237 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
238 I will be notified, and then you'll automatically be notified of progress on
239 your bug as I make changes.
240
241 =head1 SUPPORT
242
243 More information at:
244
245 =over 4
246
247 =item * RT: CPAN's request tracker
248
249 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
250
251 =item * AnnoCPAN: Annotated CPAN documentation
252
253 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
254
255 =item * CPAN Ratings
256
257 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
258
259 =item * Search CPAN
260
261 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
262
263 =back
264
265 =head1 COPYRIGHT & LICENSE
266
267 Copyright (c) 2008 Rafael Kitover
268
269 This program is free software; you can redistribute it and/or modify it
270 under the same terms as Perl itself.
271
272 =cut
273
274 1; # End of Catalyst::Controller::CGIBin
275
276 # vim: expandtab shiftwidth=4 ts=4 tw=80: