wrapcgi: releasing 0.0027 with kill_env from confound++
[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.005
26
27 =cut
28
29 our $VERSION = '0.005';
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         eval ' 
200             package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.';
201             sub {' . slurp($cgi) . '}'
202     }
203 }
204
205 =head2 $self->wrap_nonperl_cgi($path, $action_name)
206
207 Takes the path to a non-Perl CGI and returns a coderef for executing it.
208
209 C<$action_name> is the generated name for the action representing the CGI file.
210
211 By default returns:
212
213     sub { system $path }
214
215 =cut
216
217 sub wrap_nonperl_cgi {
218     my ($self, $cgi, $action_name) = @_;
219
220     sub { system $cgi }
221 }
222
223 =head1 SEE ALSO
224
225 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
226 L<Catalyst::Controller>, L<CGI>, L<Catalyst>
227
228 =head1 AUTHOR
229
230 Rafael Kitover, C<< <rkitover at cpan.org> >>
231
232 =head1 BUGS
233
234 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
235 rt.cpan.org>, or through the web interface at
236 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
237 I will be notified, and then you'll automatically be notified of progress on
238 your bug as I make changes.
239
240 =head1 SUPPORT
241
242 More information at:
243
244 =over 4
245
246 =item * RT: CPAN's request tracker
247
248 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
249
250 =item * AnnoCPAN: Annotated CPAN documentation
251
252 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
253
254 =item * CPAN Ratings
255
256 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
257
258 =item * Search CPAN
259
260 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
261
262 =back
263
264 =head1 COPYRIGHT & LICENSE
265
266 Copyright (c) 2008 Rafael Kitover
267
268 This program is free software; you can redistribute it and/or modify it
269 under the same terms as Perl itself.
270
271 =cut
272
273 1; # End of Catalyst::Controller::CGIBin
274
275 # vim: expandtab shiftwidth=4 ts=4 tw=80: