| 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 || {}
MC/DC0%0/1
$self->roots || {}
$self->roots
Truth table50%1/2
A
A= $self->roots
| |||||||||||||||||||||||||||||
| 30 | 7 | ▶ | unless ( ref($roots) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
exists $roots->{$path_info} or exists $$roots{"/$path_info"}
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"}
| |||||||||||||||||||||||||||||
| 41 | 7 | ▶ | my $path = $roots->{$path_info} || $roots->{"/$path_info"}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$roots->{$path_info} || $$roots{"/$path_info"}
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"}
| |||||||||||||||||||||||||||||
| 42 | 7 | my $config = {}; | |||||||||||||||||||||||||||
| 43 | 7 | ▶ | if ( ref $path eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 7 | ▶ | if ( $self->save ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$self->expires || 86400
$self->expires
Truth table50%1/2
A
A= $self->expires
| |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | if ( $stat && $stat->mtime + $expiry > time ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$stat and $stat->mtime + $expiry > time
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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'
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)
| |||||||||||||||||||||||||||||
| 98 | # Set the last modified to the most recent file. | ||||||||||||||||||||||||||||
| 99 | 12 | ▶ | $last_modified = $stat->mtime if $stat->mtime > $last_modified; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 12 | ▶ | if ( exists $config->{processor} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||