FastCGI/IO.pm

filter: Statementstmt 21.3% 32 / 150 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 56.2% 9 / 16 Podpod 100% 7 / 7 totaltotal 15.6% 48 / 306 CC 54 SCAR 44.7
CC
54
Cov
12%
read_record
182.0
read_header
132.0
can_read
72.0
CRAP 87.2
↑ Summary FastCGI/Protocol.pm »
1 package Net::FastCGI::IO;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Errno qw[EBADF EINTR EPIPE];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::FastCGI::Constant qw[FCGI_HEADER_LEN];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::FastCGI::Protocol qw[build_header build_record build_stream
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 parse_header parse_record];
11  
12 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 our $VERSION = '0.14';
14 1 our @EXPORT_OK = qw[ can_read
15 can_write
16 read_header
17 read_record
18 write_header
19 write_record
20 write_stream ];
21  
22 1 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
23  
24 1 require Exporter;
25 1 *import = \&Exporter::import;
26  
27 1 ▶ eval q<use Time::HiRes 'time'>;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 }
29  
30 1 *throw = \&Carp::croak;
31  
32 0 ▶sub read_header {
Subroutine read_header: not called CC=11 SCAR=48.8
Pod documented
33 0 ▶ @_ == 1 || throw(q/Usage: read_header(fh)/);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or throw("Usage: read_header(fh)") 0 0 0
34 0 my ($fh) = @_;
35  
36 0 my $len = FCGI_HEADER_LEN;
37 0 my $off = 0;
38 0 my $buf;
39  
40 0 ▶ while ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
41 0 my $r = sysread($fh, $buf, $len, $off);
42 0 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
elsif ($! != 4) { } 0 0 0
43 0 ▶ last unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
44 0 $len -= $r;
45 0 $off += $r;
46 }
47 elsif ($! != EINTR) {
48 0 ▶ warnings::warn(qq<FastCGI: Could not read FCGI_Header: '$!'>)
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
49 if warnings::enabled;
50 0 return;
51 }
52 }
53 0 ▶ if ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
54 0 ▶ $! = $off ? EPIPE : 0;
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
55 0 ▶ warnings::warn(q<FastCGI: Could not read FCGI_Header: Unexpected end of stream>)
Branch: 1 branch
BranchTrueFalseTotal
if $off and warnings::enabled() 0 0 0
Condition0%0/3
$off and warnings::enabled()
!ll&&!rl&&r
000
MC/DC0%0/2
$off and warnings::enabled()
$off warnings::enabled()
Truth table0%0/3
A and B
A= $offB= warnings::enabled()
ABresult
0X0
100
111
56 if $off && warnings::enabled;
57 0 return;
58 }
59 0 return parse_header($buf);
60 }
61  
62 0 ▶sub write_header {
Subroutine write_header: not called CC=6 SCAR=37.4
Pod documented
63 0 ▶ @_ == 5 || throw(q/Usage: write_header(fh, type, request_id, content_length, padding_length)/);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 5 or throw("Usage: write_header(fh, type, request_id, content_length, padding_length)") 0 0 0
64 0 my $fh = shift;
65  
66 0 my $buf = &build_header;
67 0 my $len = FCGI_HEADER_LEN;
68 0 my $off = 0;
69  
70 0 while () {
71 0 my $r = syswrite($fh, $buf, $len, $off);
72 0 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
elsif ($! != 4) { } 0 0 0
73 0 $len -= $r;
74 0 $off += $r;
75 0 ▶ last unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
76 }
77 elsif ($! != EINTR) {
78 0 ▶ warnings::warn(qq<FastCGI: Could not write FCGI_Header: '$!'>)
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
79 if warnings::enabled;
80 0 return undef;
81 }
82 }
83 0 return $off;
84 }
85  
86 0 ▶sub read_record {
Subroutine read_record: not called CC=13 SCAR=52.0
Pod documented
87 0 ▶ @_ == 1 || throw(q/Usage: read_record(fh)/);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or throw("Usage: read_record(fh)") 0 0 0
88 0 my ($fh) = @_;
89  
90 0 my $len = FCGI_HEADER_LEN;
91 0 my $off = 0;
92 0 my $buf;
93  
94 0 ▶ while ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
95 0 my $r = sysread($fh, $buf, $len, $off);
96 0 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
elsif ($! != 4) { } 0 0 0
97 0 ▶ last unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
98 0 $len -= $r;
99 0 $off += $r;
100 0 ▶ if (!$len && $off == FCGI_HEADER_LEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $len and $off == 8) 0 0 0
Condition0%0/3
not $len and $off == 8
!ll&&!rl&&r
000
MC/DC0%0/2
not $len and $off == 8
not $len $off == 8
Truth table0%0/3
not(A) and B
A= not $lenB= $off == 8
ABresult
0X0
100
111
101 0 $len = vec($buf, 2, 16) # Content Length
102 + vec($buf, 6, 8); # Padding Length
103 }
104 }
105 elsif ($! != EINTR) {
106 0 ▶ warnings::warn(qq<FastCGI: Could not read FCGI_Record: '$!'>)
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
107 if warnings::enabled;
108 0 return;
109 }
110 }
111 0 ▶ if ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
112 0 ▶ $! = $off ? EPIPE : 0;
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
113 0 ▶ warnings::warn(q<FastCGI: Could not read FCGI_Record: Unexpected end of stream>)
Branch: 1 branch
BranchTrueFalseTotal
if $off and warnings::enabled() 0 0 0
Condition0%0/3
$off and warnings::enabled()
!ll&&!rl&&r
000
MC/DC0%0/2
$off and warnings::enabled()
$off warnings::enabled()
Truth table0%0/3
A and B
A= $offB= warnings::enabled()
ABresult
0X0
100
111
114 if $off && warnings::enabled;
115 0 return;
116 }
117 0 return parse_record($buf);
118 }
119  
120 0 ▶sub write_record {
Subroutine write_record: not called CC=7 SCAR=40.3
Pod documented
121 0 ▶ @_ == 4 || @_ == 5 || throw(q/Usage: write_record(fh, type, request_id [, content])/);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or @_ == 5 or throw("Usage: write_record(fh, type, request_id [, content])") 0 0 0
Condition0%0/3
@_ == 4 or @_ == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 4 or @_ == 5
@_ == 4 @_ == 5
Truth table0%0/3
A or B
A= @_ == 4B= @_ == 5
ABresult
1X1
011
000
122 0 my $fh = shift;
123  
124 0 my $buf = &build_record;
125 0 my $len = length $buf;
126 0 my $off = 0;
127  
128 0 while () {
129 0 my $r = syswrite($fh, $buf, $len, $off);
130 0 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
elsif ($! != 4) { } 0 0 0
131 0 $len -= $r;
132 0 $off += $r;
133 0 ▶ last unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
134 }
135 elsif ($! != EINTR) {
136 0 ▶ warnings::warn(qq<FastCGI: Could not write FCGI_Record: '$!'>)
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
137 if warnings::enabled;
138 0 return undef;
139 }
140 }
141 0 return $off;
142 }
143  
144 0 ▶sub write_stream {
Subroutine write_stream: not called CC=7 SCAR=40.3
Pod documented
145 0 ▶ @_ == 4 || @_ == 5 || throw(q/Usage: write_stream(fh, type, request_id, content [, terminate])/);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or @_ == 5 or throw("Usage: write_stream(fh, type, request_id, content [, terminate])") 0 0 0
Condition0%0/3
@_ == 4 or @_ == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 4 or @_ == 5
@_ == 4 @_ == 5
Truth table0%0/3
A or B
A= @_ == 4B= @_ == 5
ABresult
1X1
011
000
146 0 my $fh = shift;
147  
148 0 my $buf = &build_stream;
149 0 my $len = length $buf;
150 0 my $off = 0;
151  
152 0 while () {
153 0 my $r = syswrite($fh, $buf, $len, $off);
154 0 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
elsif ($! != 4) { } 0 0 0
155 0 $len -= $r;
156 0 $off += $r;
157 0 ▶ last unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
158 }
159 elsif ($! != EINTR) {
160 0 ▶ warnings::warn(qq<FastCGI: Could not write FCGI_Record stream: '$!'>)
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
161 if warnings::enabled;
162 0 return undef;
163 }
164 }
165 0 return $off;
166 }
167  
168 0 ▶sub can_read (*$) {
Subroutine can_read: not called CC=8 SCAR=42.8
Pod documented
169 0 ▶ @_ == 2 || throw(q/Usage: can_read(fh, timeout)/);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or throw("Usage: can_read(fh, timeout)") 0 0 0
170 0 my ($fh, $timeout) = @_;
171  
172 0 my $fd = fileno($fh);
173 0 ▶ unless (defined $fd && $fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fd and $fd >= 0) 0 0 0
Condition0%0/3
defined $fd and $fd >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table0%0/3
A and B
A= defined $fdB= $fd >= 0
ABresult
0X0
100
111
174 0 $! = EBADF;
175 0 return undef;
176 }
177  
178 0 my $initial = time;
179 0 my $pending = $timeout;
180 0 my $nfound;
181  
182 0 vec(my $fdset = '', $fd, 1) = 1;
183  
184 0 while () {
185 0 $nfound = select($fdset, undef, undef, $pending);
186 0 ▶ if ($nfound == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == -1) 0 0 0
187 0 ▶ return undef unless $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 4 0 0 0
188 0 ▶ redo if !$timeout || ($pending = $timeout - (time - $initial)) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $timeout or ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0 0 0 0
Condition0%0/3
not $timeout or ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $timeout or ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0
not $timeout ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0
Truth table0%0/3
not(A) or B
A= not $timeoutB= ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0
ABresult
1X1
011
000
189 0 $nfound = 0;
190 }
191 0 last;
192 }
193 0 $! = 0;
194 0 return $nfound;
195 }
196  
197 0 ▶sub can_write (*$) {
Subroutine can_write: not called CC=8 SCAR=42.8
Pod documented
198 0 ▶ @_ == 2 || throw(q/Usage: can_write(fh, timeout)/);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or throw("Usage: can_write(fh, timeout)") 0 0 0
199 0 my ($fh, $timeout) = @_;
200  
201 0 my $fd = fileno($fh);
202 0 ▶ unless (defined $fd && $fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fd and $fd >= 0) 0 0 0
Condition0%0/3
defined $fd and $fd >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table0%0/3
A and B
A= defined $fdB= $fd >= 0
ABresult
0X0
100
111
203 0 $! = EBADF;
204 0 return undef;
205 }
206  
207 0 my $initial = time;
208 0 my $pending = $timeout;
209 0 my $nfound;
210  
211 0 vec(my $fdset = '', $fd, 1) = 1;
212  
213 0 while () {
214 0 $nfound = select(undef, $fdset, undef, $pending);
215 0 ▶ if ($nfound == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == -1) 0 0 0
216 0 ▶ return undef unless $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 4 0 0 0
217 0 ▶ redo if !$timeout || ($pending = $timeout - (time - $initial)) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $timeout or ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0 0 0 0
Condition0%0/3
not $timeout or ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $timeout or ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0
not $timeout ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0
Truth table0%0/3
not(A) or B
A= not $timeoutB= ($pending = $timeout - (&Net::FastCGI::IO::time() - $initial)) > 0
ABresult
1X1
011
000
218 0 $nfound = 0;
219 }
220 0 last;
221 }
222 0 $! = 0;
223 0 return $nfound;
224 }
225  
226 1 1;
227  
↑ Summary FastCGI/Protocol.pm »