Catalyst/Controller/CGIBin.pm

filter: Statementstmt 93.3% 140 / 150 Branchbran 64.2% 27 / 42 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 96.3% 26 / 27 Podpod 100% 7 / 7 totaltotal 86.3% 202 / 234 CC 26 SCAR 20.6
CC
26
Cov
86%
register_actions
18.1
is_perl_cgi
6.8
__ANON__
6.6
CRAP 7.8
« Catalyst/Controller/WrapCGI.pm ↑ Summary CatalystX/GlobalContext.pm »
1 17 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Catalyst::Controller::CGIBin;
3 17 our $AUTHORITY = 'cpan:RKITOVER';
4 17 $Catalyst::Controller::CGIBin::VERSION = '0.039';
5 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 extends 'Catalyst::Controller::WrapCGI';
10  
11 17 ▶use File::Find::Rule ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use File::Spec::Functions qw/splitdir abs2rel/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use List::MoreUtils 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use File::Temp 'tempfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use File::pushd;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use CGI::Compile;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 NAME
24  
25 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
26  
27 =head1 SYNOPSIS
28  
29 In your controller:
30  
31 package MyApp::Controller::Foo;
32  
33 use parent qw/Catalyst::Controller::CGIBin/;
34  
35 In your .conf:
36  
37 <Controller::Foo>
38 cgi_root_path cgi-bin
39 cgi_dir cgi-bin
40 cgi_chain_root /optional/private/path/to/Chained/root
41 cgi_file_pattern *.cgi
42 # or regex
43 cgi_file_pattern /\.pl\z/
44 <CGI>
45 username_field username # used for REMOTE_USER env var
46 pass_env PERL5LIB
47 pass_env PATH
48 pass_env /^MYAPP_/
49 </CGI>
50 </Controller::Foo>
51  
52 =head1 DESCRIPTION
53  
54 Dispatches to CGI files in root/cgi-bin for /cgi-bin/ paths.
55  
56 Unlike L<ModPerl::Registry> this module does _NOT_ stat and recompile the CGI
57 for every invocation. This may be supported in the future if there's interest.
58  
59 CGI paths are converted into action names using L</cgi_action>.
60  
61 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
62 module for other configuration information.
63  
64 =head1 CONFIG PARAMS
65  
66 =head2 cgi_root_path
67  
68 The global URI path prefix for CGIs, defaults to C<cgi-bin>.
69  
70 =head2 cgi_chain_root
71  
72 By default L<Path|Catalyst::DispatchType::Path> actions are created for CGIs,
73 but if you specify this option, the actions will be created as
74 L<Chained|Catalyst::DispatchType::Chained> end-points, chaining off the
75 specified private path.
76  
77 If this option is used, the L</cgi_root_path> option is ignored. The root path
78 will be determined by your chain.
79  
80 The L<PathPart|Catalyst::DispatchType::Chained/PathPart> of the action will be
81 the path to the CGI file.
82  
83 =head2 cgi_dir
84  
85 Path from which to read CGI files. Can be relative to C<$MYAPP_HOME/root> or
86 absolute. Defaults to C<$MYAPP_HOME/root/cgi-bin>.
87  
88 =head2 cgi_file_pattern
89  
90 By default all files in L</cgi_dir> will be loaded as CGIs, however, with this
91 option you can specify either a glob or a regex to match the names of files you
92 want to be loaded.
93  
94 Can be an array of globs/regexes as well.
95  
96 =cut
97  
98 17 { my $stringified = subtype as 'Str';
99 coerce $stringified,
100 from 'Object',
101 2 ▶ via { "$_" };
Subroutine __ANON__: called CC=1 SCAR=0.0
102  
103 17 has cgi_root_path => (is => 'ro', coerce => 1, isa => $stringified, default => 'cgi-bin' );
104 17 has cgi_chain_root => (is => 'ro', isa => 'Str');
105 17 has cgi_dir => (is => 'ro', coerce => 1, isa => $stringified, default => 'cgi-bin');
106 11 ▶ has cgi_file_pattern => (is => 'rw', default => sub { ['*'] });
Subroutine __ANON__: called CC=1 SCAR=0.0
107  
108 }
109  
110 20 ▶sub register_actions {
Subroutine register_actions: called CC=17 SCAR=29.0
Pod documented
111 20 my ($self, $app) = @_;
112  
113 20 my $cgi_bin;
114 20 ▶ if( File::Spec->file_name_is_absolute($self->cgi_dir) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($self->cgi_dir)) { } 2 18 20
elsif ('File::Spec'->file_name_is_absolute($app->config->{'root'})) { } 15 3 18
115 2 $cgi_bin = $self->cgi_dir;
116 } elsif( File::Spec->file_name_is_absolute( $app->config->{root} ) ) {
117 15 $cgi_bin = File::Spec->catdir( $app->config->{root}, $self->cgi_dir );
118 } else {
119 3 $cgi_bin = $app->path_to( $app->config->{root}, $self->cgi_dir);
120 }
121  
122 20 my $namespace = $self->action_namespace($app);
123  
124 20 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
2000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
125  
126 20 my $patterns = $self->cgi_file_pattern;
127 20 ▶ $patterns = [ $patterns ] if not ref $patterns;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $patterns 0 20 20
128 20 for my $pat (@$patterns) {
129 29 ▶ if ($pat =~ m{^/(.*)/\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat =~ m[^/(.*)/\z]) 0 29 29
130 0 $pat = qr/$1/;
131 }
132 }
133 20 $self->cgi_file_pattern($patterns);
134  
135 20 for my $file (File::Find::Rule->file->name(@$patterns)->in($cgi_bin)) {
136 55 my $cgi_path = abs2rel($file, $cgi_bin);
137  
138 55 ▶ next if any { $_ eq '.svn' } splitdir $cgi_path;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq ".svn"; } , splitdir($cgi_path)) 0 55 55
139 55 ▶ next if $cgi_path =~ /\.swp\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $cgi_path =~ /\.swp\z/ 0 55 55
140  
141 55 my $path = join '/' => splitdir($cgi_path);
142 55 my $action_name = $self->cgi_action($path);
143 55 ▶ my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
Branch: 1 branch
BranchTrueFalseTotal
$namespace ? : 55 0 55
144  
145 55 my $attrs = do {
146 55 ▶ if (my $chain_root = $self->cgi_chain_root) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chain_root = $self->cgi_chain_root) { } 2 53 55
147 2 { Chained => [ $chain_root ], PathPart => [ $path ], Args => [] };
148 }
149 else {
150 53 { Path => [ $self->cgi_path($path) ] };
151 }
152 };
153  
154 55 my ($cgi, $type);
155  
156 55 ▶ if ($self->is_perl_cgi($file)) { # syntax check passed
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_perl_cgi($file)) { } 35 8 43
157 35 $type = 'Perl';
158 35 $cgi = $self->wrap_perl_cgi($file, $action_name);
159 } else {
160 8 $type = 'Non-Perl';
161 8 $cgi = $self->wrap_nonperl_cgi($file, $action_name);
162 }
163  
164 43 ▶ $app->log->info("Registering root/cgi-bin/$cgi_path as a $type CGI.")
Branch: 1 branch
BranchTrueFalseTotal
if $app->debug 0 43 43
165 if $app->debug;
166  
167 16 ▶ my $code = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
168 16 my ($controller, $context) = @_;
169 16 $controller->cgi_to_response($context, $cgi)
170 43 };
171  
172 43 my $action = $self->create_action(
173 name => $action_name,
174 code => $code,
175 reverse => $reverse,
176 namespace => $namespace,
177 class => $class,
178 attributes => $attrs
179 );
180  
181 43 $app->dispatcher->register($app, $action);
182 }
183  
184 8 $self->next::method($app, @_);
185  
186 # Tell Static::Simple to ignore cgi_dir
187 8 ▶ if ($cgi_bin =~ /^\Q@{[ $app->path_to('root') ]}\E/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cgi_bin =~ /^\Q@{[$app->path_to("root")];}\E/) 6 2 8
188 6 my $rel = File::Spec->abs2rel($cgi_bin, $app->path_to('root'));
189  
190 0 ▶ if (!any { $_ eq $rel }
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if (not &any(sub { $_ eq $rel; } , @{$app->config->{"Plgin::Static::Simple"}{"ignore_dirs"} or [];})) 6 0 6
191 6 ▶ @{ $app->config->{'Plgin::Static::Simple'}{ignore_dirs}||[] }) {
Condition50%1/2
$app->config->{'Plgin::Static::Simple'}{'ignore_dirs'} || []
l!l
06
MC/DC0%0/1
$app->config->{'Plgin::Static::Simple'}{'ignore_dirs'} || []
$app->config->{'Plgin::Static::Simple'}{'ignore_dirs'}
Truth table50%1/2
A
A= $app->config->{'Plgin::Static::Simple'}{'ignore_dirs'}
Aresult
11
00
192 6 push @{ $app->config->{'Plugin::Static::Simple'}{ignore_dirs} }, $rel;
193 }
194 }
195 }
196  
197 =head1 METHODS
198  
199 =head2 cgi_action
200  
201 C<< $self->cgi_action($cgi) >>
202  
203 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
204 the action name it is registered as.
205  
206 =cut
207  
208 56 ▶sub cgi_action {
Subroutine cgi_action: called CC=1 SCAR=0.0
Pod documented
209 56 my ($self, $cgi) = @_;
210  
211 56 my $action_name = 'CGI_' . $cgi;
212 56 $action_name =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
213  
214 56 return $action_name;
215 }
216  
217 =head2 cgi_path
218  
219 C<< $self->cgi_path($cgi) >>
220  
221 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
222 the public path it should be registered under.
223  
224 The default is to prefix with C<$cgi_root_path/>, using the C<cgi_root_path>
225 config setting, above.
226  
227 =cut
228  
229 9 ▶sub cgi_path {
Subroutine cgi_path: called CC=1 SCAR=0.0
Pod documented
230 9 my ($self, $cgi) = @_;
231  
232 9 my $root = $self->cgi_root_path;
233 9 $root =~ s{/*$}{};
234 9 return "$root/$cgi";
235 }
236  
237 =head2 is_perl_cgi
238  
239 C<< $self->is_perl_cgi($path) >>
240  
241 Tries to figure out whether the CGI is Perl or not.
242  
243 If it's Perl, it will be inlined into a sub instead of being forked off, see
244 L</wrap_perl_cgi>.
245  
246 =cut
247  
248 55 ▶sub is_perl_cgi {
Subroutine is_perl_cgi: called CC=6 SCAR=19.2
Pod documented
249 55 my ($self, $cgi) = @_;
250  
251 55 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 55 55
252 # the fork code fails on Win32
253 0 eval { $self->wrap_perl_cgi($cgi, '__DUMMY__') };
254 0 ▶ my $success = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
255 0 require Class::Unload;
256 0 Class::Unload->unload($self->cgi_package('__DUMMY__'));
257 0 return $success;
258 }
259  
260 55 my (undef, $tempfile) = tempfile;
261  
262 55 my $pid = fork;
263 55 ▶ die "Cannot fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 55 55
264  
265 55 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 43 12 55
266 43 waitpid $pid, 0;
267 43 my $errors = IO::File->new($tempfile)->getline;
268 43 unlink $tempfile;
269 43 ▶ return $errors ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$errors ? : 8 35 43
270 }
271  
272 # child
273 12 local *NULL;
274 12 open NULL, '>', File::Spec->devnull;
275 12 open STDOUT, '>&', \*NULL;
276 12 open STDERR, '>&', \*NULL;
277 12 close STDIN;
278  
279 12 eval { $self->wrap_perl_cgi($cgi, '__DUMMY__') };
280  
281 12 IO::File->new(">$tempfile")->print($@);
282  
283 12 exit;
284 }
285  
286 =head2 wrap_perl_cgi
287  
288 C<< $self->wrap_perl_cgi($path, $action_name) >>
289  
290 Takes the path to a Perl CGI and returns a coderef suitable for passing to
291 cgi_to_response (from L<Catalyst::Controller::WrapCGI>) using L<CGI::Compile>.
292  
293 C<$action_name> is the generated name for the action representing the CGI file
294 from C<cgi_action>.
295  
296 This is similar to how L<ModPerl::Registry> works, but will only work for
297 well-written CGIs. Otherwise, you may have to override this method to do
298 something more involved (see L<ModPerl::PerlRun>.)
299  
300 Scripts with C<__DATA__> sections now work too, as well as scripts that call
301 C<exit()>.
302  
303 =cut
304  
305 47 ▶sub wrap_perl_cgi {
Subroutine wrap_perl_cgi: called CC=1 SCAR=0.0
Pod documented
306 47 my ($self, $cgi, $action_name) = @_;
307  
308 47 return CGI::Compile->compile($cgi, $self->cgi_package($action_name));
309 }
310  
311 =head2 cgi_package
312  
313 C<< $self->cgi_package($action_name) >>
314  
315 Returns the package name a Perl CGI is compiled into for a given
316 C<$action_name>.
317  
318 =cut
319  
320 47 ▶sub cgi_package {
Subroutine cgi_package: called CC=1 SCAR=0.0
Pod documented
321 47 my ($self, $action_name) = @_;
322  
323 47 return "Catalyst::Controller::CGIBin::_CGIs_::$action_name";
324 }
325  
326 =head2 wrap_nonperl_cgi
327  
328 C<< $self->wrap_nonperl_cgi($path, $action_name) >>
329  
330 Takes the path to a non-Perl CGI and returns a coderef for executing it.
331  
332 C<$action_name> is the generated name for the action representing the CGI file.
333  
334 By default returns something like:
335  
336 sub { system $path }
337  
338 =cut
339  
340 8 ▶sub wrap_nonperl_cgi {
Subroutine wrap_nonperl_cgi: called CC=1 SCAR=0.4
Pod documented
341 8 my ($self, $cgi, $action_name) = @_;
342  
343 2 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=18.9
344 2 system $cgi;
345  
346 2 ▶ if ($? == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 0 2 2
elsif ($? & 127) { } 0 2 2
347 0 die "failed to execute CGI '$cgi': $!";
348 }
349 elsif ($? & 127) {
350 0 ▶ die sprintf "CGI '$cgi' died with signal %d, %s coredump",
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 0 0
351 ($? & 127), ($? & 128) ? 'with' : 'without';
352 }
353 else {
354 2 my $exit_code = $? >> 8;
355  
356 2 ▶ return 0 if $exit_code == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code == 0 1 1 2
357  
358 1 die "CGI '$cgi' exited non-zero with: $exit_code";
359 }
360 8 };
361 }
362  
363 17 __PACKAGE__->meta->make_immutable;
364  
365 =head1 SEE ALSO
366  
367 L<Catalyst::Controller::WrapCGI>, L<CatalystX::GlobalContext>,
368 L<Catalyst::Controller>, L<CGI>, L<CGI::Compile>, L<Catalyst>
369  
370 =head1 BUGS
371  
372 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
373 rt.cpan.org>, or through the web interface at
374 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
375 I will be notified, and then you'll automatically be notified of progress on
376 your bug as I make changes.
377  
378 =head1 AUTHOR
379  
380 Rafael Kitover <rkitover@gmail.com>
381  
382 =head1 COPYRIGHT & LICENSE
383  
384 Copyright (c) 2008-2015 Rafael Kitover <rkitover@gmail.com> and
385 L<Catalyst::Controller::WrapCGI/CONTRIBUTORS>.
386  
387 This program is free software; you can redistribute it and/or modify it
388 under the same terms as Perl itself.
389  
390 =cut
391  
392 17 __PACKAGE__; # End of Catalyst::Controller::CGIBin
393  
394 # vim:et sw=4 sts=4 tw=0:
« Catalyst/Controller/WrapCGI.pm ↑ Summary CatalystX/GlobalContext.pm »