blib/lib/Net/UPnP/HTTP.pm

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