| 1 | package YATT::Lite::Site; sub MY () {__PACKAGE__} | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 72 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 72 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 72 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 72 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # YATT::Lite::Site is the public facade API of YATT::Lite. | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # This class is inserted as a parent class of YATT::Lite::Factory, | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # so that every site object satisfies `$site isa YATT::Lite::Site` | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # (YATT::Lite::WebMVC0::SiteApp = Site + PSGI adapter). | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # This class must NOT declare any fields and must NOT have parent classes. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # Methods only. All instance fields it touches are defined in Factory | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # (or its subclasses), so keep `$self` untyped here. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 72 | ▶ | use YATT::Lite::Util qw(catch try_invoke); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 72 | ▶ | use YATT::Lite::PSGIEnv; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 72 | require File::Basename; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 72 | require Encode; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 72 | require YATT::Lite::Response; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # Class methods to open a site. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # Resolve which class implements the factory machinery. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # (Site is a *parent* of Factory, so Site itself cannot inherit them.) | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 61 | ▶ | sub _facade_factory_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_facade_factory_class: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 61 | my ($pack) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 61 | ▶ | if ($pack->can('find_load_factory_script')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 29 | $pack; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 32 | require YATT::Lite::Factory; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 32 | 'YATT::Lite::Factory'; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # Load the site object defined by app.psgi/runyatt.psgi, searching upward | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # from $opts{dir}. Dies when not found. Defaults to offline => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # (error_handler passes raw die through) since this entry is mainly for | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # programs and tools; the web entry point is app.psgi itself. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # Like Node's require.cache, one factory script yields one site object | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # per process: loading the same script twice would collide on app_ns | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # anyway, so subsequent load()s return the cached instance (with any | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # explicitly given opts re-configured onto it). | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 72 | our %SCRIPT_CACHE; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 23 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 23 | my ($pack, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 23 | my $class = $pack->_facade_factory_class; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 23 | ▶ | my $dir = delete $opts{dir} // '.'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts{"dir"} // "."
MC/DC0%0/1
delete $opts{"dir"} // "."
delete $opts{"dir"}
Truth table50%1/2
A
A= delete $opts{"dir"}
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 23 | my $stop_at = delete $opts{stop_at}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 23 | ▶ | my ($script) = $class->find_factory_script($dir, stop_at => $stop_at) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | or croak "Can't find factory script (app.psgi or runyatt.psgi)" | ||||||||||||||||||||||||||||||||||||||||||
| 60 | . " at or above: $dir" | ||||||||||||||||||||||||||||||||||||||||||
| 61 | . (defined $stop_at ? " (search stopped at: $stop_at)" : ""); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 21 | my $key = YATT::Lite::Util::normalize_fs_path($class->rel2abs($script)); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 21 | ▶ | if (my $site = $SCRIPT_CACHE{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 6 | ▶ | $site->configure(%opts) if %opts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 6 | return $site; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 15 | ▶ | $opts{offline} = 1 unless exists $opts{offline}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 15 | ▶ | my $site = $class->load_factory_script($key) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | or croak "Can't load YATT::Lite::Factory instance from $script"; | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 15 | $site->configure(%opts); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 15 | $SCRIPT_CACHE{$key} = $site; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # Like load, but falls back to a plain SiteApp when no factory script | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # is found. app_ns is auto-uniquified so that multiple default sites | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # can coexist in one process. The class of the synthesized site can be | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # overridden with class => 'My::SiteApp'. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 38 | ▶ | sub load_or_default { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_or_default: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 38 | my ($pack, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 38 | my $class = $pack->_facade_factory_class; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 38 | ▶ | my $app_class = delete $opts{class} // 'YATT::Lite::WebMVC0::SiteApp'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $opts{"class"} // "YATT::Lite::WebMVC0::SiteApp"
MC/DC100%1/1
delete $opts{"class"} // "YATT::Lite::WebMVC0::SiteApp"
delete $opts{"class"}
Truth table100%2/2
A
A= delete $opts{"class"}
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 38 | ▶ | if ($class->find_factory_script($opts{dir}, stop_at => $opts{stop_at})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 19 | return $pack->load(%opts); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 19 | my $dir = delete $opts{dir}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 19 | delete $opts{stop_at}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 19 | ▶ | $opts{offline} = 1 unless exists $opts{offline}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 19 | YATT::Lite::Util::ckrequire($app_class); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 19 | ▶ | $app_class->new | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | (app_ns => $pack->_uniq_default_app_ns | ||||||||||||||||||||||||||||||||||||||||||
| 91 | , (defined $dir ? (app_root => $dir, doc_root => $dir) : ()) | ||||||||||||||||||||||||||||||||||||||||||
| 92 | , header_charset => 'utf-8' | ||||||||||||||||||||||||||||||||||||||||||
| 93 | , tmpl_encoding => 'utf-8' | ||||||||||||||||||||||||||||||||||||||||||
| 94 | , output_encoding => 'utf-8' | ||||||||||||||||||||||||||||||||||||||||||
| 95 | , %opts); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 72 | our $DEFAULT_APP_NS_SEQ = 0; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 19 | ▶ | sub _uniq_default_app_ns { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_uniq_default_app_ns: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 19 | my ($pack) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 19 | my $ns; | |||||||||||||||||||||||||||||||||||||||||
| 102 | do { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 19 | my $seq = $DEFAULT_APP_NS_SEQ++; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 19 | ▶ | $ns = 'MyYATT' . ($seq ? $seq : ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 72 | ▶ | } while (do {no strict 'refs'; %{"${ns}::"}}); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 19 | $ns; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # resolve_file: filename => (dirhandler, template_name [, location]) | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # Files under doc_root are resolved via location (exactly like the web | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # does), others fall back to the physical dirhandler. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 27 | ▶ | sub resolve_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve_file: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 27 | my ($self, $fn) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 27 | ▶ | defined $fn and $fn ne '' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $fn and $fn ne ""
MC/DC0%0/2
defined $fn and $fn ne ""
defined $fn
$fn ne ""
Truth table33%1/3
A and B
A= defined $fnB= $fn ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | or croak "resolve_file: filename is missing!"; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 27 | my $abs = YATT::Lite::Util::normalize_fs_path($self->rel2abs($fn)); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 27 | my $dir = File::Basename::dirname($abs); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 27 | my $name = File::Basename::basename($abs); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 27 | my $loc = $self->_loc_under_doc_root($dir); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 27 | ▶ | my $dh = defined $loc | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | ? $self->get_lochandler($loc) | ||||||||||||||||||||||||||||||||||||||||||
| 126 | : $self->get_dirhandler($dir); | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 27 | ▶ | wantarray ? ($dh, $name, $loc) : $dh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 27 | ▶ | sub _loc_under_doc_root { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_loc_under_doc_root: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 27 | my ($self, $dir) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 132 | my $root = $self->{doc_root} | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 27 | ▶ | or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 27 | $root = YATT::Lite::Util::normalize_fs_path($self->rel2abs($root)); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 27 | $root =~ s{/+\z}{}; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 27 | ▶ | if ($dir eq $root) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 20 | '/'; | |||||||||||||||||||||||||||||||||||||||||
| 138 | } elsif (index($dir, "$root/") == 0) { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 5 | substr($dir, length($root)); | |||||||||||||||||||||||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # request($method, $path, $args, %opts) | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # Synthesizes a PSGI env and runs it through the real call($env), | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # so the result is identical to a real web request | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # (path resolution, sigil, public check, SIG traps, error page and all). | ||||||||||||||||||||||||||||||||||||||||||
| 151 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 7 | ▶ | sub request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 7 | my ($self, $method, $path, $args, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 7 | ▶ | my $call = $self->can('call') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | or croak "request() requires a web-capable site (SiteApp), not: " | ||||||||||||||||||||||||||||||||||||||||||
| 157 | . ref($self); | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 7 | my Env $env = $self->psgi_env_for($method, $path, $args, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 7 | ▶ | $self->prepare_app unless $self->{is_psgi}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | YATT::Lite::Response->from_psgi($call->($self, $env)); | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 26 | ▶ | sub psgi_env_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
psgi_env_for: called CC=15 SCAR=29.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 26 | my ($self, $method, $path, $args, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 26 | ▶ | $method = uc($method // 'GET'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$method // "GET"
MC/DC0%0/1
$method // "GET"
$method
Truth table50%1/2
A
A= $method
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 26 | ▶ | defined $path and $path =~ m{^/} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
defined $path and $path =~ m[^/]
$path // "(undef)"
MC/DC0%0/2
defined $path and $path =~ m[^/]
defined $path
$path =~ m[^/]
MC/DC0%0/1
$path // "(undef)"
$path
Truth table33%1/3
A and B
A= defined $pathB= $path =~ m[^/]
Truth table0%0/2
A
A= $path
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | or croak "request path must start with '/': " . ($path // '(undef)'); | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 26 | my ($path_info, $query) = $path =~ m{^([^?]*) (?:\?(.*))?\z}x; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 26 | my Env $env = Env->psgi_simple_env(PATH_INFO => $path_info); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 26 | $env->{REQUEST_METHOD} = $method; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 26 | $env->{SCRIPT_NAME} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 26 | $env->{SERVER_NAME} = 'localhost'; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 26 | $env->{SERVER_PORT} = 80; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 26 | $env->{SERVER_PROTOCOL} = 'HTTP/1.1'; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 26 | $env->{HTTP_HOST} = 'localhost'; | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 26 | my $encoded = $self->encode_query($args); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 26 | ▶ | if ($method eq 'POST' or $method eq 'PUT') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$method eq "POST" or $method eq "PUT"
MC/DC50%1/2
$method eq "POST" or $method eq "PUT"
$method eq "POST"
$method eq "PUT"
Truth table66%2/3
A or B
A= $method eq "POST"B= $method eq "PUT"
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | $env->{QUERY_STRING} = $query // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$query // ""
MC/DC0%0/1
$query // ""
$query
Truth table50%1/2
A
A= $query
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | my $body = $encoded // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$encoded // ""
MC/DC0%0/1
$encoded // ""
$encoded
Truth table50%1/2
A
A= $encoded
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | open my $input, '<', \$body or die "Can't open in-memory body: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | $env->{'psgi.input'} = $input; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | $env->{CONTENT_TYPE} = 'application/x-www-form-urlencoded'; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | $env->{CONTENT_LENGTH} = length($body); | |||||||||||||||||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 25 | ▶ | $env->{QUERY_STRING} = join '&', grep {defined $_ and $_ ne ''} | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $_ && $_ ne ''
MC/DC50%1/2
defined $_ && $_ ne ''
defined $_
$_ ne ''
Truth table66%2/3
A && B
A= defined $_B= $_ ne ''
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | ($query, $encoded); | ||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | $env->{REQUEST_URI} = $path_info | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 26 | ▶ | . (($env->{QUERY_STRING} // '') ne '' ? "?$env->{QUERY_STRING}" : ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$env->{"QUERY_STRING"} // ""
MC/DC0%0/1
$env->{"QUERY_STRING"} // ""
$env->{"QUERY_STRING"}
Truth table50%1/2
A
A= $env->{"QUERY_STRING"}
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 26 | ▶ | if (my $override = $opts{env}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | $env->{$_} = $override->{$_} for keys %$override; | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 26 | $env; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | # $args: HASH (value may be ARRAY for multi-value), ARRAY of pairs, | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # or a raw query string. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 26 | ▶ | sub encode_query { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_query: called CC=9 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 26 | my ($self, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 26 | ▶ | return undef unless defined $args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 4 | ▶ | return $args unless ref $args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 4 | my @pairs; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 4 | ▶ | if (ref $args eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 4 | foreach my $key (sort keys %$args) { | |||||||||||||||||||||||||||||||||||||||||
| 208 | 4 | my $value = $args->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 209 | push @pairs, _url_encode($key) . '=' . _url_encode($_) | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 4 | ▶ | for ref $value eq 'ARRAY' ? @$value : $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | } elsif (ref $args eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | my @list = @$args; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | while (my ($key, $value) = splice @list, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | push @pairs, _url_encode($key) . '=' . _url_encode($value); | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | croak "Unsupported args type for query: " . ref $args; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 4 | join '&', @pairs; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 8 | ▶ | sub _url_encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_url_encode: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 8 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 8 | ▶ | $str //= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$str //= ""
MC/DC0%0/1
$str //= ""
$str
Truth table50%1/2
A
A= $str
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 8 | ▶ | $str = Encode::encode_utf8($str) if utf8::is_utf8($str); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 8 | $str =~ s{([^0-9A-Za-z_.~-])}{sprintf("%%%02X", ord $1)}eg; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 8 | $str; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # render_file($fn, $args, %opts) | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # Dispatch (part resolution, sigil, public check, argument reordering) | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # is identical to the web, but the error policy defaults to tool style: | ||||||||||||||||||||||||||||||||||||||||||
| 236 | # no error page conversion, raw die passes through (perl -d friendly). | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # Pass error_style => 'web' for web-equivalent error handling. | ||||||||||||||||||||||||||||||||||||||||||
| 238 | #======================================== | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 19 | ▶ | sub render_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_file: called CC=14 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 19 | my ($self, $fn, $args, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 19 | ▶ | my $error_style = delete $opts{error_style} // 'raw'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $opts{"error_style"} // "raw"
MC/DC100%1/1
delete $opts{"error_style"} // "raw"
delete $opts{"error_style"}
Truth table100%2/2
A
A= delete $opts{"error_style"}
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 19 | ▶ | $error_style =~ /^(?:raw|web)\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | or croak "Unknown error_style: $error_style (raw or web)"; | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 19 | my ($dh, $name, $loc) = $self->resolve_file($fn); | |||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 19 | my Env $env = $self->psgi_env_for(GET => $self->_path_info_for($name, $loc)); | |||||||||||||||||||||||||||||||||||||||||
| 249 | 19 | try_invoke($self, 'set_yatt_script_name', $env); | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 19 | require Hash::MultiValue; | |||||||||||||||||||||||||||||||||||||||||
| 252 | my $params = Hash::MultiValue->new | ||||||||||||||||||||||||||||||||||||||||||
| 253 | (map { | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 19 | my $key = $_; my $value = $args->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | ref $value eq 'ARRAY' ? (map {($key => $_)} @$value) : ($key => $value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | } $args ? keys %$args : ()); | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | my $con = $self->make_connection | ||||||||||||||||||||||||||||||||||||||||||
| 259 | (undef | ||||||||||||||||||||||||||||||||||||||||||
| 260 | , env => $env | ||||||||||||||||||||||||||||||||||||||||||
| 261 | , path_info => $env->{PATH_INFO} | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 19 | , parameters => $params | |||||||||||||||||||||||||||||||||||||||||
| 263 | , file => $name | ||||||||||||||||||||||||||||||||||||||||||
| 264 | , noheader => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 265 | , yatt => $dh); | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 19 | my $dh_fields = YATT::Lite::Util::fields_hash($dh); | |||||||||||||||||||||||||||||||||||||||||
| 268 | 19 | ▶ | my $error = catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 19 | ▶ | if ($error_style eq 'web' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$error_style eq "web" or not $dh_fields && $dh_fields->{"_ignore_die"}
$dh_fields && $dh_fields->{'_ignore_die'}
MC/DC33%1/3
$error_style eq "web" or not $dh_fields && $dh_fields->{"_ignore_die"}
$error_style eq "web"
$dh_fields
$dh_fields->{'_ignore_die'}
Truth table50%2/4
A or not(B && C)
A= $error_style eq "web"B= $dh_fieldsC= $dh_fields->{'_ignore_die'}
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | or not ($dh_fields and $dh_fields->{_ignore_die})) { | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 1 | $self->run_dirhandler($dh, $con, $name); | |||||||||||||||||||||||||||||||||||||||||
| 272 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 273 | # Tool style: neutralize the __DIE__/__WARN__ traps installed by | ||||||||||||||||||||||||||||||||||||||||||
| 274 | # DirApp::handle, so plain die reaches the caller (and perl -d) as is. | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 18 | local $dh->{_ignore_die} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 18 | local $dh->{_ignore_warn} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 18 | $self->run_dirhandler($dh, $con, $name); | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 19 | }; | |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 19 | try_invoke($con, 'flush_headers'); | |||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 19 | ▶ | if ($error_style eq 'web' and $con->can('is_error') and $con->is_error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$error_style eq "web" and $con->can("is_error") and $con->is_error
$error_style eq "web" and $con->can("is_error")
MC/DC0%0/3
$error_style eq "web" and $con->can("is_error") and $con->is_error
$error_style eq "web"
$con->can("is_error")
$con->is_error
Truth table50%2/4
A and B and C
A= $error_style eq "web"B= $con->can("is_error")C= $con->is_error
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | return $self->_error_to_response($con->error_list->[0], $env, $con); | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 19 | ▶ | if (not $error or YATT::Lite::Util::is_done($error)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
not $error or YATT::Lite::Util::is_done($error)
MC/DC50%1/2
not $error or YATT::Lite::Util::is_done($error)
not $error
YATT::Lite::Util::is_done($error)
Truth table66%2/3
not(A) or B
A= not $errorB= YATT::Lite::Util::is_done($error)
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 13 | $self->_response_of_connection($con); | |||||||||||||||||||||||||||||||||||||||||
| 289 | ▶ | } elsif (ref $error eq 'ARRAY' or ref $error eq 'CODE') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $error eq "ARRAY" or ref $error eq "CODE"
MC/DC50%1/2
ref $error eq "ARRAY" or ref $error eq "CODE"
ref $error eq "ARRAY"
ref $error eq "CODE"
Truth table66%2/3
A or B
A= ref $error eq "ARRAY"B= ref $error eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | YATT::Lite::Response->from_psgi($error); | |||||||||||||||||||||||||||||||||||||||||
| 291 | ▶ | } elsif ($error_style eq 'web' | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$error_style eq "web" and UNIVERSAL::isa($error, "YATT::Lite::Error")
MC/DC50%1/2
$error_style eq "web" and UNIVERSAL::isa($error, "YATT::Lite::Error")
$error_style eq "web"
UNIVERSAL::isa($error, "YATT::Lite::Error")
Truth table66%2/3
A and B
A= $error_style eq "web"B= UNIVERSAL::isa($error, "YATT::Lite::Error")
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | and UNIVERSAL::isa($error, 'YATT::Lite::Error')) { | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | $self->_error_to_response($error, $env, $con); | |||||||||||||||||||||||||||||||||||||||||
| 294 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 3 | die $error; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 19 | ▶ | sub _path_info_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_path_info_for: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 19 | my ($self, $name, $loc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 19 | my $req_name = $name; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 19 | ▶ | if (defined(my $ext = $self->{ext_public})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 19 | $req_name =~ s/\.\Q$ext\E\z//; | |||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 19 | ▶ | my $path_info = $loc // '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$loc // "/"
MC/DC100%1/1
$loc // "/"
$loc
Truth table100%2/2
A
A= $loc
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 19 | ▶ | $path_info .= '/' unless $path_info =~ m{/\z}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 19 | $path_info . $req_name; | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | 13 | ▶ | sub _response_of_connection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_response_of_connection: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 13 | my ($self, $con) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 13 | ▶ | my @headers = $con->can('list_header') ? $con->list_header : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 13 | ▶ | YATT::Lite::Response->new | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
try_invoke($con, ["cget", "status"]) // 200
MC/DC0%0/1
try_invoke($con, ["cget", "status"]) // 200
try_invoke($con, ["cget", "status"])
Truth table50%1/2
A
A= try_invoke($con, ["cget", "status"])
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | (status => (try_invoke($con, [cget => 'status']) // 200) | ||||||||||||||||||||||||||||||||||||||||||
| 315 | , headers => \@headers | ||||||||||||||||||||||||||||||||||||||||||
| 316 | , body => [$con->buffer]); | ||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 1 | ▶ | sub _error_to_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_error_to_response: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | my ($self, $err, $env, $con) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | ▶ | if ($self->can('error_response')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | YATT::Lite::Response->from_psgi($self->error_response($err, $env, $con)); | |||||||||||||||||||||||||||||||||||||||||
| 323 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 72 | 1; | |||||||||||||||||||||||||||||||||||||||||