| 1 | package Plack::Middleware::TemplateToolkit; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Serve files with Template Toolkit and Plack | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | $Plack::Middleware::TemplateToolkit::VERSION = '0.28'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use parent 'Exporter'; # for 'use Plack::Middleware::TemplateToolkit $version;' | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use parent 'Plack::Middleware'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Plack::Request 0.994; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Plack::MIME; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Template 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use HTTP::Status qw(status_message); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use Plack::Middleware::Debug::Timer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # Configuration options as described in Template::Manual::Config | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | our @TT_CONFIG; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | our @DEPRECATED; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 7 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | @TT_CONFIG = qw(ENCODING START_TAG END_TAG OUTLINE_TAG TAG_STYLE PRE_CHOMP POST_CHOMP TRIM | |||||||||||||||||||||||||||||||||||||||||
| 26 | INTERPOLATE ANYCASE INCLUDE_PATH DELIMITER ABSOLUTE RELATIVE DEFAULT | ||||||||||||||||||||||||||||||||||||||||||
| 27 | BLOCKS VIEWS AUTO_RESET RECURSION VARIABLES CONSTANTS | ||||||||||||||||||||||||||||||||||||||||||
| 28 | CONSTANT_NAMESPACE NAMESPACE PRE_PROCESS POST_PROCESS PROCESS WRAPPER | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ERROR EVAL_PERL OUTPUT OUTPUT_PATH STRICT DEBUG DEBUG_FORMAT | ||||||||||||||||||||||||||||||||||||||||||
| 30 | CACHE_SIZE STAT_TTL COMPILE_EXT COMPILE_DIR PLUGINS PLUGIN_BASE | ||||||||||||||||||||||||||||||||||||||||||
| 31 | LOAD_PERL FILTERS LOAD_TEMPLATES LOAD_PLUGINS LOAD_FILTERS | ||||||||||||||||||||||||||||||||||||||||||
| 32 | TOLERANT SERVICE CONTEXT STASH PARSER GRAMMAR | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # the following ugly code is only needed to catch deprecated accessors | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | @DEPRECATED = qw(pre_process process eval_perl interpolate post_chomp); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | my $module = "Plack::Middleware::TemplateToolkit"; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | foreach my $name (@DEPRECATED) { | |||||||||||||||||||||||||||||||||||||||||
| 40 | 35 | ▶ | *{ $module . "::$name" } = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | my $correct = uc($name); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | carp $module. "$name is deprecated, use ::$correct"; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | my $method = $module . "::$correct"; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | &$method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 35 | } | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 21 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 21 | my $self = Plack::Component::new(@_); | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | # Support 'root' config (matches MW::Static etc) | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # if INCLUDE_PATH hasn't been defined | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 21 | ▶ | $self->INCLUDE_PATH( $self->root ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->INCLUDE_PATH and $self->root
MC/DC0%0/2
not $self->INCLUDE_PATH and $self->root
not $self->INCLUDE_PATH
$self->root
Truth table66%2/3
not(A) and B
A= not $self->INCLUDE_PATHB= $self->root
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | if !$self->INCLUDE_PATH() && $self->root; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 21 | foreach ( grep { defined $self->{$_} } @DEPRECATED ) { | |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | $self->$_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 21 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 7 | ▶ | use Plack::Util::Accessor ( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | qw(dir_index path extension content_type default_type tt root timer | |||||||||||||||||||||||||||||||||||||||||
| 65 | pass_through decode_request encode_response vars request_vars), | ||||||||||||||||||||||||||||||||||||||||||
| 66 | @TT_CONFIG | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 35 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=16 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 35 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 35 | ▶ | $self->dir_index('index.html') unless $self->dir_index; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 35 | ▶ | $self->pass_through(0) unless defined $self->pass_through; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 35 | ▶ | $self->default_type('text/html') unless $self->default_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 35 | ▶ | $self->decode_request('utf8') unless defined $self->decode_request; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 35 | ▶ | $self->encode_response('utf8') unless defined $self->encode_response; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 35 | ▶ | $self->request_vars( [] ) unless defined $self->request_vars; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 35 | ▶ | if ( not $self->vars ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 17 | ▶ | $self->vars( sub { return { params => shift->query_parameters } } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | } elsif ( ref $self->vars eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | my $vars = $self->vars; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | $self->vars( sub { return $vars; } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } elsif ( ref $self->vars ne 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | die 'vars must be a code or hash reference, if defined'; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 35 | my $config = {}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 35 | foreach (@TT_CONFIG) { | |||||||||||||||||||||||||||||||||||||||||
| 90 | 1785 | ▶ | next unless $self->$_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 30 | $config->{$_} = $self->$_; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 30 | $self->$_(undef); # don't initialize twice | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 35 | ▶ | if ( $self->tt ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 17 | ▶ | die 'tt must be a Template instance' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | unless UNIVERSAL::isa( $self->tt, 'Template' ); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 17 | ▶ | die 'Either specify a template with tt or Template options, not both' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | if %$config; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 18 | ▶ | die 'No INCLUDE_PATH supplied' unless $config->{INCLUDE_PATH}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 18 | $self->tt( Template->new($config) ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 45 | ▶ | sub call { # adapted from Plack::Middleware::Static | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=7 SCAR=20.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 45 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 45 | ▶ | my $start = [ Time::HiRes::gettimeofday ] if $self->timer; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 45 | my $res = $self->_handle_template($env); | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 45 | ▶ | if ( !$res or ( $self->pass_through and $res->[0] == 404 ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $res or $self->pass_through and $res->[0] == 404
$self->pass_through and $res->[0] == 404
MC/DC100%3/3
not $res or $self->pass_through and $res->[0] == 404
not $res
$self->pass_through
$res->[0] == 404
Truth table100%4/4
not(A) or B and C
A= not $resB= $self->pass_throughC= $res->[0] == 404
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | if ( $self->app ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | # pass to the next middleware/app | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | $res = $self->app->($env); | |||||||||||||||||||||||||||||||||||||||||
| 116 | # if ( $self->catch_errors and $res->[0] =~ /^[45]/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # TODO: process error message (but better use callback) | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | $res = $self->process_error( 404, 'Not found', 'text/plain', $req ); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 45 | ▶ | if ($self->timer) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my $end = [ Time::HiRes::gettimeofday ]; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $env->{'tt.start'} = Plack::Middleware::Debug::Timer->format_time($start); | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $env->{'tt.end'} = Plack::Middleware::Debug::Timer->format_time($end); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $env->{'tt.elapsed'} | |||||||||||||||||||||||||||||||||||||||||
| 130 | = sprintf '%.6f s', Time::HiRes::tv_interval $start, $end; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 45 | $res; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 51 | ▶ | sub process_template { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_template: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 51 | my ( $self, $template, $code, $vars ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 51 | my ( $content, $res ); | |||||||||||||||||||||||||||||||||||||||||
| 140 | 51 | ▶ | if ( $self->tt->process( $template, $vars, \$content ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 38 | ▶ | my $type = $self->content_type || do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$self->content_type || do {
'Plack::MIME'->mime_type($1) if $template =~ /(\.\w{1,6})$/
} || $self->default_type
$self->content_type or do {
"Plack::MIME"->mime_type($1) if $template =~ /(\.\w{1,6})$/
}
MC/DC0%0/3
$self->content_type || do {
'Plack::MIME'->mime_type($1) if $template =~ /(\.\w{1,6})$/
} || $self->default_type
$self->content_type
do {
"Plack::MIME"->mime_type($1) if $template =~ /(\.\w{1,6})$/
}
$self->default_type
Truth table50%2/4
A or B || C
A= $self->content_typeB= do {
"Plack::MIME"->mime_type($1) if $template =~ /(\.\w{1,6})$/
}C= $self->default_type
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 38 | ▶ | Plack::MIME->mime_type($1) if $template =~ /(\.\w{1,6})$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | || $self->default_type; | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 38 | ▶ | if ( $self->encode_response ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 37 | $content = encode( $self->encode_response, $content ); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 38 | $res = [ $code, [ 'Content-Type' => $type ], [$content] ]; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 13 | $res = $self->tt->error->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 51 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 24 | ▶ | sub process_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_error: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 24 | my ( $self, $code, $error, $type, $req ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 24 | ▶ | $code = 500 unless $code && $code =~ /^\d\d\d$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$code and $code =~ /^\d\d\d$/
MC/DC50%1/2
$code and $code =~ /^\d\d\d$/
$code
$code =~ /^\d\d\d$/
Truth table66%2/3
A and B
A= $codeB= $code =~ /^\d\d\d$/
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 24 | ▶ | $error = status_message($code) unless $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 24 | ▶ | $type = ( $self->content_type || $self->default_type || 'text/plain' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$self->content_type || $self->default_type || 'text/plain'
$self->content_type or $self->default_type
MC/DC50%1/2
$self->content_type || $self->default_type || 'text/plain'
$self->content_type
$self->default_type
Truth table66%2/3
A or B
A= $self->content_typeB= $self->default_type
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | unless $type; | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | # plain error without template | ||||||||||||||||||||||||||||||||||||||||||
| 165 | return [ $code, [ 'Content-Type' => $type ], [$error] ] | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 24 | ▶ | unless $self->{$code} and $self->tt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{$code} and $self->tt
MC/DC100%2/2
$self->{$code} and $self->tt
$self->{$code}
$self->tt
Truth table100%3/3
A and B
A= $self->{$code}B= $self->tt
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 12 | ▶ | $req = Plack::Request->new( { 'tt.vars' => {} } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($req) and $req->isa("Plack::Request")
MC/DC50%1/2
builtin::blessed($req) and $req->isa("Plack::Request")
builtin::blessed($req)
$req->isa("Plack::Request")
Truth table66%2/3
A and B
A= builtin::blessed($req)B= $req->isa("Plack::Request")
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | unless blessed $req && $req->isa('Plack::Request'); | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 12 | eval { $self->_set_vars($req); }; | |||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 12 | $req->env->{'tt.vars'}->{'error'} = $error; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 12 | $req->env->{'tt.vars'}->{'path'} = $req->path_info; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 12 | $req->env->{'tt.vars'}->{'request'} = $req; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 12 | my $tpl = $self->{$code}; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 12 | my $res = $self->process_template( $tpl, $code, $req->env->{'tt.vars'} ); | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 12 | ▶ | unless ( ref $res ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | # processing error document failed: result in a 500 error | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 4 | ▶ | if ( $code eq 500 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 2 | $res = [ 500, [ 'Content-Type' => $type ], [$res] ]; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | $tpl = undef; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 2 | ▶ | if ( ref $req->logger ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 2 | $req->logger->( { level => 'warn', message => $res } ); | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 2 | ( $res, $tpl ) = $self->process_error( 500, $res, $type, $req ); | |||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 12 | ▶ | return wantarray ? ( $res, $tpl ) : $res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 53 | ▶ | sub _set_vars { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_vars: called CC=14 SCAR=26.5
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 53 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 53 | my $env = $req->env; | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | # we must not copy the vars by reference because | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # otherwise we might modify the same object | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 53 | ▶ | my (%vars) = %{ $self->vars->($req) } if defined $self->vars; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 51 | my $rv = $self->request_vars; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 51 | ▶ | unless ( exists $vars{request} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 50 | ▶ | if ( $rv eq 'all' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | $vars{request} = $req; | |||||||||||||||||||||||||||||||||||||||||
| 208 | ▶ | } elsif ( ref $rv and @$rv ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $rv and @$rv
MC/DC50%1/2
ref $rv and @$rv
ref $rv
@$rv
Truth table66%2/3
A and B
A= ref $rvB= @$rv
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 4 | $vars{request} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 4 | foreach ( @{ $self->request_vars } ) { | |||||||||||||||||||||||||||||||||||||||||
| 211 | 8 | ▶ | next unless $req->can($_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 6 | my $value = $req->$_; | |||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | # request vars should also be byte strings, so we must decode it | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 6 | ▶ | if ( $self->decode_request ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 6 | my $encoding = $self->decode_request; | |||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 6 | ▶ | if ( blessed($value) and $value->isa('Hash::MultiValue') ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($value) and $value->isa("Hash::MultiValue")
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Hash::MultiValue")
builtin::blessed($value)
$value->isa("Hash::MultiValue")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Hash::MultiValue")
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | { | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 4 | my @values = $value->values; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 4 | @values = map { decode( $encoding, $_ ) } @values; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 4 | my $hash = Hash::MultiValue->new; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 4 | foreach my $key ( $value->keys ) { | |||||||||||||||||||||||||||||||||||||||||
| 224 | 4 | $key = decode( $encoding, $key ); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 4 | $hash->add( $key, shift @values ); | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 4 | $value = $hash; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 2 | $value = decode( $encoding, $value ); | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | 6 | $vars{request}->{$_} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 51 | ▶ | if ( $env->{'tt.vars'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | # add to existing vars | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 14 | foreach ( keys %vars ) { | |||||||||||||||||||||||||||||||||||||||||
| 241 | 14 | $env->{'tt.vars'}->{$_} = $vars{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 37 | $env->{'tt.vars'} = \%vars; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | # core function called once in 'call' | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 45 | ▶ | sub _handle_template { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_template: called CC=17 SCAR=28.4
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 45 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | 45 | ▶ | if ( not $env->{'tt.template'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 43 | ▶ | my $path = $env->{'tt.path'} || do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$env->{'tt.path'} || do {
$env->{'tt.path'} = $env->{'PATH_INFO'} || '/'
}
MC/DC0%0/2
$env->{'tt.path'} || do {
$env->{'tt.path'} = $env->{'PATH_INFO'} || '/'
}
$env->{'tt.path'}
do {
$env->{'tt.path'} = $env->{'PATH_INFO'} || '/'
}
Truth table66%2/3
A || B
A= $env->{'tt.path'}B= do {
$env->{'tt.path'} = $env->{'PATH_INFO'} || '/'
}
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 42 | ▶ | $env->{'tt.path'} = $env->{PATH_INFO} || '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$env->{'PATH_INFO'} || '/'
MC/DC100%1/1
$env->{'PATH_INFO'} || '/'
$env->{'PATH_INFO'}
Truth table100%2/2
A
A= $env->{'PATH_INFO'}
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | 43 | ▶ | my $path_match = $self->path || '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->path || '/'
MC/DC100%1/1
$self->path || '/'
$self->path
Truth table100%2/2
A
A= $self->path
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 43 | for ($path) { | |||||||||||||||||||||||||||||||||||||||||
| 259 | 43 | ▶ | my $matched | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | = 'CODE' eq ref $path_match | ||||||||||||||||||||||||||||||||||||||||||
| 261 | ? $path_match->($_) | ||||||||||||||||||||||||||||||||||||||||||
| 262 | : $_ =~ $path_match; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 43 | ▶ | if (not $matched) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | delete $env->{'tt.path'}; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 40 | ▶ | $path .= $self->dir_index if $path =~ /\/$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 40 | $path =~ s{^/}{}; # Do not want to enable absolute paths | |||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 40 | my $extension = $self->extension; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 40 | ▶ | if ( $extension and $path !~ /${extension}$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$extension and not $path =~ /$extension$/
MC/DC100%2/2
$extension and not $path =~ /$extension$/
$extension
not $path =~ /$extension$/
Truth table100%3/3
A and not(B)
A= $extensionB= not $path =~ /$extension$/
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | # This 404 will be catched in method call if pass_through is set | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | my ($res, $tpl) = $self->process_error( | |||||||||||||||||||||||||||||||||||||||||
| 276 | 404, 'Not found', 'text/plain', Plack::Request->new($env) ); | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | $env->{'tt.template'} = $tpl; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 39 | $env->{'tt.template'} = $path; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 2 | delete $env->{'tt.path'}; | |||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 41 | my ($res, $tpl); | |||||||||||||||||||||||||||||||||||||||||
| 287 | 41 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 41 | eval { $self->_set_vars($req); }; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 41 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | my $error = "error setting template variables: $@"; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 2 | ▶ | my $type = $self->content_type || $self->default_type; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->content_type || $self->default_type
MC/DC0%0/2
$self->content_type || $self->default_type
$self->content_type
$self->default_type
Truth table33%1/3
A || B
A= $self->content_typeB= $self->default_type
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 2 | ( $res, $tpl ) = $self->process_error( 500, $error, $type, $req ); | |||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | $env->{'tt.template'} = $tpl; | |||||||||||||||||||||||||||||||||||||||||
| 294 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 295 | $res = $self->process_template( | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 39 | $env->{'tt.template'}, 200, $env->{'tt.vars'} ); | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 41 | ▶ | unless ( ref $res ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 9 | ▶ | my $type = $self->content_type || $self->default_type; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->content_type || $self->default_type
MC/DC0%0/2
$self->content_type || $self->default_type
$self->content_type
$self->default_type
Truth table33%1/3
A || B
A= $self->content_typeB= $self->default_type
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 9 | ▶ | if ( $res =~ /file error .+ not found/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 6 | ( $res, $tpl ) = $self->process_error( 404, $res, $type, $req ); | |||||||||||||||||||||||||||||||||||||||||
| 303 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 3 | ▶ | if ( ref $req->logger ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 3 | $req->logger->( { level => 'warn', message => $res } ); | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 3 | ( $res, $tpl ) = $self->process_error( 500, $res, $type, $req ); | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | 9 | $env->{'tt.template'} = $tpl; | |||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 41 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | __END__ | ||||||||||||||||||||||||||||||||||||||||||