blib/lib/Zuzu/Web/PSGI.pm

filter: Statementstmt 96.5% 222 / 230 Branchbran 77.8% 81 / 104 Conditioncond 70.4% 74 / 105 MC/DCmcdc 53.3% 32 / 60 Subroutinesub 100% 41 / 41 Podpod 0.0% 0 / 1 totaltotal 83.1% 450 / 541 CC 96 SCAR 19.6
CC
96
Cov
86%
_request_env_from_psgi
18.5
_read_psgi_body
13.8
_render_thrown_value
13.4
CRAP 7.1
« blib/lib/Zuzu/Value/Equality.pm ↑ Summary blib/lib/Zuzu/Runtime/Async/TaskGroup.pm »
1 package Zuzu::Web::PSGI;
2  
3 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 7 our $VERSION = '0.007002';
8  
9 7 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Zuzu::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Zuzu::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Zuzu::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Zuzu::Value::BinaryString;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Zuzu::Value::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Zuzu::Value::PairList;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 21 ▶sub app {
Subroutine app: called CC=6 SCAR=17.9
Pod undocumented
20 21 my ( $class, @args ) = @_;
21 21 ▶ die "Zuzu::Web::PSGI->app expects named arguments\n" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 21 21
22  
23 21 my %args = @args;
24 21 my $script = $args{script};
25 21 ▶ die "Zuzu::Web::PSGI->app requires a script argument\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $script or $script eq "" 1 20 21
Condition66%2/3
not defined $script or $script eq ""
l!l&&r!l&&!r
1020
MC/DC50%1/2
not defined $script or $script eq ""
not defined $script $script eq ""
Truth table66%2/3
not(A) or B
A= not defined $scriptB= $script eq ""
ABresult
1X1
011
000
26 if !defined $script or $script eq '';
27  
28 20 ▶ if ( exists $args{debug_level} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"debug_level"}) 3 17 20
29 3 $Zuzu::Runtime::DEBUG_LEVEL = $args{debug_level};
30 3 $ENV{ZUZU_DEBUG_LEVEL} = $args{debug_level};
31 }
32  
33 20 my $source = _slurp_utf8($script);
34 20 my $runtime = _build_runtime(%args);
35 20 my $ast = Zuzu::Parser->new->parse( $source, $script );
36 20 $runtime->evaluate($ast);
37  
38 20 ▶ die "ZuzuScript PSGI application '$script' does not define __request__\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $runtime->has_function("__request__") 2 18 20
39 if !$runtime->has_function('__request__');
40  
41 22 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
42 22 my ( $env ) = @_;
43 22 my $response;
44 22 my $ok = eval {
45 22 my $request_env = _request_env_from_psgi($env);
46 21 my $zuzu_response = $runtime->call( '__request__', $request_env );
47 18 $response = _response_to_psgi($zuzu_response);
48 15 1;
49 };
50 22 ▶ return $response if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 15 7 22
51 7 _handle_request_error( $runtime, $env, $@ );
52 7 return _server_error_response();
53 18 };
54 }
55  
56 7 ▶sub _handle_request_error {
Subroutine _handle_request_error: called CC=1 SCAR=0.0
57 7 my ( $runtime, $psgi_env, $error ) = @_;
58  
59 7 _log_error( $psgi_env, _render_error( $runtime, $error ) );
60  
61 7 return;
62 }
63  
64 7 ▶sub _log_error {
Subroutine _log_error: called CC=5 SCAR=16.2
65 7 my ( $psgi_env, $message ) = @_;
66  
67 7 ▶ $message //= '';
Condition50%1/2
$message //= ""
l!l
70
MC/DC0%0/1
$message //= ""
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
68 7 $message =~ s/\s+\z//;
69 7 my $line = "Zuzu PSGI error: $message\n";
70 7 ▶ my $errors = $psgi_env ? $psgi_env->{'psgi.errors'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$psgi_env ? : 7 0 7
71  
72 7 ▶ if ( defined $errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $errors) 6 1 7
73 6 my $ok = eval {
74 6 print {$errors} $line;
75 6 1;
76 };
77 6 ▶ return if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 6 0 6
78 }
79  
80 1 eval {
81 1 print STDERR $line;
82 1 1;
83 };
84  
85 1 return;
86 }
87  
88 7 ▶sub _render_error {
Subroutine _render_error: called CC=3 SCAR=11.0
89 7 my ( $runtime, $error ) = @_;
90  
91 7 ▶ if ( ref($error) eq 'HASH' and $error->{_zuzu_throw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error eq "HASH" and $error->{"_zuzu_throw"}) 3 4 7
Condition66%2/3
ref $error eq "HASH" and $error->{"_zuzu_throw"}
!ll&&!rl&&r
403
MC/DC50%1/2
ref $error eq "HASH" and $error->{"_zuzu_throw"}
ref $error eq "HASH" $error->{"_zuzu_throw"}
Truth table66%2/3
A and B
A= ref $error eq "HASH"B= $error->{"_zuzu_throw"}
ABresult
0X0
100
111
92 3 return _render_thrown_value( $runtime, $error->{value} );
93 }
94  
95 4 return "$error";
96 }
97  
98 3 ▶sub _render_thrown_value {
Subroutine _render_thrown_value: called CC=9 SCAR=26.0
99 3 my ( $runtime, $value ) = @_;
100  
101 3 my $text = eval { $runtime->_to_String($value) };
102 3 ▶ $text = defined $value ? "$value" : '' if !defined $text or $@ ne '';
Branch: 2 branches
BranchTrueFalseTotal
if not defined $text or $@ ne "" 0 3 3
defined $value ? : 0 0 0
Condition33%1/3
not defined $text or $@ ne ""
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $text or $@ ne ""
not defined $text $@ ne ""
Truth table33%1/3
not(A) or B
A= not defined $textB= $@ ne ""
ABresult
1X1
011
000
103 3 ▶ return $text if !blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 0 3 3
104 3 ▶ return $text if !$value->isa('Zuzu::Value::Object');
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa("Zuzu::Value::Object") 0 3 3
105  
106 3 ▶ my $slots = $value->slots // {};
Condition50%1/2
$value->slots // {}
l!l
30
MC/DC0%0/1
$value->slots // {}
$value->slots
Truth table50%1/2
A
A= $value->slots
Aresult
11
00
107 3 my $file = $slots->{file};
108 3 my $line = $slots->{line};
109 3 ▶ return $text if !defined $file or !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $line 0 3 3
Condition33%1/3
defined $file and defined $line
!ll&&!rl&&r
003
MC/DC0%0/2
defined $file and defined $line
defined $file defined $line
Truth table33%1/3
A and B
A= defined $fileB= defined $line
ABresult
0X0
100
111
110  
111 3 return "$text at $file, line $line";
112 }
113  
114 27 ▶sub _response_to_psgi {
Subroutine _response_to_psgi: called CC=4 SCAR=13.9
115 27 my ( $value ) = @_;
116  
117 27 ▶ die "Zuzu PSGI response must be an Array\n"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) and $value->isa("Zuzu::Value::Array") 1 26 27
Condition66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
!ll&&!rl&&r
1026
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
builtin::blessed($value) $value->isa("Zuzu::Value::Array")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
118 if !blessed($value) or !$value->isa('Zuzu::Value::Array');
119  
120 26 my @items = $value->resolved_items;
121 26 ▶ die "Zuzu PSGI response Array must contain status, headers, and body\n"
Branch: 1 branch
BranchTrueFalseTotal
if @items != 3 1 25 26
122 if @items != 3;
123  
124 25 my $status = _status_to_psgi( $items[0] );
125 22 my $headers = _headers_to_psgi( $items[1] );
126  
127 21 return _response_parts_to_psgi( $status, $headers, $items[2] );
128 }
129  
130 21 ▶sub _response_parts_to_psgi {
Subroutine _response_parts_to_psgi: called CC=2 SCAR=6.9
131 21 my ( $status, $headers, $body ) = @_;
132  
133 21 ▶ if ( _is_path_body($body) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_path_body($body)) 6 15 21
134 6 return _path_response_to_psgi( $status, $headers, $body );
135 }
136  
137 15 return [ $status, $headers, _body_to_psgi($body) ];
138 }
139  
140 25 ▶sub _status_to_psgi {
Subroutine _status_to_psgi: called CC=6 SCAR=19.9
141 25 my ( $value ) = @_;
142  
143 25 ▶ die "Zuzu PSGI response status must be an integer\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or builtin::blessed($value) or not $value =~ /\A[0-9]+\z/ 0 25 25
Condition33%2/6
not defined $value or builtin::blessed($value) or not $value =~ /\A[0-9]+\z/
l!l&&r!l&&!r
0025
not defined $value or builtin::blessed($value)
l!l&&r!l&&!r
0025
MC/DC0%0/3
not defined $value or builtin::blessed($value) or not $value =~ /\A[0-9]+\z/
not defined $value builtin::blessed($value) not $value =~ /\A[0-9]+\z/
Truth table25%1/4
not(A) or B or not(C)
A= not defined $valueB= builtin::blessed($value)C= not $value =~ /\A[0-9]+\z/
ABCresult
1XX1
01X1
0011
0000
144 if !defined $value
145 or blessed($value)
146 or $value !~ /\A[0-9]+\z/;
147  
148 25 my $status = 0 + $value;
149 25 ▶ die "Zuzu PSGI response status must be in the range 100..599\n"
Branch: 1 branch
BranchTrueFalseTotal
if $status < 100 or $status > 599 3 22 25
Condition66%2/3
$status < 100 or $status > 599
l!l&&r!l&&!r
3022
MC/DC50%1/2
$status < 100 or $status > 599
$status < 100 $status > 599
Truth table66%2/3
A or B
A= $status < 100B= $status > 599
ABresult
1X1
011
000
150 if $status < 100 or $status > 599;
151  
152 22 return $status;
153 }
154  
155 22 ▶sub _headers_to_psgi {
Subroutine _headers_to_psgi: called CC=7 SCAR=19.5
156 22 my ( $value ) = @_;
157  
158 22 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::PairList') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::PairList")) 14 8 22
Condition66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::PairList")
!ll&&!rl&&r
0814
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Zuzu::Value::PairList")
builtin::blessed($value) $value->isa("Zuzu::Value::PairList")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::PairList")
ABresult
0X0
100
111
159 14 my @headers;
160 14 ▶ for ( my $i = 0; $i < @{ $value->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$value->list;}) 7 14 21
161 7 push @headers, _header_pair_to_psgi(
162 $value->list->[$i][0],
163 $value->_value_at($i),
164 );
165 }
166 14 return \@headers;
167 }
168  
169 8 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Dict') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Dict")) 7 1 8
Condition66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Dict")
!ll&&!rl&&r
017
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Dict")
builtin::blessed($value) $value->isa("Zuzu::Value::Dict")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Dict")
ABresult
0X0
100
111
170 7 my @headers;
171 7 for my $name ( sort keys %{ $value->map } ) {
172 12 push @headers, _header_pair_to_psgi( $name, $value->get($name) );
173 }
174 7 return \@headers;
175 }
176  
177 1 die "Zuzu PSGI response headers must be a PairList or Dict\n";
178 }
179  
180 19 ▶sub _header_pair_to_psgi {
Subroutine _header_pair_to_psgi: called CC=5 SCAR=20.9
181 19 my ( $name, $value ) = @_;
182  
183 19 ▶ die "Zuzu PSGI response header names and values must be scalars\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name && defined $value or builtin::blessed($name) or builtin::blessed($value) 0 19 19
Condition33%3/9
not defined $name && defined $value or builtin::blessed($name) or builtin::blessed($value)
l!l&&r!l&&!r
0019
not defined $name && defined $value or builtin::blessed($name)
l!l&&r!l&&!r
0019
defined $name && defined $value
!ll&&!rl&&r
0019
MC/DC0%0/4
not defined $name && defined $value or builtin::blessed($name) or builtin::blessed($value)
defined $name defined $value builtin::blessed($name) builtin::blessed($value)
Truth table20%1/5
not(A && B) or C or D
A= defined $nameB= defined $valueC= builtin::blessed($name)D= builtin::blessed($value)
ABCDresult
0XXX1
10XX1
111X1
11011
11000
184 if !defined $name
185 or !defined $value
186 or blessed($name)
187 or blessed($value);
188  
189 19 return ( "$name", "$value" );
190 }
191  
192 15 ▶sub _body_to_psgi {
Subroutine _body_to_psgi: called CC=4 SCAR=13.9
193 15 my ( $value ) = @_;
194  
195 15 ▶ return [] if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 14 15
196  
197 14 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Array') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Array")) 11 3 14
Condition100%3/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
!ll&&!rl&&r
1211
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
builtin::blessed($value) $value->isa("Zuzu::Value::Array")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
198 11 return [ map { _body_chunk_to_bytes($_) } $value->resolved_items ];
199 }
200  
201 3 return [ _body_chunk_to_bytes($value) ];
202 }
203  
204 21 ▶sub _is_path_body {
Subroutine _is_path_body: called CC=4 SCAR=13.9
205 21 my ( $value ) = @_;
206  
207 return (
208 blessed($value)
209 and $value->isa('Zuzu::Value::Object')
210 and exists $value->slots->{_path_tiny}
211 21 ▶ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('Zuzu::Value::Object') && exists $value->slots->{'_path_tiny'} ? : 6 15 21
Condition83%5/6
builtin::blessed($value) and $value->isa("Zuzu::Value::Object") and exists $value->slots->{"_path_tiny"}
!ll&&!rl&&r
1506
builtin::blessed($value) and $value->isa("Zuzu::Value::Object")
!ll&&!rl&&r
2136
MC/DC66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Object") and exists $value->slots->{"_path_tiny"}
builtin::blessed($value) $value->isa("Zuzu::Value::Object") exists $value->slots->{"_path_tiny"}
Truth table75%3/4
A and B and C
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Object")C= exists $value->slots->{"_path_tiny"}
ABCresult
0XX0
10X0
1100
1111
212 }
213  
214 6 ▶sub _path_response_to_psgi {
Subroutine _path_response_to_psgi: called CC=4 SCAR=13.9
215 6 my ( $status, $headers, $body ) = @_;
216 6 my $path = $body->slots->{_path_tiny};
217  
218 6 ▶ if ( !$path->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path->exists) 1 5 6
219 1 return _plain_response( 404, "Not Found\n" );
220 }
221 5 ▶ if ( $path->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->is_dir) 1 4 5
222 1 return _plain_response( 403, "Forbidden\n" );
223 }
224  
225 4 ▶ open my $fh, '<:raw', "$path"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", "$path" or die "Could not open static response path '${path}': $!\n" 4 0 4
226 or die "Could not open static response path '$path': $!\n";
227  
228 4 my @headers = @$headers;
229 4 _add_inferred_content_type( \@headers, $path );
230  
231 4 return [ $status, \@headers, $fh ];
232 }
233  
234 4 ▶sub _add_inferred_content_type {
Subroutine _add_inferred_content_type: called CC=3 SCAR=11.0
235 4 my ( $headers, $path ) = @_;
236  
237 4 ▶ return if _headers_include_content_type($headers);
Branch: 1 branch
BranchTrueFalseTotal
if _headers_include_content_type($headers) 1 3 4
238  
239 3 ▶ my $content_type = Plack::MIME->mime_type("$path")
Condition100%2/2
"Plack::MIME"->mime_type("$path") // "application/octet-stream"
l!l
21
MC/DC100%1/1
"Plack::MIME"->mime_type("$path") // "application/octet-stream"
"Plack::MIME"->mime_type("$path")
Truth table100%2/2
A
A= "Plack::MIME"->mime_type("$path")
Aresult
11
00
240 // 'application/octet-stream';
241 3 push @$headers, 'Content-Type', $content_type;
242  
243 3 return;
244 }
245  
246 4 ▶sub _headers_include_content_type {
Subroutine _headers_include_content_type: called CC=3 SCAR=11.0
247 4 my ( $headers ) = @_;
248  
249 4 ▶ for ( my $i = 0; $i < @$headers; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$headers) 1 3 4
250 1 ▶ return 1 if lc( $headers->[$i] ) eq 'content-type';
Branch: 1 branch
BranchTrueFalseTotal
if lc $headers->[$i] eq "content-type" 1 0 1
251 }
252  
253 3 return 0;
254 }
255  
256 34 ▶sub _body_chunk_to_bytes {
Subroutine _body_chunk_to_bytes: called CC=5 SCAR=16.1
257 34 my ( $value ) = @_;
258  
259 34 ▶ die "Zuzu PSGI response body chunks may not be null\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 34 34
260 if !defined $value;
261  
262 34 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::BinaryString') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")) 2 32 34
Condition100%3/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
3022
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
263 2 return $value->bytes;
264 }
265  
266 32 ▶ die "Unsupported Zuzu PSGI response body value\n" if blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) 2 30 32
267  
268 30 return Encode::encode( 'UTF-8', "$value", Encode::FB_CROAK );
269 }
270  
271 7 ▶sub _server_error_response {
Subroutine _server_error_response: called CC=1 SCAR=0.0
272 7 return _plain_response( 500, "Internal Server Error\n" );
273 }
274  
275 9 ▶sub _plain_response {
Subroutine _plain_response: called CC=1 SCAR=0.0
276 9 my ( $status, $body ) = @_;
277  
278 return [
279 9 $status,
280 [ 'Content-Type' => 'text/plain; charset=UTF-8' ],
281 [ $body ],
282 ];
283 }
284  
285 26 ▶sub _request_env_from_psgi {
Subroutine _request_env_from_psgi: called CC=18 SCAR=29.2
286 26 my ( $psgi_env ) = @_;
287 26 ▶ $psgi_env //= {};
Condition50%1/2
$psgi_env //= {}
l!l
260
MC/DC0%0/1
$psgi_env //= {}
$psgi_env
Truth table50%1/2
A
A= $psgi_env
Aresult
11
00
288  
289 26 my $body = _read_psgi_body($psgi_env);
290 24 ▶ my $request_uri = $psgi_env->{REQUEST_URI} // $psgi_env->{RAW_URI};
Condition66%2/3
$psgi_env->{"REQUEST_URI"} // $psgi_env->{"RAW_URI"}
l!l&&r!l&&!r
13011
MC/DC50%1/2
$psgi_env->{"REQUEST_URI"} // $psgi_env->{"RAW_URI"}
$psgi_env->{"REQUEST_URI"} $psgi_env->{"RAW_URI"}
Truth table66%2/3
A // B
A= $psgi_env->{"REQUEST_URI"}B= $psgi_env->{"RAW_URI"}
ABresult
1X1
011
000
291 my $raw_path = defined $request_uri
292 ? $request_uri =~ s/\?.*\z//r
293 24 ▶ : ( ( $psgi_env->{SCRIPT_NAME} // '' ) . ( $psgi_env->{PATH_INFO} // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $request_uri ? : 13 11 24
Condition100%4/4
$psgi_env->{"SCRIPT_NAME"} // ""
l!l
110
$psgi_env->{"PATH_INFO"} // ""
l!l
38
MC/DC100%1/1
$psgi_env->{"SCRIPT_NAME"} // ""
$psgi_env->{"SCRIPT_NAME"}
MC/DC100%1/1
$psgi_env->{"PATH_INFO"} // ""
$psgi_env->{"PATH_INFO"}
Truth table100%2/2
A
A= $psgi_env->{"SCRIPT_NAME"}
Aresult
11
00
Truth table100%2/2
A
A= $psgi_env->{"PATH_INFO"}
Aresult
11
00
294 my $full_request_uri = defined $request_uri
295 ? $request_uri
296 : $raw_path
297 . (
298 24 ▶ defined $psgi_env->{QUERY_STRING} && $psgi_env->{QUERY_STRING} ne ''
Branch: 2 branches
BranchTrueFalseTotal
defined $request_uri ? : 13 11 24
defined $psgi_env->{'QUERY_STRING'} && $psgi_env->{'QUERY_STRING'} ne '' ? : 0 11 11
Condition33%1/3
defined $psgi_env->{"QUERY_STRING"} and $psgi_env->{"QUERY_STRING"} ne ""
!ll&&!rl&&r
1100
MC/DC0%0/2
defined $psgi_env->{"QUERY_STRING"} and $psgi_env->{"QUERY_STRING"} ne ""
defined $psgi_env->{"QUERY_STRING"} $psgi_env->{"QUERY_STRING"} ne ""
Truth table33%1/3
A and B
A= defined $psgi_env->{"QUERY_STRING"}B= $psgi_env->{"QUERY_STRING"} ne ""
ABresult
0X0
100
111
299 ? "?$psgi_env->{QUERY_STRING}"
300 : ''
301 );
302 24 ▶ my $protocol = $psgi_env->{SERVER_PROTOCOL} // 'HTTP/1.1';
Condition100%2/2
$psgi_env->{"SERVER_PROTOCOL"} // "HTTP/1.1"
l!l
1212
MC/DC100%1/1
$psgi_env->{"SERVER_PROTOCOL"} // "HTTP/1.1"
$psgi_env->{"SERVER_PROTOCOL"}
Truth table100%2/2
A
A= $psgi_env->{"SERVER_PROTOCOL"}
Aresult
11
00
303  
304 return Zuzu::Value::Dict->new->add(
305 [ method => $psgi_env->{REQUEST_METHOD} // '' ],
306 [ protocol => $protocol ],
307 [ server_protocol => $protocol ],
308 [ scheme => _scheme_from_psgi($psgi_env) ],
309 [ host => $psgi_env->{HTTP_HOST} // '' ],
310 [ server_name => $psgi_env->{SERVER_NAME} // '' ],
311 [ server_port => _server_port_from_psgi($psgi_env) ],
312 [ remote_addr => $psgi_env->{REMOTE_ADDR} // '' ],
313 [ remote_host => $psgi_env->{REMOTE_HOST} // '' ],
314 [ remote_user => $psgi_env->{REMOTE_USER} ],
315 [ script_name => $psgi_env->{SCRIPT_NAME} // '' ],
316 [ path => $psgi_env->{PATH_INFO} // '' ],
317 [ raw_path => $raw_path ],
318 [ request_uri => $full_request_uri ],
319 24 ▶ [ query_string => $psgi_env->{QUERY_STRING} // '' ],
Condition100%16/16
$psgi_env->{"REQUEST_METHOD"} // ""
l!l
168
$psgi_env->{"HTTP_HOST"} // ""
l!l
1311
$psgi_env->{"SERVER_NAME"} // ""
l!l
1311
$psgi_env->{"REMOTE_ADDR"} // ""
l!l
1311
$psgi_env->{"REMOTE_HOST"} // ""
l!l
1212
$psgi_env->{"SCRIPT_NAME"} // ""
l!l
1410
$psgi_env->{"PATH_INFO"} // ""
l!l
168
$psgi_env->{"QUERY_STRING"} // ""
l!l
1311
MC/DC100%1/1
$psgi_env->{"REQUEST_METHOD"} // ""
$psgi_env->{"REQUEST_METHOD"}
MC/DC100%1/1
$psgi_env->{"HTTP_HOST"} // ""
$psgi_env->{"HTTP_HOST"}
MC/DC100%1/1
$psgi_env->{"SERVER_NAME"} // ""
$psgi_env->{"SERVER_NAME"}
MC/DC100%1/1
$psgi_env->{"REMOTE_ADDR"} // ""
$psgi_env->{"REMOTE_ADDR"}
MC/DC100%1/1
$psgi_env->{"REMOTE_HOST"} // ""
$psgi_env->{"REMOTE_HOST"}
MC/DC100%1/1
$psgi_env->{"SCRIPT_NAME"} // ""
$psgi_env->{"SCRIPT_NAME"}
MC/DC100%1/1
$psgi_env->{"PATH_INFO"} // ""
$psgi_env->{"PATH_INFO"}
MC/DC100%1/1
$psgi_env->{"QUERY_STRING"} // ""
$psgi_env->{"QUERY_STRING"}
Truth table100%2/2
A
A= $psgi_env->{"REQUEST_METHOD"}
Aresult
11
00
Truth table100%2/2
A
A= $psgi_env->{"HTTP_HOST"}
Aresult
11
00
Truth table100%2/2
A
A= $psgi_env->{"SERVER_NAME"}
Aresult
11
00
Truth table100%2/2
A
A= $psgi_env->{"REMOTE_ADDR"}
Aresult
11
00
Truth table100%2/2
A
A= $psgi_env->{"REMOTE_HOST"}
Aresult
11
00
Truth table100%2/2
A
A= $psgi_env->{"SCRIPT_NAME"}
Aresult
11
00
Truth table100%2/2
A
A= $psgi_env->{"PATH_INFO"}
Aresult
11
00
Truth table100%2/2
A
A= $psgi_env->{"QUERY_STRING"}
Aresult
11
00
320 [ headers => _headers_from_psgi($psgi_env) ],
321 [ body => Zuzu::Value::BinaryString->new( bytes => $body ) ],
322 [ body_text => _body_text($body) ],
323 );
324 }
325  
326 24 ▶sub _scheme_from_psgi {
Subroutine _scheme_from_psgi: called CC=5 SCAR=16.2
327 24 my ( $psgi_env ) = @_;
328  
329 return $psgi_env->{'psgi.url_scheme'}
330 if defined $psgi_env->{'psgi.url_scheme'}
331 24 ▶ and $psgi_env->{'psgi.url_scheme'} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $psgi_env->{"psgi.url_scheme"} and $psgi_env->{"psgi.url_scheme"} ne "" 13 11 24
Condition66%2/3
defined $psgi_env->{"psgi.url_scheme"} and $psgi_env->{"psgi.url_scheme"} ne ""
!ll&&!rl&&r
11013
MC/DC50%1/2
defined $psgi_env->{"psgi.url_scheme"} and $psgi_env->{"psgi.url_scheme"} ne ""
defined $psgi_env->{"psgi.url_scheme"} $psgi_env->{"psgi.url_scheme"} ne ""
Truth table66%2/3
A and B
A= defined $psgi_env->{"psgi.url_scheme"}B= $psgi_env->{"psgi.url_scheme"} ne ""
ABresult
0X0
100
111
332  
333 return 'https'
334 if defined $psgi_env->{HTTPS}
335 11 ▶ and $psgi_env->{HTTPS} =~ /\A(?:on|1)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $psgi_env->{"HTTPS"} and $psgi_env->{"HTTPS"} =~ /\A(?:on|1)\z/i 1 10 11
Condition66%2/3
defined $psgi_env->{"HTTPS"} and $psgi_env->{"HTTPS"} =~ /\A(?:on|1)\z/i
!ll&&!rl&&r
1001
MC/DC50%1/2
defined $psgi_env->{"HTTPS"} and $psgi_env->{"HTTPS"} =~ /\A(?:on|1)\z/i
defined $psgi_env->{"HTTPS"} $psgi_env->{"HTTPS"} =~ /\A(?:on|1)\z/i
Truth table66%2/3
A and B
A= defined $psgi_env->{"HTTPS"}B= $psgi_env->{"HTTPS"} =~ /\A(?:on|1)\z/i
ABresult
0X0
100
111
336  
337 10 return 'http';
338 }
339  
340 24 ▶sub _server_port_from_psgi {
Subroutine _server_port_from_psgi: called CC=3 SCAR=11.0
341 24 my ( $psgi_env ) = @_;
342 24 my $port = $psgi_env->{SERVER_PORT};
343  
344 24 ▶ return undef if !defined $port or $port !~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port and $port =~ /\A[0-9]+\z/ 11 13 24
Condition100%3/3
defined $port and $port =~ /\A[0-9]+\z/
!ll&&!rl&&r
10113
MC/DC100%2/2
defined $port and $port =~ /\A[0-9]+\z/
defined $port $port =~ /\A[0-9]+\z/
Truth table100%3/3
A and B
A= defined $portB= $port =~ /\A[0-9]+\z/
ABresult
0X0
100
111
345 13 return 0 + $port;
346 }
347  
348 24 ▶sub _headers_from_psgi {
Subroutine _headers_from_psgi: called CC=5 SCAR=16.1
349 24 my ( $psgi_env ) = @_;
350 24 my $headers = Zuzu::Value::PairList->new;
351  
352 $headers->add( 'Content-Type', $psgi_env->{CONTENT_TYPE} )
353 24 ▶ if defined $psgi_env->{CONTENT_TYPE};
Branch: 1 branch
BranchTrueFalseTotal
if defined $psgi_env->{"CONTENT_TYPE"} 2 22 24
354 $headers->add( 'Content-Length', $psgi_env->{CONTENT_LENGTH} )
355 24 ▶ if defined $psgi_env->{CONTENT_LENGTH};
Branch: 1 branch
BranchTrueFalseTotal
if defined $psgi_env->{"CONTENT_LENGTH"} 15 9 24
356  
357 24 for my $key ( sort grep { /\AHTTP_/ } keys %$psgi_env ) {
358 17 ▶ next if !defined $psgi_env->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $psgi_env->{$key} 0 17 17
359 17 $headers->add( _header_name_from_psgi_key($key), $psgi_env->{$key} );
360 }
361  
362 24 return $headers;
363 }
364  
365 17 ▶sub _header_name_from_psgi_key {
Subroutine _header_name_from_psgi_key: called CC=1 SCAR=0.0
366 17 my ( $key ) = @_;
367 17 $key =~ s/\AHTTP_//;
368  
369 17 return join '-', map { ucfirst lc } split /_/, $key;
370 }
371  
372 26 ▶sub _read_psgi_body {
Subroutine _read_psgi_body: called CC=6 SCAR=26.2
373 26 my ( $psgi_env ) = @_;
374 26 my $input = $psgi_env->{'psgi.input'};
375  
376 26 ▶ return '' if !defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 9 17 26
377  
378 17 my $length = $psgi_env->{CONTENT_LENGTH};
379 17 ▶ if ( defined $length and $length =~ /\A[0-9]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length and $length =~ /\A[0-9]+\z/) 17 0 17
Condition33%1/3
defined $length and $length =~ /\A[0-9]+\z/
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $length and $length =~ /\A[0-9]+\z/
defined $length $length =~ /\A[0-9]+\z/
Truth table33%1/3
A and B
A= defined $lengthB= $length =~ /\A[0-9]+\z/
ABresult
0X0
100
111
380 17 return _read_exactly( $input, 0 + $length );
381 }
382  
383 0 my $body = '';
384 0 while ( 1 ) {
385 0 my $chunk = '';
386 0 my $read = read $input, $chunk, 8192;
387 0 ▶ die "Could not read PSGI request body: $!\n" if !defined $read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 0 0
388 0 ▶ last if $read == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 0 0 0
389 0 $body .= $chunk;
390 }
391  
392 0 return $body;
393 }
394  
395 17 ▶sub _read_exactly {
Subroutine _read_exactly: called CC=4 SCAR=13.9
396 17 my ( $input, $length ) = @_;
397 17 my $body = '';
398  
399 17 ▶ while ( length($body) < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body < $length) 8 15 23
400 8 my $remaining = $length - length($body);
401 8 my $chunk = '';
402 8 my $read = read $input, $chunk, $remaining;
403 8 ▶ die "Could not read PSGI request body: $!\n" if !defined $read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 8 8
404 8 ▶ die "PSGI request body ended before CONTENT_LENGTH bytes were read\n"
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 2 6 8
405 if $read == 0;
406 6 $body .= $chunk;
407 }
408  
409 15 return $body;
410 }
411  
412 24 ▶sub _body_text {
Subroutine _body_text: called CC=1 SCAR=0.0
413 24 my ( $bytes ) = @_;
414  
415 24 return eval { Encode::decode( 'UTF-8', $bytes, Encode::FB_CROAK ) };
416 }
417  
418 20 ▶sub _build_runtime {
Subroutine _build_runtime: called CC=4 SCAR=13.9
419 20 my ( %args ) = @_;
420  
421 my @lib = (
422 20 ▶ @{ $args{lib} // [] },
Condition100%2/2
$args{"lib"} // []
l!l
317
MC/DC100%1/1
$args{"lib"} // []
$args{"lib"}
Truth table100%2/2
A
A= $args{"lib"}
Aresult
11
00
423 @Zuzu::Runtime::DEFAULT_LIB,
424 );
425  
426 return Zuzu::Runtime->new(
427 lib => \@lib,
428 deny => $args{deny} // [],
429 20 ▶ deny_modules => $args{deny_modules} // [],
Condition100%4/4
$args{"deny"} // []
l!l
317
$args{"deny_modules"} // []
l!l
317
MC/DC100%1/1
$args{"deny"} // []
$args{"deny"}
MC/DC100%1/1
$args{"deny_modules"} // []
$args{"deny_modules"}
Truth table100%2/2
A
A= $args{"deny"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"deny_modules"}
Aresult
11
00
430 );
431 }
432  
433 20 ▶sub _slurp_utf8 {
Subroutine _slurp_utf8: called CC=2 SCAR=7.0
434 20 my ( $path ) = @_;
435  
436 20 ▶ open my $fh, '<:encoding(UTF-8)', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $path or die "Could not open '${path}': $!\n" 20 0 20
437 or die "Could not open '$path': $!\n";
438 20 local $/;
439 20 my $source = <$fh>;
440 20 close $fh;
441  
442 20 return $source;
443 }
444  
445 =pod
446  
447 =head1 NAME
448  
449 Zuzu::Web::PSGI - PSGI adapter for ZuzuScript web applications
450  
451 =head1 DESCRIPTION
452  
453 Loads a ZuzuScript application at PSGI application construction time and
454 returns a PSGI app coderef. The application must define C<__request__>.
455  
456 Request headers are converted from the standard CGI-style PSGI
457 environment variables. PSGI normally does not preserve original header
458 order, original spelling, or duplicate request headers.
459  
460 =head1 COPYRIGHT AND LICENCE
461  
462 B<< Zuzu::Web::PSGI >> is copyright Toby Inkster.
463  
464 It is free software; you may redistribute it and/or modify it under
465 the terms of either the Artistic License 1.0 or the GNU General Public
466 License version 2.
467  
468 =cut
469  
470 7 1;
« blib/lib/Zuzu/Value/Equality.pm ↑ Summary blib/lib/Zuzu/Runtime/Async/TaskGroup.pm »