blib/lib/Plack/Middleware/ComboLoader.pm

filter: Statementstmt 92.7% 102 / 110 Branchbran 83.3% 20 / 24 Conditioncond 46.6% 7 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 92.3% 12 / 13 Podpod 100% 1 / 1 totaltotal 83.1% 143 / 172 CC 20 SCAR 26.5
CC
20
Cov
87%
call
22.1
__ANON__
2.0
BEGIN
1.0
CRAP 14.2
↑ Summary
1 package Plack::Middleware::ComboLoader;
2 {
3 3 $Plack::Middleware::ComboLoader::VERSION = '0.04';
4 }
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use URI::Escape 'uri_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use HTTP::Date 'time2str';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # ABSTRACT: Handle combination loading and processing of on-disk resources.
22  
23 3 __PACKAGE__->mk_accessors(qw( roots save expires max_age));
24  
25  
26 7 ▶sub call {
Subroutine call: called CC=20 SCAR=31.0
Pod documented
27 7 my ( $self, $env ) = @_;
28  
29 7 ▶ my $roots = $self->roots || {};
Condition50%1/2
$self->roots || {}
l!l
70
MC/DC0%0/1
$self->roots || {}
$self->roots
Truth table50%1/2
A
A= $self->roots
Aresult
11
00
30 7 ▶ unless ( ref($roots) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $roots eq "HASH") 0 7 7
31 0 carp "Invalid root configuration, roots must be a hash ref of names to paths\n";
32 }
33  
34 7 my $path_info = $env->{PATH_INFO};
35 7 $path_info =~ s/^\///;
36  
37 7 my $req = Plack::Request->new( $env );
38 7 my $res = $req->new_response;
39  
40 7 ▶ if ( exists $roots->{$path_info} or exists $roots->{"/$path_info"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $roots->{$path_info} or exists $$roots{"/$path_info"}) 7 0 7
Condition33%1/3
exists $roots->{$path_info} or exists $$roots{"/$path_info"}
l!l&&r!l&&!r
700
MC/DC0%0/2
exists $roots->{$path_info} or exists $$roots{"/$path_info"}
exists $roots->{$path_info} exists $$roots{"/$path_info"}
Truth table33%1/3
A or B
A= exists $roots->{$path_info}B= exists $$roots{"/$path_info"}
ABresult
1X1
011
000
41 7 ▶ my $path = $roots->{$path_info} || $roots->{"/$path_info"};
Condition33%1/3
$roots->{$path_info} || $$roots{"/$path_info"}
l!l&&r!l&&!r
700
MC/DC0%0/2
$roots->{$path_info} || $$roots{"/$path_info"}
$roots->{$path_info} $$roots{"/$path_info"}
Truth table33%1/3
A || B
A= $roots->{$path_info}B= $$roots{"/$path_info"}
ABresult
1X1
011
000
42 7 my $config = {};
43 7 ▶ if ( ref $path eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path eq 'HASH') { } 1 6 7
44 1 $config = $path;
45 } else {
46 6 $config->{path} = $path;
47 }
48  
49 7 my $dir = Path::Class::Dir->new($config->{path});
50 7 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 7 7
51 0 $res->status(500);
52 0 $res->body("Invalid root directory for `/$path_info`: $dir does not exist");
53 0 return $res->finalize;
54 }
55  
56 7 my @resources = split('&', $env->{QUERY_STRING});
57 7 $res->status(200);
58  
59 7 my $content_type = 'plain/text';
60 7 ▶ my $max_age = defined $self->max_age ? $self->max_age : 315360000;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->max_age ? : 0 7 7
61  
62 7 ▶ if ( $self->save ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->save) 3 4 7
63 3 my $save_dir = Path::Class::Dir->new($self->save)->subdir($path_info);
64 3 my $f = $save_dir->file( uri_escape($env->{QUERY_STRING}) );
65 3 my $stat = $f->stat;
66 3 ▶ my $expiry = $self->expires || 86400;
Condition50%1/2
$self->expires || 86400
l!l
03
MC/DC0%0/1
$self->expires || 86400
$self->expires
Truth table50%1/2
A
A= $self->expires
Aresult
11
00
67 3 ▶ if ( $stat && $stat->mtime + $expiry > time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat and $stat->mtime + $expiry > time) 1 2 3
Condition66%2/3
$stat and $stat->mtime + $expiry > time
!ll&&!rl&&r
201
MC/DC50%1/2
$stat and $stat->mtime + $expiry > time
$stat $stat->mtime + $expiry > time
Truth table66%2/3
A and B
A= $statB= $stat->mtime + $expiry > time
ABresult
0X0
100
111
68 # Not sure what the best way to do this is. Looking at
69 # Plack::App::File
70 1 my ( $content_type, @buffer ) = $f->slurp;
71 1 $res->header('Last-Modified' => time2str( $stat->mtime ));
72 1 $res->header('X-Generated-On' => time2str( $stat->mtime ));
73  
74 1 $res->header('Age' => 0);
75 1 $res->header('Cache-Control' => "public, max-age=$max_age");
76 1 $res->header('Expires' => time2str( time + $max_age ) );
77  
78 1 $res->content_type($content_type);
79 1 $res->content(join("", @buffer));
80 1 return $res->finalize;
81 }
82 }
83  
84 6 my $buffer = '';
85 6 my $last_modified = 0;
86 6 my %seen_types = ();
87  
88 6 foreach my $resource ( @resources ) {
89 13 my $f = $dir->file($resource);
90 13 my $stat = $f->stat;
91 13 ▶ unless ( defined $stat ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stat) 1 12 13
92 1 $res->status(400);
93 1 $res->content("Invalid resource requested: `$resource` is not available.");
94 1 return $res->finalize;
95 }
96  
97 12 ▶ $seen_types{ Plack::MIME->mime_type($f->basename) || 'text/plain' } = 1;
Condition50%1/2
'Plack::MIME'->mime_type($f->basename) || 'text/plain'
l!l
120
MC/DC0%0/1
'Plack::MIME'->mime_type($f->basename) || 'text/plain'
'Plack::MIME'->mime_type($f->basename)
Truth table50%1/2
A
A= 'Plack::MIME'->mime_type($f->basename)
Aresult
11
00
98 # Set the last modified to the most recent file.
99 12 ▶ $last_modified = $stat->mtime if $stat->mtime > $last_modified;
Branch: 1 branch
BranchTrueFalseTotal
if $stat->mtime > $last_modified 6 6 12
100  
101 12 ▶ if ( exists $config->{processor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{'processor'}) { } 2 10 12
102 2 local $_ = $f;
103 2 ▶ try { $buffer .= $config->{processor}->($f); }
Subroutine __ANON__: called CC=1 SCAR=0.0
104 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 $res->status(500);
106 0 $res->body("Processing failed for `$resource`: $_");
107 0 return $res->finalize;
108 2 };
109 } else {
110 10 $buffer .= $f->slurp;
111 }
112 }
113 5 ▶ if ( $self->save ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->save) 2 3 5
114 2 my $save_dir = Path::Class::Dir->new($self->save)->subdir($path_info);
115 2 $save_dir->mkpath;
116 2 my $f = $save_dir->file( uri_escape($env->{QUERY_STRING}) );
117 2 my $fh = $f->openw;
118 2 print $fh "$content_type\n";
119 2 print $fh $buffer;
120 2 $fh->close;
121 }
122  
123 # We only encountered one content-type, rejoice, for we can set one
124 # sensibly!
125 5 ▶ if ( scalar keys %seen_types == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %seen_types == 1) 4 1 5
126 4 ( $content_type ) = keys %seen_types;
127 }
128  
129 5 $res->content_type($content_type);
130  
131 5 $res->header('Last-Modified' => time2str( $last_modified ) );
132 5 $res->header('Age' => 0);
133 5 $res->header('Cache-Control' => "public, max-age=$max_age");
134 5 $res->header('Expires' => time2str( time + $max_age ) );
135  
136 5 $res->content($buffer);
137 5 return $res->finalize;
138 }
139  
140 0 $self->app->($env);
141 }
142  
143 3 1;
144  
145 __END__
↑ Summary