Parse/PSGI.pm

filter: Statementstmt 96.3% 52 / 54 Branchbran 75.0% 15 / 20 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 87.8% 87 / 99 CC 15 SCAR 24.8
CC
15
Cov
89%
parse_cgi_output
15.5
BEGIN
1.0
BEGIN
1.0
CRAP 11.9
↑ Summary Emulate/PSGI.pm »
1 package CGI::Parse::PSGI;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 our @EXPORT_OK = qw( parse_cgi_output );
5  
6 6 ▶use IO::File; # perl bug: should be loaded to call ->getline etc. on filehandle/PerlIO
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub parse_cgi_output {
10 8 ▶ my $output = shift;
Subroutine parse_cgi_output: called CC=15 SCAR=27.4
Pod undocumented
11  
12 8 my $length;
13 8 ▶ if (ref $output eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $output eq 'SCALAR') { } 3 5 8
14 3 $length = length $$output;
15 3 open my $io, "<", $output;
16 3 $output = $io;
17 } else {
18 5 ▶ open my $tmp, '<&=:perlio:raw', fileno($output) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $tmp, "<&=:perlio:raw", fileno $output or die $! 0 5 5
19 5 $output = $tmp;
20 5 $length = -s $output;
21 }
22  
23 8 my $headers;
24 8 ▶ while ( my $line = $output->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $output->getline) 24 0 24
25 24 $headers .= $line;
26 24 ▶ last if $headers =~ /\x0d?\x0a\x0d?\x0a$/;
Branch: 1 branch
BranchTrueFalseTotal
if $headers =~ /\x0d?\x0a\x0d?\x0a$/ 8 16 24
27 }
28 8 ▶ unless ( defined $headers ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $headers) 0 8 8
29 0 $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a";
30 }
31  
32 8 ▶ unless ( $headers =~ /^HTTP/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers =~ /^HTTP/) 8 0 8
33 8 $headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers;
34 }
35  
36 8 my $response = HTTP::Response->parse($headers);
37  
38 # RFC 3875 6.2.3
39 8 ▶ if ($response->header('Location') && !$response->header('Status')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header("Location") and not $response->header("Status")) 1 7 8
Condition100%3/3
$response->header("Location") and not $response->header("Status")
!ll&&!rl&&r
521
MC/DC100%2/2
$response->header("Location") and not $response->header("Status")
$response->header("Location") not $response->header("Status")
Truth table100%3/3
A and not(B)
A= $response->header("Location")B= not $response->header("Status")
ABresult
0X0
100
111
40 1 $response->header('Status', 302);
41 }
42  
43 8 ▶ my $status = $response->header('Status') || 200;
Condition100%2/2
$response->header('Status') || 200
l!l
35
MC/DC100%1/1
$response->header('Status') || 200
$response->header('Status')
Truth table100%2/2
A
A= $response->header('Status')
Aresult
11
00
44 8 $status =~ s/\s+.*$//; # remove ' OK' in '200 OK'
45  
46 8 $response->remove_header('Status'); # PSGI doesn't allow having Status header in the response
47  
48 8 my $remaining = $length - tell( $output );
49 8 ▶ if ( $response->code == 500 && !$remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 500 and not $remaining) 0 8 8
Condition33%1/3
$response->code == 500 and not $remaining
!ll&&!rl&&r
800
MC/DC0%0/2
$response->code == 500 and not $remaining
$response->code == 500 not $remaining
Truth table33%1/3
A and not(B)
A= $response->code == 500B= not $remaining
ABresult
0X0
100
111
50 return [
51 0 500,
52 [ 'Content-Type' => 'text/html' ],
53 [ $response->error_as_HTML ]
54 ];
55 }
56  
57 # TODO we can pass $output to the response body without buffering all?
58  
59 {
60 8 my $length = 0;
61 8 ▶ while ( $output->read( my $buffer, 4096 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->read(my $buffer, 4096)) 7 8 15
62 7 $length += length($buffer);
63 7 $response->add_content($buffer);
64 }
65  
66 8 ▶ if ( $length && !$response->content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length and not $response->content_length) 5 3 8
Condition100%3/3
$length and not $response->content_length
!ll&&!rl&&r
125
MC/DC100%2/2
$length and not $response->content_length
$length not $response->content_length
Truth table100%3/3
A and not(B)
A= $lengthB= not $response->content_length
ABresult
0X0
100
111
67 5 $response->content_length($length);
68 }
69 }
70  
71 return [
72 $status,
73 +[
74 map {
75 8 my $k = $_;
76 19 map { ( $k => _cleanup_newline($_) ) } $response->headers->header($_);
77 } $response->headers->header_field_names
78 ],
79 [$response->content],
80 ];
81 }
82  
83 sub _cleanup_newline {
84 19 ▶ local $_ = shift;
Subroutine _cleanup_newline: called CC=1 SCAR=0.0
85 19 s/\r?\n//g;
86 19 return $_;
87 }
88  
89 6 1;
90  
91 __END__
↑ Summary Emulate/PSGI.pm »