blib/lib/Net/UPnP/HTTP.pm

filter: Statementstmt 96.1% 252 / 262 Branchbran 79.6% 121 / 152 Conditioncond 46.0% 41 / 89 MC/DCmcdc 25.0% 12 / 48 Subroutinesub 100% 28 / 28 Podpod 0.0% 0 / 5 totaltotal 77.7% 454 / 584 CC 109 SCAR 31.6
CC
109
Cov
82%
_exchange
55.2
post
18.0
_resolve
13.9
CRAP 23.5
« blib/lib/Net/UPnP/ControlPoint.pm ↑ Summary blib/lib/Net/UPnP/AV/MediaServer.pm »
1 package Net::UPnP::HTTP;
2  
3 #-----------------------------------------------------------------
4 # Net::UPnP::HTTP
5 #-----------------------------------------------------------------
6  
7 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Errno qw(EINPROGRESS EWOULDBLOCK EAGAIN EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use POSIX qw(_exit);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use Net::UPnP;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Net::UPnP::HTTPResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 ▶use vars qw($STATUS_CODE $STATUS $HEADER $CONTENT $POST $GET);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 $POST = 'POST';
24 8 $GET = 'GET';
25  
26 8 $STATUS_CODE = 'status_code';
27 8 $STATUS = 'status';
28 8 $HEADER = 'header';
29 8 $CONTENT = 'content';
30  
31 #------------------------------
32 # new
33 #------------------------------
34  
35 66 ▶sub new {
Subroutine new: called CC=12 SCAR=25.3
Pod undocumented
36 66 my($class) = shift;
37 66 my($this) = { timeout => 10, max_header_bytes => 65536,
38 max_line_bytes => 8192, max_body_bytes => 16 * 1024 * 1024, @_ };
39 66 foreach my $key (qw(timeout max_header_bytes max_line_bytes max_body_bytes)) {
40 croak "Invalid HTTP $key" unless defined($this->{$key})
41 && $this->{$key} =~ ($key eq 'timeout' ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/)
42 257 ▶ && _isfinite(0 + $this->{$key}) && $this->{$key} > 0;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $this->{$key} and $this->{$key} =~ ($key eq "timeout" ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/) and _isfinite(0 + $this->{$key}) and $this->{$key} > 0 4 253 257
$key eq 'timeout' ? : 66 191 257
Condition77%7/9
defined $this->{$key} and $this->{$key} =~ ($key eq "timeout" ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/) and _isfinite(0 + $this->{$key}) and $this->{$key} > 0
!ll&&!rl&&r
40253
defined $this->{$key} and $this->{$key} =~ ($key eq "timeout" ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/) and _isfinite(0 + $this->{$key})
!ll&&!rl&&r
13253
defined $this->{$key} and $this->{$key} =~ ($key eq "timeout" ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/)
!ll&&!rl&&r
01256
MC/DC50%2/4
defined $this->{$key} and $this->{$key} =~ ($key eq "timeout" ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/) and _isfinite(0 + $this->{$key}) and $this->{$key} > 0
defined $this->{$key} $this->{$key} =~ ($key eq "timeout" ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/) _isfinite(0 + $this->{$key}) $this->{$key} > 0
Truth table60%3/5
A and B and C and D
A= defined $this->{$key}B= $this->{$key} =~ ($key eq "timeout" ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/)C= _isfinite(0 + $this->{$key})D= $this->{$key} > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
43 }
44 62 ▶ if (exists $this->{deadline}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"deadline"}) 9 53 62
45 croak 'Invalid HTTP deadline' unless defined($this->{deadline})
46 9 ▶ && $this->{deadline} =~ /\A\d+(?:\.\d+)?\z/ && _isfinite(0 + $this->{deadline});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"deadline"} and $this->{"deadline"} =~ /\A\d+(?:\.\d+)?\z/ and _isfinite(0 + $this->{"deadline"}) 1 8 9
Condition50%3/6
defined $this->{"deadline"} and $this->{"deadline"} =~ /\A\d+(?:\.\d+)?\z/ and _isfinite(0 + $this->{"deadline"})
!ll&&!rl&&r
018
defined $this->{"deadline"} and $this->{"deadline"} =~ /\A\d+(?:\.\d+)?\z/
!ll&&!rl&&r
009
MC/DC33%1/3
defined $this->{"deadline"} and $this->{"deadline"} =~ /\A\d+(?:\.\d+)?\z/ and _isfinite(0 + $this->{"deadline"})
defined $this->{"deadline"} $this->{"deadline"} =~ /\A\d+(?:\.\d+)?\z/ _isfinite(0 + $this->{"deadline"})
Truth table50%2/4
A and B and C
A= defined $this->{"deadline"}B= $this->{"deadline"} =~ /\A\d+(?:\.\d+)?\z/C= _isfinite(0 + $this->{"deadline"})
ABCresult
0XX0
10X0
1100
1111
47 }
48 $this->{allowed_addresses} = _address_list($this->{allowed_addresses})
49 61 ▶ if exists $this->{allowed_addresses};
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->{"allowed_addresses"} 29 32 61
50 57 bless $this, $class;
51 }
52  
53 # Only caller-supplied numeric IPv4 addresses may expand a discovery policy.
54 45 ▶sub _address_list {
Subroutine _address_list: called CC=6 SCAR=17.9
55 45 my ($addresses) = @_;
56 45 ▶ croak 'Invalid allowed_addresses' unless ref($addresses) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $addresses eq "ARRAY" 2 43 45
57 43 my @copy;
58 43 foreach my $address (@$addresses) {
59 38 ▶ croak 'Invalid allowed IPv4 address' unless defined($address)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address and $address =~ /\A(?:\d{1,3}\.){3}\d{1,3}\z/ 1 37 38
Condition66%2/3
defined $address and $address =~ /\A(?:\d{1,3}\.){3}\d{1,3}\z/
!ll&&!rl&&r
0137
MC/DC50%1/2
defined $address and $address =~ /\A(?:\d{1,3}\.){3}\d{1,3}\z/
defined $address $address =~ /\A(?:\d{1,3}\.){3}\d{1,3}\z/
Truth table66%2/3
A and B
A= defined $addressB= $address =~ /\A(?:\d{1,3}\.){3}\d{1,3}\z/
ABresult
0X0
100
111
60 && $address =~ /\A(?:\d{1,3}\.){3}\d{1,3}\z/;
61 37 my @octets = split(/\./, $address);
62 37 ▶ croak 'Invalid allowed IPv4 address' if grep { $_ > 255 } @octets;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 255;} @octets 1 36 37
63 36 push @copy, inet_ntoa(pack('C4', @octets));
64 }
65 41 return \@copy;
66 }
67  
68 1594 ▶sub _now { clock_gettime(CLOCK_MONOTONIC) }
Subroutine _now: called CC=1 SCAR=0.0
69  
70 # True when the number is neither an infinity nor a NaN. POSIX::isfinite needs
71 # perl 5.22, so this stays pure Perl: inf * 0 and NaN * 0 are both NaN, and
72 # NaN never compares equal to anything (RT #182943).
73 458 ▶sub _isfinite {
Subroutine _isfinite: called CC=2 SCAR=6.9
74 458 my $number = 0 + shift;
75 458 ▶ return ($number * 0 == 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$number * 0 == 0 ? : 442 16 458
76 }
77  
78 37 ▶sub geterror { $_[0]->{error} || '' }
Subroutine geterror: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"error"} or "" 30 7 37
79  
80 115 ▶sub _wait {
Subroutine _wait: called CC=7 SCAR=19.5
81 115 my ($fh, $write, $deadline) = @_;
82 115 my $select = IO::Select->new($fh);
83 115 while (1) {
84 116 my $remaining = $deadline - _now();
85 116 ▶ die "HTTP deadline exceeded\n" if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 116 116
86 116 $! = 0;
87 116 ▶ my @ready = $write ? $select->can_write($remaining) : $select->can_read($remaining);
Branch: 1 branch
BranchTrueFalseTotal
$write ? : 64 52 116
88 116 ▶ next if !@ready && $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if not @ready and $! == 4 1 115 116
Condition100%3/3
not @ready and $! == 4
!ll&&!rl&&r
10871
MC/DC100%2/2
not @ready and $! == 4
not @ready $! == 4
Truth table100%3/3
not(A) and B
A= not @readyB= $! == 4
ABresult
0X0
100
111
89 115 ▶ die "HTTP deadline exceeded\n" unless @ready && _now() < $deadline;
Branch: 1 branch
BranchTrueFalseTotal
unless @ready and _now() < $deadline 7 108 115
Condition66%2/3
@ready and _now() < $deadline
!ll&&!rl&&r
70108
MC/DC50%1/2
@ready and _now() < $deadline
@ready _now() < $deadline
Truth table66%2/3
A and B
A= @readyB= _now() < $deadline
ABresult
0X0
100
111
90 108 return;
91 }
92 }
93  
94 # Resolve in a child so a blocking system resolver cannot outlive the request.
95 # The pipe carries exactly four address bytes, never executable input.
96 49 ▶sub _resolve {
Subroutine _resolve: called CC=10 SCAR=26.3
97 49 my ($host, $deadline) = @_;
98 49 ▶ if ($host =~ /\A(?:\d{1,3}\.){3}\d{1,3}\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /\A(?:\d{1,3}\.){3}\d{1,3}\z/) 43 6 49
99 43 my @octets = split(/\./, $host);
100 43 ▶ die "Invalid IPv4 address\n" if grep { $_ > 255 } @octets;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 255;} @octets 0 43 43
101 43 return pack('C4', @octets);
102 }
103 6 ▶ pipe(my $reader, my $writer) or die "Resolver pipe failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $reader, my $writer or die "Resolver pipe failed: $!\n" 6 0 6
104 6 my $pid = fork();
105 6 ▶ unless (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 6 6
106 0 close($reader); close($writer);
107 0 die "Resolver fork failed: $!\n";
108 }
109 6 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 6 6
110 0 close($reader);
111 0 my $address = inet_aton($host);
112 0 ▶ syswrite($writer, $address) if defined $address;
Branch: 1 branch
BranchTrueFalseTotal
if defined $address 0 0 0
113 0 close($writer);
114 0 _exit(0);
115 }
116 6 close($writer);
117 6 my $address = '';
118 6 my $ok = eval {
119 6 _wait($reader, 0, $deadline);
120 5 my $n = sysread($reader, $address, 4);
121 5 ▶ die "Host resolution failed\n" unless defined($n) && $n == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n == 4 0 5 5
Condition33%1/3
defined $n and $n == 4
!ll&&!rl&&r
005
MC/DC0%0/2
defined $n and $n == 4
defined $n $n == 4
Truth table33%1/3
A and B
A= defined $nB= $n == 4
ABresult
0X0
100
111
122 5 1;
123 };
124 6 my $error = $@;
125 6 close($reader);
126 # The child is still ours until waitpid; terminate before reaping so even
127 # a resolver stuck after writing cannot extend the transaction deadline.
128 6 kill 'KILL', $pid;
129 6 waitpid($pid, 0);
130 6 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 5 6
131 5 return $address;
132 }
133  
134 44 ▶sub _fill {
Subroutine _fill: called CC=7 SCAR=23.2
135 44 my ($state, $deadline, $limit) = @_;
136 44 while (1) {
137 44 _wait($state->{socket}, 0, $deadline);
138 38 my $data = '';
139 38 ▶ my $size = $limit < 16384 ? $limit : 16384;
Branch: 1 branch
BranchTrueFalseTotal
$limit < 16384 ? : 37 1 38
140 38 my $n = sysread($state->{socket}, $data, $size);
141 38 ▶ next if !defined($n) && ($! == EINTR || $! == EAGAIN || $! == EWOULDBLOCK);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n and $! == 4 || $! == 11 || $! == 11 0 38 38
Condition11%1/9
not defined $n and $! == 4 || $! == 11 || $! == 11
!ll&&!rl&&r
3800
$! == 4 or $! == 11 or $! == 11
l!l&&r!l&&!r
000
$! == 4 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/4
not defined $n and $! == 4 || $! == 11 || $! == 11
not defined $n $! == 4 $! == 11 $! == 11
Truth table20%1/5
not(A) and B or C or D
A= not defined $nB= $! == 4C= $! == 11D= $! == 11
ABCDresult
0XXX0
10000
11XX1
101X1
10011
142 38 ▶ die "HTTP read failed: $!\n" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 38 38
143 38 $state->{buffer} .= $data;
144 38 return $n;
145 }
146 }
147  
148 118 ▶sub _line {
Subroutine _line: called CC=5 SCAR=16.2
149 118 my ($state, $deadline, $limit) = @_;
150 118 while (1) {
151 154 my $end = index($state->{buffer}, "\n");
152 154 ▶ if ($end >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end >= 0) 109 45 154
153 109 ▶ die "HTTP line limit exceeded\n" if $end + 1 > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $end + 1 > $limit 0 109 109
154 109 return substr($state->{buffer}, 0, $end + 1, '');
155 }
156 45 ▶ die "HTTP line limit exceeded\n" if length($state->{buffer}) >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if length $state->{"buffer"} >= $limit 5 40 45
157 die "Incomplete HTTP line\n"
158 40 ▶ unless _fill($state, $deadline, $limit - length($state->{buffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless _fill($state, $deadline, $limit - length($state->{"buffer"})) 0 36 36
159 }
160 }
161  
162 26 ▶sub _exact {
Subroutine _exact: called CC=4 SCAR=13.9
163 26 my ($state, $deadline, $length) = @_;
164 26 my $data = '';
165 26 ▶ while (length($data) < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < $length) 28 23 51
166 28 my $needed = $length - length($data);
167 28 ▶ if (length($state->{buffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->{"buffer"}) 25 3 28
168 25 $data .= substr($state->{buffer}, 0, $needed, '');
169 25 next;
170 }
171 3 ▶ die "Incomplete HTTP body\n" unless _fill($state, $deadline, $needed);
Branch: 1 branch
BranchTrueFalseTotal
unless _fill($state, $deadline, $needed) 1 0 1
172 }
173 23 return $data;
174 }
175  
176 51 ▶sub _exchange {
Subroutine _exchange: called CC=43 SCAR=40.1
177 51 my ($this, $state, $deadline, $address, $port, $request) = @_;
178 51 my $resolved = _resolve($address, $deadline);
179 50 ▶ if (exists $this->{allowed_addresses}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"allowed_addresses"}) 22 28 50
180 22 my $actual = inet_ntoa($resolved);
181 die "HTTP destination not permitted\n"
182 22 ▶ unless grep { $_ eq $actual } @{$this->{allowed_addresses}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $actual;} @{$$this{"allowed_addresses"};} 18 4 22
183 }
184 32 ▶ socket(my $socket, PF_INET, SOCK_STREAM, getprotobyname('tcp'))
Branch: 1 branch
BranchTrueFalseTotal
socket my $socket, 2, 1, getprotobyname "tcp" or die "HTTP socket failed: $!\n" 32 0 32
185 or die "HTTP socket failed: $!\n";
186 32 $state->{socket} = $socket;
187 32 my $flags = fcntl($socket, F_GETFL, 0);
188 32 ▶ die "HTTP nonblocking setup failed: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags and defined fcntl($socket, 4, $flags | 2048) 0 32 32
Condition33%1/3
defined $flags and defined fcntl($socket, 4, $flags | 2048)
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $flags and defined fcntl($socket, 4, $flags | 2048)
defined $flags defined fcntl($socket, 4, $flags | 2048)
Truth table33%1/3
A and B
A= defined $flagsB= defined fcntl($socket, 4, $flags | 2048)
ABresult
0X0
100
111
189 unless defined($flags) && defined(fcntl($socket, F_SETFL, $flags | O_NONBLOCK));
190 32 ▶ unless (connect($socket, sockaddr_in($port, $resolved))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (connect $socket, sockaddr_in($port, $resolved)) 32 0 32
191 32 ▶ die "HTTP connect failed: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 115 or $! == 11 or $! == 4 0 32 32
Condition33%2/6
$! == 115 or $! == 11 or $! == 4
l!l&&r!l&&!r
3200
$! == 115 or $! == 11
l!l&&r!l&&!r
3200
MC/DC0%0/3
$! == 115 or $! == 11 or $! == 4
$! == 115 $! == 11 $! == 4
Truth table25%1/4
A or B or C
A= $! == 115B= $! == 11C= $! == 4
ABCresult
1XX1
01X1
0011
0000
192 unless $! == EINPROGRESS || $! == EWOULDBLOCK || $! == EINTR;
193 32 _wait($socket, 1, $deadline);
194 32 my $error = getsockopt($socket, SOL_SOCKET, SO_ERROR);
195 32 ▶ die "HTTP connect failed\n" unless defined($error) && unpack('i', $error) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error and unpack("i", $error) == 0 0 32 32
Condition33%1/3
defined $error and unpack("i", $error) == 0
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $error and unpack("i", $error) == 0
defined $error unpack("i", $error) == 0
Truth table33%1/3
A and B
A= defined $errorB= unpack("i", $error) == 0
ABresult
0X0
100
111
196 }
197 32 my $offset = 0;
198 32 ▶ while ($offset < length($request)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $request) 32 32 64
199 32 _wait($socket, 1, $deadline);
200 32 my $n = syswrite($socket, $request, length($request) - $offset, $offset);
201 32 ▶ next if !defined($n) && ($! == EINTR || $! == EAGAIN || $! == EWOULDBLOCK);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n and $! == 4 || $! == 11 || $! == 11 0 32 32
Condition11%1/9
not defined $n and $! == 4 || $! == 11 || $! == 11
!ll&&!rl&&r
3200
$! == 4 or $! == 11 or $! == 11
l!l&&r!l&&!r
000
$! == 4 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/4
not defined $n and $! == 4 || $! == 11 || $! == 11
not defined $n $! == 4 $! == 11 $! == 11
Truth table20%1/5
not(A) and B or C or D
A= not defined $nB= $! == 4C= $! == 11D= $! == 11
ABCDresult
0XXX0
10000
11XX1
101X1
10011
202 32 ▶ die "HTTP write failed: $!\n" unless defined($n) && $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n > 0 0 32 32
Condition33%1/3
defined $n and $n > 0
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $n and $n > 0
defined $n $n > 0
Truth table33%1/3
A and B
A= defined $nB= $n > 0
ABresult
0X0
100
111
203 32 $offset += $n;
204 }
205 32 my $header_bytes = 0;
206 102 ▶ my $next_header = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
207 102 my $remaining = $this->{max_header_bytes} - $header_bytes;
208 102 ▶ die "HTTP header limit exceeded\n" if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 102 102
209 102 ▶ my $limit = $remaining < $this->{max_line_bytes} ? $remaining : $this->{max_line_bytes};
Branch: 1 branch
BranchTrueFalseTotal
$remaining < $this->{'max_line_bytes'} ? : 21 81 102
210 102 my $line = _line($state, $deadline, $limit);
211 94 $header_bytes += length($line);
212 94 return $line;
213 32 };
214 32 my $status = $next_header->();
215 28 ▶ die "Invalid HTTP status\n" unless $status =~ /\AHTTP\/\d+\.\d+\s+(\d{3})\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $status =~ m[\AHTTP/\d+\.\d+\s+(\d{3})\b] 0 28 28
216 28 my $status_code = $1;
217 28 my ($headers, $length, $encoding) = ('', undef, '');
218 28 while (1) {
219 57 my $line = $next_header->();
220 54 ▶ last if $line =~ /\A\r?\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\r?\n\z/ 22 32 54
221 32 $headers .= $line;
222 32 ▶ if ($line =~ /\AContent-Length\s*:\s*(.*?)\s*\r?\n\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\AContent-Length\s*:\s*(.*?)\s*\r?\n\z/i) 14 18 32
223 14 my $value = $1;
224 14 ▶ die "Invalid HTTP content length\n" unless $value =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A\d+\z/ 1 13 14
225 13 ▶ die "HTTP body limit exceeded\n" if $value > $this->{max_body_bytes};
Branch: 1 branch
BranchTrueFalseTotal
if $value > $this->{"max_body_bytes"} 1 12 13
226 12 ▶ die "Conflicting HTTP content lengths\n" if defined($length) && $length != $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $length and $length != $value 1 11 12
Condition66%2/3
defined $length and $length != $value
!ll&&!rl&&r
1101
MC/DC50%1/2
defined $length and $length != $value
defined $length $length != $value
Truth table66%2/3
A and B
A= defined $lengthB= $length != $value
ABresult
0X0
100
111
227 11 $length = 0 + $value;
228 }
229 29 ▶ if ($line =~ /\ATransfer-Encoding\s*:\s*(.*?)\s*\r?\n\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\ATransfer-Encoding\s*:\s*(.*?)\s*\r?\n\z/i) 10 19 29
230 10 ▶ die "Unsupported HTTP transfer encoding\n" if length($encoding) || lc($1) ne 'chunked';
Branch: 1 branch
BranchTrueFalseTotal
if length $encoding or lc $1 ne "chunked" 0 10 10
Condition33%1/3
length $encoding or lc $1 ne "chunked"
l!l&&r!l&&!r
0010
MC/DC0%0/2
length $encoding or lc $1 ne "chunked"
length $encoding lc $1 ne "chunked"
Truth table33%1/3
A or B
A= length $encodingB= lc $1 ne "chunked"
ABresult
1X1
011
000
231 10 $encoding = lc($1);
232 }
233 }
234 22 ▶ die "Ambiguous HTTP framing\n" if length($encoding) && defined($length);
Branch: 1 branch
BranchTrueFalseTotal
if length $encoding and defined $length 1 21 22
Condition100%3/3
length $encoding and defined $length
!ll&&!rl&&r
1291
MC/DC100%2/2
length $encoding and defined $length
length $encoding defined $length
Truth table100%3/3
A and B
A= length $encodingB= defined $length
ABresult
0X0
100
111
235 21 my $body = '';
236 21 ▶ if (_bodyless($status_code, $request)) {
Branch: 3 branches
BranchTrueFalseTotal
if (_bodyless($status_code, $request)) { } 1 20 21
elsif ($encoding eq 'chunked') { } 9 11 20
elsif (defined $length) { } 9 2 11
237 # HEAD and status codes without message bodies complete at the headers.
238 }
239 elsif ($encoding eq 'chunked') {
240 9 while (1) {
241 16 my $line = _line($state, $deadline, $this->{max_line_bytes});
242 15 ▶ die "Invalid HTTP chunk size\n" unless $line =~ /\A([0-9a-f]+)(?:;[^\r\n]*)?\r?\n\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A([0-9a-f]+)(?:;[^\r\n]*)?\r?\n\z/i 0 15 15
243 15 my $digits = $1;
244 15 $digits =~ s/\A0+//;
245 # Bound the conversion before hex(), as well as the decoded total.
246 15 ▶ die "HTTP body limit exceeded\n" if length($digits) > length(sprintf('%x', $this->{max_body_bytes}));
Branch: 1 branch
BranchTrueFalseTotal
if length $digits > length sprintf("%x", $this->{"max_body_bytes"}) 1 14 15
247 14 ▶ my $chunk = length($digits) ? hex($digits) : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $digits ? : 10 4 14
248 14 ▶ die "HTTP body limit exceeded\n" if $chunk > $this->{max_body_bytes} - length($body);
Branch: 1 branch
BranchTrueFalseTotal
if $chunk > $this->{"max_body_bytes"} - length($body) 1 13 14
249 13 ▶ if (!$chunk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chunk) 4 9 13
250 4 while (1) {
251 13 my $trailer = $next_header->();
252 12 ▶ last if $trailer =~ /\A\r?\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $trailer =~ /\A\r?\n\z/ 3 9 12
253 }
254 3 last;
255 }
256 9 $body .= _exact($state, $deadline, $chunk);
257 8 ▶ die "Invalid HTTP chunk terminator\n" unless _exact($state, $deadline, 2) eq "\r\n";
Branch: 1 branch
BranchTrueFalseTotal
unless _exact($state, $deadline, 2) eq "\r\n" 1 7 8
258 }
259 }
260 elsif (defined $length) {
261 9 $body = _exact($state, $deadline, $length);
262 }
263 else {
264 2 while (1) {
265 2 ▶ die "HTTP body limit exceeded\n" if length($body) + length($state->{buffer}) > $this->{max_body_bytes};
Branch: 1 branch
BranchTrueFalseTotal
if length($body) + length($state->{"buffer"}) > $this->{"max_body_bytes"} 1 1 2
266 1 $body .= $state->{buffer};
267 1 $state->{buffer} = '';
268 1 ▶ last unless _fill($state, $deadline, $this->{max_body_bytes} - length($body) + 1);
Branch: 1 branch
BranchTrueFalseTotal
unless _fill($state, $deadline, $this->{"max_body_bytes"} - length($body) + 1) 1 0 1
269 }
270 }
271 12 my $res = Net::UPnP::HTTPResponse->new();
272 12 $res->setstatus($status); $res->setheader($headers); $res->setcontent($body);
273 12 return $res;
274 }
275  
276 21 ▶sub _bodyless {
Subroutine _bodyless: called CC=3 SCAR=11.4
277 21 my ($status, $request) = @_;
278 21 ▶ return $request =~ /\AHEAD / || $status == 204 || $status == 304;
Condition66%4/6
$request =~ /\AHEAD / || $status == 204 || $status == 304
l!l&&r!l&&!r
1020
$request =~ /\AHEAD / or $status == 204
l!l&&r!l&&!r
0120
MC/DC33%1/3
$request =~ /\AHEAD / || $status == 204 || $status == 304
$request =~ /\AHEAD / $status == 204 $status == 304
Truth table50%2/4
A or B || C
A= $request =~ /\AHEAD /B= $status == 204C= $status == 304
ABCresult
1XX1
01X1
0011
0000
279 }
280  
281 #------------------------------
282 # post
283 #------------------------------
284  
285 51 ▶sub post {
Subroutine post: called CC=16 SCAR=28.9
Pod undocumented
286 51 my($this) = shift;
287 51 ▶ if (@_ < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 6) 0 51 51
288 0 return "";
289 }
290 51 my ($post_addr, $post_port, $method, $path, $add_header, $req_content) = @_;
291 my (
292 51 $post_sockaddr,
293 $req_content_len,
294 $add_header_name,
295 $add_header_value,
296 $req_header,
297 $res_status,
298 $res_header_cnt,
299 $res_header,
300 $res_content_len,
301 $res_content,
302 $res,
303 );
304  
305 # Thanks for miz <miz999 at gmail.com> (2009/02/13)
306 51 $req_content =~ s/\r//g;
307 51 $req_content =~ s/\n/\r\n/g;
308
309 51 $req_content_len = length($req_content);
310
311 51 $req_header = <<"REQUEST_HEADER";
312 $method $path HTTP/1.1
313 Host: $post_addr:$post_port
314 Content-Length: $req_content_len
315 REQUEST_HEADER
316  
317 #print "header = " . %{$add_header} . "\n";
318 #%add_header = %{$add_header_ref};
319 51 my $has_connection_header;
320 51 ▶ if (ref $add_header) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $add_header) 9 42 51
321 9 ▶ $has_connection_header = 1 if( defined ${$add_header}{'Connection'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined do { $add_header }->{"Connection"} 0 9 9
322 9 ▶ while ( ($add_header_name, $add_header_value) = each %{$add_header}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$add_header;}) 18 9 27
323 18 $req_header .= "$add_header_name: $add_header_value\n";
324 }
325 }
326  
327 51 ▶ $req_header .= "Connection: close\n" unless( $has_connection_header );
Branch: 1 branch
BranchTrueFalseTotal
unless $has_connection_header 51 0 51
328 51 $req_header .= "\n";
329 51 $req_header =~ s/\r//g;
330 51 $req_header =~ s/\n/\r\n/g;
331  
332 51 $this->{error} = '';
333 51 my $deadline = _now() + $this->{timeout};
334 51 ▶ $deadline = $this->{deadline} if defined($this->{deadline}) && $this->{deadline} < $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"deadline"} and $this->{"deadline"} < $deadline 4 47 51
Condition66%2/3
defined $this->{"deadline"} and $this->{"deadline"} < $deadline
!ll&&!rl&&r
4704
MC/DC50%1/2
defined $this->{"deadline"} and $this->{"deadline"} < $deadline
defined $this->{"deadline"} $this->{"deadline"} < $deadline
Truth table66%2/3
A and B
A= defined $this->{"deadline"}B= $this->{"deadline"} < $deadline
ABresult
0X0
100
111
335 51 my $state = { buffer => '' };
336 51 local $SIG{PIPE} = 'IGNORE';
337 51 my $ok = eval {
338 51 ▶ die "Invalid HTTP port\n" unless defined($post_port) && $post_port =~ /\A\d+\z/
Branch: 1 branch
BranchTrueFalseTotal
unless defined $post_port and $post_port =~ /\A\d+\z/ and $post_port > 0 and $post_port <= 65535 0 51 51
Condition33%3/9
defined $post_port and $post_port =~ /\A\d+\z/ and $post_port > 0 and $post_port <= 65535
!ll&&!rl&&r
0051
defined $post_port and $post_port =~ /\A\d+\z/ and $post_port > 0
!ll&&!rl&&r
0051
defined $post_port and $post_port =~ /\A\d+\z/
!ll&&!rl&&r
0051
MC/DC0%0/4
defined $post_port and $post_port =~ /\A\d+\z/ and $post_port > 0 and $post_port <= 65535
defined $post_port $post_port =~ /\A\d+\z/ $post_port > 0 $post_port <= 65535
Truth table20%1/5
A and B and C and D
A= defined $post_portB= $post_port =~ /\A\d+\z/C= $post_port > 0D= $post_port <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
339 && $post_port > 0 && $post_port <= 65535;
340 51 $res = $this->_exchange($state, $deadline, $post_addr, $post_port, $req_header . $req_content);
341 12 1;
342 };
343 51 my $error = $@;
344 51 ▶ close($state->{socket}) if $state->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"socket"} 32 19 51
345 51 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 39 12 51
346 39 ▶ $this->{error} = $error || 'HTTP request failed';
Condition50%1/2
$error || 'HTTP request failed'
l!l
390
MC/DC0%0/1
$error || 'HTTP request failed'
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
347 39 ▶ print $this->{error} if $Net::UPnP::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::UPnP::DEBUG 0 39 39
348 39 return '';
349 }
350 12 return $res;
351 }
352  
353 #------------------------------
354 # postsoap
355 #------------------------------
356  
357 9 ▶sub postsoap {
Subroutine postsoap: called CC=1 SCAR=0.0
Pod undocumented
358 9 my($this) = shift;
359 9 my ($post_addr, $post_port, $path, $action_name, $action_content) = @_;
360 my (
361 9 %soap_header,
362 $name,
363 $value
364 );
365
366 9 %soap_header = (
367 'Content-Type' => "text/xml; charset=\"utf-8\"",
368 'SOAPACTION' => $action_name,
369 );
370
371 9 $this->post($post_addr, $post_port, $Net::UPnP::HTTP::POST, $path, \%soap_header, $action_content);
372 }
373  
374 #------------------------------
375 # xmldecode
376 #------------------------------
377  
378 1042 ▶sub xmldecode {
Subroutine xmldecode: called CC=2 SCAR=7.1
Pod undocumented
379 my (
380 1042 $str
381 );
382 1042 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 1042 1042
383 0 $str = $_[1];
384 }
385 else {
386 1042 $str = $_[0];
387 }
388 1042 $str =~ s/\&gt;/>/g;
389 1042 $str =~ s/\&lt;/</g;
390 1042 $str =~ s/\&quot;/\"/g;
391 1042 $str =~ s/\&amp;/\&/g;
392 1042 $str;
393 }
394  
395 8 1;
396  
397 __END__
« blib/lib/Net/UPnP/ControlPoint.pm ↑ Summary blib/lib/Net/UPnP/AV/MediaServer.pm »