SCGI/Request.pm

filter: Statementstmt 86.9% 73 / 84 Branchbran 56.0% 28 / 50 Conditioncond 40.0% 10 / 25 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 93.3% 14 / 15 Podpod 100% 6 / 6 totaltotal 69.0% 134 / 194 CC 35 SCAR 27.4
CC
35
Cov
70%
set_blocking
42.0
read_env
22.4
_decode_env
7.8
CRAP 15.4
↑ Summary SCGI.pm »
1 package SCGI::Request;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use SCGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use POSIX ':errno_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = $SCGI::VERSION;
11  
12 =head1 NAME
13  
14 SCGI::Request
15  
16 =head1 DESCRIPTION
17  
18 This module implements the part of the SCGI protocol that reads the environment. All that remains after this is the content of the request. The protocol and this module guarentee that there will be a CONTENT_LENGTH for the body of the request in the environment.
19  
20 =head1 SYNOPISIS
21  
22 # $request got from SCGI
23 $request->read_env;
24
25 read $request->connection, my $body, $request->env->{CONTENT_LENGTH};
26  
27 =head2 public methods
28  
29 =over
30  
31 =item read_env
32  
33 Read the environment in a blocking or non-blocking manner, per parameter to C<SCGI->new>. Returns true if it has finished.
34  
35 =cut
36  
37 12947 ▶sub read_env {
Subroutine read_env: called CC=18 SCAR=31.1
Pod documented
38 12947 my ($this) = @_;
39 12947 ▶ goto &_blocking_read_env if $this->blocking;
Branch: 1 branch
BranchTrueFalseTotal
if $this->blocking 2 12945 12947
40 12945 ▶ die 'read_env called when env already read - use env method to access' if $this->{env_read};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"env_read"} 0 12945 12945
41 12945 ▶ $this->{env_length_buffer} ||= '';
Condition100%2/2
$this->{"env_length_buffer"} ||= ""
l!l
129414
MC/DC100%1/1
$this->{"env_length_buffer"} ||= ""
$this->{"env_length_buffer"}
Truth table100%2/2
A
A= $this->{"env_length_buffer"}
Aresult
11
00
42 12945 ▶ $this->{env_buffer} ||= '';
Condition50%1/2
$this->{"env_buffer"} ||= ""
l!l
012945
MC/DC0%0/1
$this->{"env_buffer"} ||= ""
$this->{"env_buffer"}
Truth table50%1/2
A
A= $this->{"env_buffer"}
Aresult
11
00
43 12945 ▶ unless ($this->{env_length_read}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"env_length_read"}) 12945 0 12945
44 12945 my $bytes_read = sysread $this->connection, my $buffer, 14;
45 12945 ▶ die "read error: $!" unless defined $bytes_read || $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes_read or $! == 11 0 12945 12945
Condition66%2/3
defined $bytes_read or $! == 11
l!l&&r!l&&!r
6129390
MC/DC0%0/2
defined $bytes_read or $! == 11
defined $bytes_read $! == 11
Truth table66%2/3
A or B
A= defined $bytes_readB= $! == 11
ABresult
1X1
011
000
46 12945 ▶ return unless $bytes_read;
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes_read 12939 6 12945
47 6 ▶ if ($buffer =~ m{ ^ (\d+) : (.*) $ }osx) {
Branch: 3 branches
BranchTrueFalseTotal
if ($buffer =~ / ^ (\d+) : (.*) $ /osx) { } 3 3 6
elsif ($this->{'env_length_buffer'} ne '' and $buffer =~ / ^ : (.*) $ /osx) { } 1 2 3
elsif ($buffer =~ / ^ \d+ $ /osx) { } 2 0 2
48 3 $this->{env_length_buffer} .= $1;
49 3 $this->{env_buffer} .= $2;
50 3 $this->{env_length_read} = 1;
51 }
52 ▶ elsif ($this->{env_length_buffer} ne '' && $buffer =~ m{ ^ : (.*) $ }osx) {
Condition100%3/3
$this->{"env_length_buffer"} ne "" and $buffer =~ / ^ : (.*) $ /osx
!ll&&!rl&&r
111
MC/DC100%2/2
$this->{"env_length_buffer"} ne "" and $buffer =~ / ^ : (.*) $ /osx
$this->{"env_length_buffer"} ne "" $buffer =~ / ^ : (.*) $ /osx
Truth table100%3/3
A and B
A= $this->{"env_length_buffer"} ne ""B= $buffer =~ / ^ : (.*) $ /osx
ABresult
0X0
100
111
53 1 $this->{env_buffer} .= $1;
54 1 $this->{env_length_read} = 1;
55 }
56 elsif ($buffer =~ m{ ^ \d+ $ }osx) {
57 2 $this->{env_length_buffer} .= $buffer;
58 2 return;
59 }
60 else {
61 0 die "malformed env length";
62 }
63 }
64 4 my $left_to_read = $this->{env_length_buffer} - length($this->{env_buffer});
65 4 my $buffer = '';
66 4 my $read = sysread $this->connection, $buffer, $left_to_read + 1;
67 4 ▶ die "read error: $!" unless defined $read || $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read or $! == 11 0 4 4
Condition33%1/3
defined $read or $! == 11
l!l&&r!l&&!r
400
MC/DC0%0/2
defined $read or $! == 11
defined $read $! == 11
Truth table33%1/3
A or B
A= defined $readB= $! == 11
ABresult
1X1
011
000
68 4 ▶ return unless $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 0 4 4
69 4 ▶ if ($read == $left_to_read + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == $left_to_read + 1) { } 4 0 4
70 4 ▶ if ((my $comma = substr $buffer, $left_to_read) ne ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $comma = substr($buffer, $left_to_read)) ne ",") 0 4 4
71 0 die "malformed netstring, expected terminating comma, found \"$comma\"";
72 }
73 4 $this->_decode_env($this->{env_buffer} . substr $buffer, 0, $left_to_read);
74 4 return 1;
75 }
76 else {
77 0 $this->{env_buffer} .= $buffer;
78 0 return;
79 }
80 }
81  
82 =item env
83  
84 Gets the environment for this request after it has been read. This will return undef until C<read_env> or C<sysread_env> has been called and returned true.
85  
86 =cut
87  
88 12 ▶sub env {
Subroutine env: called CC=1 SCAR=0.0
Pod documented
89 12 my ($this) = @_;
90 12 $this->{env};
91 }
92  
93 =item connection
94  
95 Returns the open connection to the client.
96  
97 =cut
98  
99 12977 ▶sub connection {
Subroutine connection: called CC=1 SCAR=0.0
Pod documented
100 12977 my ($this) = @_;
101 12977 $this->{connection};
102 }
103  
104 =item close
105  
106 Closes the connection.
107  
108 =cut
109  
110 6 ▶sub close {
Subroutine close: called CC=2 SCAR=7.1
Pod documented
111 6 my ($this) = @_;
112 6 ▶ $this->connection->close if $this->connection;
Branch: 1 branch
BranchTrueFalseTotal
if $this->connection 6 0 6
113 6 $this->{closed} = 1;
114 }
115  
116 =item blocking
117  
118 Returns true if the connection is blocking.
119  
120 =cut
121  
122 12947 ▶sub blocking {
Subroutine blocking: called CC=1 SCAR=0.0
Pod documented
123 12947 my ($this) = @_;
124 12947 $this->{blocking};
125 }
126  
127 =item set_blocking
128  
129 If boolean argument is true turns on blocking, otherwise turns it off.
130  
131 =cut
132  
133 0 ▶sub set_blocking {
Subroutine set_blocking: not called CC=6 SCAR=37.4
Pod documented
134 0 my ($this, $blocking) = @_;
135 0 ▶ return if $this->{blocking} && $blocking || ! $this->{blocking} && ! $blocking;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"blocking"} and $blocking or not $this->{"blocking"} || $blocking 0 0 0
Condition0%0/9
$this->{"blocking"} and $blocking or not $this->{"blocking"} || $blocking
l!l&&r!l&&!r
000
$this->{"blocking"} and $blocking
!ll&&!rl&&r
000
$this->{'blocking'} || $blocking
l!l&&r!l&&!r
000
MC/DC0%0/4
$this->{"blocking"} and $blocking or not $this->{"blocking"} || $blocking
$this->{"blocking"} $blocking $this->{'blocking'} $blocking
Truth table0%0/7
A and B or not(C || D)
A= $this->{"blocking"}B= $blockingC= $this->{'blocking'}D= $blocking
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
136 0 ▶ if ($blocking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocking) { } 0 0 0
137 0 $this->connection->blocking(1);
138 }
139 else {
140 0 $this->connection->flush;
141 0 $this->connection->blocking(0);
142 }
143 }
144  
145 =back
146  
147 =head2 private methods
148  
149 =over
150  
151 =item _new
152  
153 Creates a new SCGI::Request. This is used by SCGI in the C<accept> method, so if you are considering using this, use that instead.
154  
155 =cut
156  
157 6 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
158 6 my ($class, $connection, $blocking) = @_;
159 6 bless {connection => $connection, blocking => $blocking}, $class;
160 }
161  
162 =item _decode_env
163  
164 Takes the encoded environment as a string and sets the env ready for access with C<env>.
165  
166 =cut
167  
168 6 ▶sub _decode_env {
Subroutine _decode_env: called CC=7 SCAR=20.5
169 6 my ($this, $env_string) = @_;
170 6 my %env;
171 6 pos $env_string = 0;
172 6 ▶ $env_string =~ m{
Branch: 1 branch
BranchTrueFalseTotal
$env_string =~ / \G CONTENT_LENGTH \0 (\d+) \0 /cgmosx or die "malformed CONTENT_LENGTH header" 6 0 6
173 \G CONTENT_LENGTH \0 (\d+) \0
174 }msogcx or die "malformed CONTENT_LENGTH header";
175 6 $env{CONTENT_LENGTH} = $1;
176 6 ▶ while ($env_string =~ m{ ([^\0]+) \0 ([^\0]+) \0 }msogcx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_string =~ / ([^\0]+) \0 ([^\0]+) \0 /cgmosx) 13 6 19
177 13 ▶ warn "repeated $1 header in env" if $env{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $env{$1} 0 13 13
178 13 $env{$1} = $2;
179 }
180 6 ▶ die "malformed header" unless pos $env_string = length $env_string;
Branch: 1 branch
BranchTrueFalseTotal
unless pos $env_string = length $env_string 0 6 6
181 6 ▶ die "missing SCGI header" unless $env{SCGI} && $env{SCGI} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
unless $env{"SCGI"} and $env{"SCGI"} eq 1 0 6 6
Condition33%1/3
$env{"SCGI"} and $env{"SCGI"} eq 1
!ll&&!rl&&r
006
MC/DC0%0/2
$env{"SCGI"} and $env{"SCGI"} eq 1
$env{"SCGI"} $env{"SCGI"} eq 1
Truth table33%1/3
A and B
A= $env{"SCGI"}B= $env{"SCGI"} eq 1
ABresult
0X0
100
111
182 6 $this->_set_env(\%env);
183 }
184  
185 =item _set_env
186  
187 Sets the environment for this request.
188  
189 =cut
190  
191 6 ▶sub _set_env {
Subroutine _set_env: called CC=1 SCAR=0.0
192 6 my ($this, $env) = @_;
193 6 $this->{env} = $env;
194 }
195  
196 =item _blocking_read_env
197  
198 Reads and decodes the environment in one go. Returns true on success, raises an exception on failiure.
199  
200 =cut
201  
202 2 ▶sub _blocking_read_env {
Subroutine _blocking_read_env: called CC=5 SCAR=17.5
203 2 my ($this) = @_;
204 2 ▶ read $this->connection, my $env_length, 14 or die "cannot read env length from connection: $!";
Branch: 1 branch
BranchTrueFalseTotal
read $this->connection, my $env_length, 14 or die "cannot read env length from connection: $!" 2 0 2
205 2 ▶ my ($length, $rest) = $env_length =~ m{ ^ (\d+) : (.*) $ }osx
Branch: 1 branch
BranchTrueFalseTotal
() = $env_length =~ / ^ (\d+) : (.*) $ /osx or die "malformed env length" 2 0 2
206 or die 'malformed env length';
207 2 ▶ read $this->connection, my $env, $length + 1 - length($rest) or die "cannot read env from connection: $!";
Branch: 1 branch
BranchTrueFalseTotal
read $this->connection, my $env, $length + 1 - length($rest) or die "cannot read env from connection: $!" 2 0 2
208 2 ▶ if ((my $comma = substr $env, $length - length $rest) ne ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $comma = substr($env, $length - length($rest))) ne ",") 0 2 2
209 0 die "malformed netstring, expected terminating comma, found \"$comma\"";
210 }
211 2 $this->_decode_env($rest . substr $env, 0, $length);
212 2 1;
213 }
214  
215 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
216 6 my ($this) = @_;
217 6 ▶ $this->close unless $this->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"closed"} 0 6 6
218 }
219  
220 5 1;
221  
222 __END__
↑ Summary SCGI.pm »