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 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 isfinite);
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 65 ▶sub new {
Subroutine new: called CC=12 SCAR=25.3
Pod undocumented
36 65 my($class) = shift;
37 65 my($this) = { timeout => 10, max_header_bytes => 65536,
38 max_line_bytes => 8192, max_body_bytes => 16 * 1024 * 1024, @_ };
39 65 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 256 ▶ && 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 253 256
$key eq 'timeout' ? : 65 191 256
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
30253
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
12253
defined $this->{$key} and $this->{$key} =~ ($key eq "timeout" ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/)
!ll&&!rl&&r
01255
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 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 115 ▶sub _wait {
Subroutine _wait: called CC=7 SCAR=19.5
73 115 my ($fh, $write, $deadline) = @_;
74 115 my $select = IO::Select->new($fh);
75 115 while (1) {
76 116 my $remaining = $deadline - _now();
77 116 ▶ die "HTTP deadline exceeded\n" if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 116 116
78 116 $! = 0;
79 116 ▶ my @ready = $write ? $select->can_write($remaining) : $select->can_read($remaining);
Branch: 1 branch
BranchTrueFalseTotal
$write ? : 64 52 116
80 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
81 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
82 108 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 49 ▶sub _resolve {
Subroutine _resolve: called CC=10 SCAR=26.3
89 49 my ($host, $deadline) = @_;
90 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
91 43 my @octets = split(/\./, $host);
92 43 ▶ die "Invalid IPv4 address\n" if grep { $_ > 255 } @octets;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 255;} @octets 0 43 43
93 43 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 44 ▶sub _fill {
Subroutine _fill: called CC=7 SCAR=23.2
127 44 my ($state, $deadline, $limit) = @_;
128 44 while (1) {
129 44 _wait($state->{socket}, 0, $deadline);
130 38 my $data = '';
131 38 ▶ my $size = $limit < 16384 ? $limit : 16384;
Branch: 1 branch
BranchTrueFalseTotal
$limit < 16384 ? : 37 1 38
132 38 my $n = sysread($state->{socket}, $data, $size);
133 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
134 38 ▶ die "HTTP read failed: $!\n" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 38 38
135 38 $state->{buffer} .= $data;
136 38 return $n;
137 }
138 }
139  
140 118 ▶sub _line {
Subroutine _line: called CC=5 SCAR=16.2
141 118 my ($state, $deadline, $limit) = @_;
142 118 while (1) {
143 154 my $end = index($state->{buffer}, "\n");
144 154 ▶ if ($end >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end >= 0) 109 45 154
145 109 ▶ die "HTTP line limit exceeded\n" if $end + 1 > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $end + 1 > $limit 0 109 109
146 109 return substr($state->{buffer}, 0, $end + 1, '');
147 }
148 45 ▶ die "HTTP line limit exceeded\n" if length($state->{buffer}) >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if length $state->{"buffer"} >= $limit 5 40 45
149 die "Incomplete HTTP line\n"
150 40 ▶ unless _fill($state, $deadline, $limit - length($state->{buffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless _fill($state, $deadline, $limit - length($state->{"buffer"})) 0 36 36
151 }
152 }
153  
154 26 ▶sub _exact {
Subroutine _exact: called CC=4 SCAR=13.9
155 26 my ($state, $deadline, $length) = @_;
156 26 my $data = '';
157 26 ▶ while (length($data) < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < $length) 28 23 51
158 28 my $needed = $length - length($data);
159 28 ▶ if (length($state->{buffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->{"buffer"}) 25 3 28
160 25 $data .= substr($state->{buffer}, 0, $needed, '');
161 25 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 23 return $data;
166 }
167  
168 51 ▶sub _exchange {
Subroutine _exchange: called CC=43 SCAR=40.1
169 51 my ($this, $state, $deadline, $address, $port, $request) = @_;
170 51 my $resolved = _resolve($address, $deadline);
171 50 ▶ if (exists $this->{allowed_addresses}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"allowed_addresses"}) 22 28 50
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 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
177 or die "HTTP socket failed: $!\n";
178 32 $state->{socket} = $socket;
179 32 my $flags = fcntl($socket, F_GETFL, 0);
180 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
181 unless defined($flags) && defined(fcntl($socket, F_SETFL, $flags | O_NONBLOCK));
182 32 ▶ unless (connect($socket, sockaddr_in($port, $resolved))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (connect $socket, sockaddr_in($port, $resolved)) 32 0 32
183 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
184 unless $! == EINPROGRESS || $! == EWOULDBLOCK || $! == EINTR;
185 32 _wait($socket, 1, $deadline);
186 32 my $error = getsockopt($socket, SOL_SOCKET, SO_ERROR);
187 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
188 }
189 32 my $offset = 0;
190 32 ▶ while ($offset < length($request)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $request) 32 32 64
191 32 _wait($socket, 1, $deadline);
192 32 my $n = syswrite($socket, $request, length($request) - $offset, $offset);
193 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
194 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
195 32 $offset += $n;
196 }
197 32 my $header_bytes = 0;
198 102 ▶ my $next_header = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
199 102 my $remaining = $this->{max_header_bytes} - $header_bytes;
200 102 ▶ die "HTTP header limit exceeded\n" if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 102 102
201 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
202 102 my $line = _line($state, $deadline, $limit);
203 94 $header_bytes += length($line);
204 94 return $line;
205 32 };
206 32 my $status = $next_header->();
207 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
208 28 my $status_code = $1;
209 28 my ($headers, $length, $encoding) = ('', undef, '');
210 28 while (1) {
211 57 my $line = $next_header->();
212 54 ▶ last if $line =~ /\A\r?\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\r?\n\z/ 22 32 54
213 32 $headers .= $line;
214 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
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 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
222 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
223 10 $encoding = lc($1);
224 }
225 }
226 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
227 21 my $body = '';
228 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
229 # HEAD and status codes without message bodies complete at the headers.
230 }
231 elsif ($encoding eq 'chunked') {
232 9 while (1) {
233 16 my $line = _line($state, $deadline, $this->{max_line_bytes});
234 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
235 15 my $digits = $1;
236 15 $digits =~ s/\A0+//;
237 # Bound the conversion before hex(), as well as the decoded total.
238 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
239 14 ▶ my $chunk = length($digits) ? hex($digits) : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $digits ? : 10 4 14
240 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
241 13 ▶ if (!$chunk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chunk) 4 9 13
242 4 while (1) {
243 13 my $trailer = $next_header->();
244 12 ▶ last if $trailer =~ /\A\r?\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $trailer =~ /\A\r?\n\z/ 3 9 12
245 }
246 3 last;
247 }
248 9 $body .= _exact($state, $deadline, $chunk);
249 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
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 12 my $res = Net::UPnP::HTTPResponse->new();
264 12 $res->setstatus($status); $res->setheader($headers); $res->setcontent($body);
265 12 return $res;
266 }
267  
268 21 ▶sub _bodyless {
Subroutine _bodyless: called CC=3 SCAR=11.4
269 21 my ($status, $request) = @_;
270 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
271 }
272  
273 #------------------------------
274 # post
275 #------------------------------
276  
277 51 ▶sub post {
Subroutine post: called CC=16 SCAR=28.9
Pod undocumented
278 51 my($this) = shift;
279 51 ▶ if (@_ < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 6) 0 51 51
280 0 return "";
281 }
282 51 my ($post_addr, $post_port, $method, $path, $add_header, $req_content) = @_;
283 my (
284 51 $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 51 $req_content =~ s/\r//g;
299 51 $req_content =~ s/\n/\r\n/g;
300
301 51 $req_content_len = length($req_content);
302
303 51 $req_header = <<"REQUEST_HEADER";
304 $method $path HTTP/1.1
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 51 my $has_connection_header;
312 51 ▶ if (ref $add_header) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $add_header) 9 42 51
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 51 ▶ $req_header .= "Connection: close\n" unless( $has_connection_header );
Branch: 1 branch
BranchTrueFalseTotal
unless $has_connection_header 51 0 51
320 51 $req_header .= "\n";
321 51 $req_header =~ s/\r//g;
322 51 $req_header =~ s/\n/\r\n/g;
323  
324 51 $this->{error} = '';
325 51 my $deadline = _now() + $this->{timeout};
326 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
327 51 my $state = { buffer => '' };
328 51 local $SIG{PIPE} = 'IGNORE';
329 51 my $ok = eval {
330 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
331 && $post_port > 0 && $post_port <= 65535;
332 51 $res = $this->_exchange($state, $deadline, $post_addr, $post_port, $req_header . $req_content);
333 12 1;
334 };
335 51 my $error = $@;
336 51 ▶ close($state->{socket}) if $state->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"socket"} 32 19 51
337 51 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 39 12 51
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 12 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 1042 ▶sub xmldecode {
Subroutine xmldecode: called CC=2 SCAR=7.1
Pod undocumented
371 my (
372 1042 $str
373 );
374 1042 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 1042 1042
375 0 $str = $_[1];
376 }
377 else {
378 1042 $str = $_[0];
379 }
380 1042 $str =~ s/\&gt;/>/g;
381 1042 $str =~ s/\&lt;/</g;
382 1042 $str =~ s/\&quot;/\"/g;
383 1042 $str =~ s/\&amp;/\&/g;
384 1042 $str;
385 }
386  
387 8 1;
388  
389 __END__
« blib/lib/Net/UPnP/ControlPoint.pm ↑ Summary blib/lib/Net/UPnP/AV/MediaServer.pm »