File Coverage

blib/lib/Plack/App/WWW.pm
Criterion Covered Total %
statement 52 55 94.5
branch 18 24 75.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 1 2 50.0
total 82 94 87.2


line stmt bran cond sub pod time code
1             package Plack::App::WWW;
2 2     2   74830 use strict;
  2         12  
  2         59  
3 2     2   11 use warnings;
  2         3  
  2         67  
4 2     2   490 use parent qw/Plack::App::CGIBin/;
  2         299  
  2         10  
5 2     2   158508 use Plack::App::File;
  2         4  
  2         49  
6 2     2   10 use Plack::App::WrapCGI;
  2         3  
  2         35  
7 2     2   1010 use Plack::App::Directory;
  2         115381  
  2         1165  
8              
9             our $VERSION = '0.03';
10              
11             sub call {
12 6     6 1 30345 my $self = shift;
13 6         10 my $env = shift;
14              
15 6         11 my $temp;
16              
17             # check if pathinfo is a folder
18 6 100       25 if (-d $self->root . $env->{PATH_INFO}) {
19             # find index
20 3         95 my $index = $self->_find_index($env->{PATH_INFO});
21              
22             # if index is false add directory index
23 3 100       36 return Plack::App::Directory->new(
24             root => $self->root
25             )->to_app->($env) unless $index;
26              
27             # set temp
28 1         3 $temp = $env->{PATH_INFO};
29              
30             # set index in pathinfo
31 1         3 $env->{PATH_INFO} = $index;
32             }
33              
34 4         170 my ($file, $path_info) = $self->locate_file($env);
35              
36             # back pathinfo original
37 4 100       393 $env->{PATH_INFO} = $temp if $temp;
38              
39 4 50       26 return $file if ref $file eq 'ARRAY';
40              
41 4 50       11 if ($path_info) {
42 0         0 $env->{'plack.file.SCRIPT_NAME'} = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
43 0         0 $env->{'plack.file.SCRIPT_NAME'} =~ s/\Q$path_info\E$//;
44 0         0 $env->{'plack.file.PATH_INFO'} = $path_info;
45             } else {
46 4         15 $env->{'plack.file.SCRIPT_NAME'} = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
47 4         10 $env->{'plack.file.PATH_INFO'} = '';
48             }
49              
50 4         11 return $self->serve_path($env, $file);
51             }
52              
53             sub serve_path {
54 4     4 0 11 my($self, $env, $file) = @_;
55              
56 4         8 local @{$env}{qw(SCRIPT_NAME PATH_INFO)} = @{$env}{qw( plack.file.SCRIPT_NAME plack.file.PATH_INFO )};
  4         20  
  4         10  
57              
58 4 100       13 if ($self->_valid_file_perl($file)) {
59 3   33     23 my $app = $self->{_compiled}->{$file} ||= Plack::App::WrapCGI->new(
60             script => $file, execute => $self->would_exec($file)
61             )->to_app;
62 3         2838 $app->($env);
63             } else {
64 1         13 Plack::App::File->new(file => $file)->to_app->($env);
65             }
66             }
67              
68             sub _find_index {
69 3     3   10 my ($self, $path_info) = @_;
70              
71 3         12 $path_info =~ s/\/$//;
72              
73 3 100       10 return $path_info . '/index.pl' if -e $self->root . $path_info . '/index.pl';
74 2 50       55 return $path_info . '/index.cgi' if -e $self->root . $path_info . '/index.cgi';
75 2 50       43 return $path_info . '/index.html' if -e $self->root . $path_info . '/index.html';
76 2 50       44 return $path_info . '/index.htm' if -e $self->root . $path_info . '/index.htm';
77              
78 2         39 return;
79             }
80              
81             sub _valid_file_perl {
82 4     4   6 my ($self, $file) = @_;
83              
84 4 100       36 return 1 if $file =~ /.(pl|cgi)$/i;
85 1 50       5 return 1 if $self->shebang_for($file) =~ /^\#\!.*perl/;
86              
87 1         85 return;
88             }
89              
90             1;
91              
92             __END__