Parse/PSGI.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 84.6% 22 / 26 Conditioncond 64.2% 9 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 85.5% 101 / 118 CC 19 SCAR 27.7
CC
19
Cov
89%
parse_cgi_output
19.8
BEGIN
1.0
BEGIN
1.0
CRAP 15.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 6 our %DEFAULT_OPTS = (
10 ignore_status_line => 0,
11 );
12  
13 16 ▶sub parse_cgi_output {
Subroutine parse_cgi_output: called CC=19 SCAR=29.9
Pod undocumented
14 16 my $output = shift;
15 16 my $options = \%DEFAULT_OPTS;
16 16 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 4 12 16
17 4 $options = { %DEFAULT_OPTS, %{ +shift } }; # Use default opts where none supplied
18 }
19  
20 16 my $length;
21 16 ▶ if (ref $output eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $output eq 'SCALAR') { } 11 5 16
22 11 $length = length $$output;
23 11 open my $io, "<", $output;
24 11 $output = $io;
25 } else {
26 5 ▶ open my $tmp, '<&=:perlio:raw', fileno($output) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $tmp, "<&=:perlio:raw", fileno $output or die $! 5 0 5
27 5 $output = $tmp;
28 5 $length = -s $output;
29 }
30  
31 16 my $headers;
32 16 ▶ while ( my $line = $output->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $output->getline) 49 0 49
33 49 $headers .= $line;
34 49 ▶ last if $headers =~ /\x0d?\x0a\x0d?\x0a$/;
Branch: 1 branch
BranchTrueFalseTotal
if $headers =~ /\x0d?\x0a\x0d?\x0a$/ 16 33 49
35 }
36 16 ▶ unless ( defined $headers ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $headers) 0 16 16
37 0 $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a";
38 }
39  
40 16 ▶ unless ( $headers =~ /^HTTP/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers =~ /^HTTP/) 10 6 16
41 10 $headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers;
42 }
43  
44 16 my $response = HTTP::Response->parse($headers);
45  
46 # RFC 3875 6.2.3
47 16 ▶ if ($response->header('Location') && !$response->header('Status')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header("Location") and not $response->header("Status")) 1 15 16
Condition100%3/3
$response->header("Location") and not $response->header("Status")
!ll&&!rl&&r
1321
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
48 1 $response->header('Status', 302);
49 }
50  
51 my $status = $options->{ignore_status_line}?
52 16 ▶ 200 : ($response->code || 200);
Branch: 1 branch
BranchTrueFalseTotal
$options->{'ignore_status_line'} ? : 2 14 16
Condition50%1/2
$response->code or 200
l!l
140
MC/DC0%0/1
$response->code or 200
$response->code
Truth table50%1/2
A
A= $response->code
Aresult
11
00
53  
54 16 my $status_header = $response->header('Status');
55 16 ▶ if ($status_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status_header) 6 10 16
56 # Use the header status preferentially, if present and well formed
57  
58 # Extract the code from the header (should be 3 digits, non zero)
59 6 my ($code) = ($status_header =~ /^ \s* (\d+) /x);
60  
61 6 ▶ $status = $code || $status;
Condition33%1/3
$code || $status
l!l&&r!l&&!r
600
MC/DC0%0/2
$code || $status
$code $status
Truth table33%1/3
A || B
A= $codeB= $status
ABresult
1X1
011
000
62 }
63  
64 16 $response->remove_header('Status'); # PSGI doesn't allow having Status header in the response
65  
66 16 my $remaining = $length - tell( $output );
67 16 ▶ if ( $response->code == 500 && !$remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 500 and not $remaining) 0 16 16
Condition33%1/3
$response->code == 500 and not $remaining
!ll&&!rl&&r
1600
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
68 return [
69 0 500,
70 [ 'Content-Type' => 'text/html' ],
71 [ $response->error_as_HTML ]
72 ];
73 }
74  
75 # TODO we can pass $output to the response body without buffering all?
76  
77 {
78 16 my $length = 0;
79 16 ▶ while ( $output->read( my $buffer, 4096 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->read(my $buffer, 4096)) 15 16 31
80 15 $length += length($buffer);
81 15 $response->add_content($buffer);
82 }
83  
84 16 ▶ if ( $length && !$response->content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length and not $response->content_length) 13 3 16
Condition100%3/3
$length and not $response->content_length
!ll&&!rl&&r
1213
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
85 13 $response->content_length($length);
86 }
87 }
88  
89 return [
90 $status,
91 +[
92 map {
93 16 my $k = $_;
94 35 map { ( $k => _cleanup_newline($_) ) } $response->headers->header($_);
95 } $response->headers->header_field_names
96 ],
97 [$response->content],
98 ];
99 }
100  
101 35 ▶sub _cleanup_newline {
Subroutine _cleanup_newline: called CC=1 SCAR=0.0
102 35 local $_ = shift;
103 35 s/\r?\n//g;
104 35 return $_;
105 }
106  
107 6 1;
108  
109 __END__
↑ Summary Emulate/PSGI.pm »