blib/lib/Net/UPnP/Service.pm

filter: Statementstmt 84.7% 117 / 138 Branchbran 65.0% 26 / 40 Conditioncond 73.0% 19 / 26 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 85.7% 18 / 21 Podpod 42.8% 6 / 14 totaltotal 76.0% 194 / 255 CC 31 SCAR 18.0
CC
31
Cov
79%
postaction
12.0
postquery
7.6
_joinurl
7.6
CRAP 6.0
« blib/lib/Net/UPnP/GW/Gateway.pm ↑ Summary blib/lib/Net/UPnP/AV/RSS.pm »
1 package Net::UPnP::Service;
2  
3 #-----------------------------------------------------------------
4 # Net::Net::UPnP::Service
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 Net::UPnP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Net::UPnP::ActionResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Net::UPnP::QueryResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use vars qw($_DEVICE $_DEVICE_DESCRIPTION $SERVICETYPE $SERVICEID $SCPDURL $CONTROLURL $EVENTSUBURL);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 $_DEVICE = 'device';
17 8 $_DEVICE_DESCRIPTION = 'device_description';
18  
19 8 $SERVICETYPE = 'serviceType';
20 8 $SERVICEID = 'serviceId';
21 8 $SCPDURL = 'SCPDURL';
22 8 $CONTROLURL = 'controlURL';
23 8 $EVENTSUBURL = 'eventSubURL';
24  
25 #------------------------------
26 # new
27 #------------------------------
28  
29 14 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
30 14 my($class) = shift;
31 14 my($this) = {
32 $Net::UPnP::Service::_DEVICE => undef,
33 $Net::UPnP::Service::_DEVICE_DESCRIPTION => '',
34 };
35 14 bless $this, $class;
36 }
37  
38 #------------------------------
39 # device
40 #------------------------------
41  
42 14 ▶sub setdevice() {
Subroutine setdevice: called CC=2 SCAR=7.1
Pod undocumented
43 14 my($this) = shift;
44 14 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 0 14
45 14 $this->{$Net::UPnP::Service::_DEVICE} = $_[0];
46 }
47 }
48  
49 34 ▶sub getdevice() {
Subroutine getdevice: called CC=1 SCAR=0.0
Pod documented
50 34 my($this) = shift;
51 34 $this->{$Net::UPnP::Service::_DEVICE};
52 }
53  
54 #------------------------------
55 # device description
56 #------------------------------
57  
58 16 ▶sub setdevicedescription() {
Subroutine setdevicedescription: called CC=1 SCAR=0.0
Pod undocumented
59 16 my($this) = shift;
60 16 $this->{$Net::UPnP::Service::_DEVICE_DESCRIPTION} = $_[0];
61 }
62  
63 85 ▶sub getdevicedescription() {
Subroutine getdevicedescription: called CC=3 SCAR=12.3
Pod documented
64 85 my($this) = shift;
65 85 my %args = (
66 name => undef,
67 @_,
68 );
69 85 ▶ if ($args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"name"}) 85 0 85
70 85 ▶ unless ($this->{$Net::UPnP::Service::_DEVICE_DESCRIPTION} =~ m/<$args{name}>(.*)<\/$args{name}>/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{$Net::UPnP::Service::_DEVICE_DESCRIPTION} =~ m[<$args{"name"}>(.*)</$args{"name"}>]i) 0 85 85
71 0 return '';
72 }
73 85 return $1;
74 }
75 0 $this->{$Net::UPnP::Service::_DEVICE_DESCRIPTION};
76 }
77  
78 #------------------------------
79 # getservicetype
80 #------------------------------
81  
82 66 ▶sub getservicetype() {
Subroutine getservicetype: called CC=1 SCAR=0.0
Pod documented
83 66 my($this) = shift;
84 66 $this->getdevicedescription(name => $Net::UPnP::Service::SERVICETYPE);
85 }
86  
87 #------------------------------
88 # getserviceid
89 #------------------------------
90  
91 0 ▶sub getserviceid() {
Subroutine getserviceid: not called CC=1 SCAR=6.9
Pod documented
92 0 my($this) = shift;
93 0 $this->getdevicedescription(name => $Net::UPnP::Service::SERVICEID);
94 }
95  
96 #------------------------------
97 # getscpdurl
98 #------------------------------
99  
100 0 ▶sub getscpdurl() {
Subroutine getscpdurl: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my($this) = shift;
102 0 $this->getdevicedescription(name => $Net::UPnP::Service::SCPDURL);
103 }
104  
105 #------------------------------
106 # getcontrolurl
107 #------------------------------
108  
109 19 ▶sub getcontrolurl() {
Subroutine getcontrolurl: called CC=1 SCAR=0.0
Pod undocumented
110 19 my($this) = shift;
111 19 $this->getdevicedescription(name => $Net::UPnP::Service::CONTROLURL);
112 }
113  
114 #------------------------------
115 # geteventsuburl
116 #------------------------------
117  
118 0 ▶sub geteventsuburl() {
Subroutine geteventsuburl: not called CC=1 SCAR=6.9
Pod undocumented
119 0 my($this) = shift;
120 0 $this->getdevicedescription(name => $Net::UPnP::Service::EVENTSUBURL);
121 }
122  
123 #------------------------------
124 # getposturl
125 #------------------------------
126  
127 22 ▶sub getposturl() {
Subroutine getposturl: called CC=7 SCAR=19.5
Pod undocumented
128 22 my($this) = shift;
129 22 my ($ctrl_url) = @_;
130 my (
131 22 $dev,
132 $location_url,
133 $url_base,
134 );
135
136 22 $dev = $this->getdevice();
137
138 22 $location_url = $dev->getlocation();
139 22 $url_base = $dev->geturlbase();
140 # Use the caller's URL when given; fall back to the service's
141 # controlURL only when none is supplied (RT #21076).
142 22 ▶ $ctrl_url = $this->getcontrolurl() unless defined($ctrl_url) && length($ctrl_url);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctrl_url and length $ctrl_url 7 15 22
Condition100%3/3
defined $ctrl_url and length $ctrl_url
!ll&&!rl&&r
6115
MC/DC100%2/2
defined $ctrl_url and length $ctrl_url
defined $ctrl_url length $ctrl_url
Truth table100%3/3
A and B
A= defined $ctrl_urlB= length $ctrl_url
ABresult
0X0
100
111
143  
144 #print "$location_url\n";
145 #print "$url_base\n";
146 #print "$ctrl_url\n";
147
148 22 ▶ unless ($ctrl_url =~ m{http://(.*)}i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctrl_url =~ m[http://(.*)]i) 17 5 22
149 17 ▶ if (0 < length($url_base)) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < length $url_base) { } 6 11 17
elsif ($location_url =~ m[http://([0-9a-z.-]+)(?::(\d+))?(?:/|\z)]i) { } 11 0 11
150 # Thanks for Thus0 (2005/01/12)
151 6 $ctrl_url = _joinurl($url_base, $ctrl_url);
152 }
153 elsif ($location_url =~ m{http://([0-9a-z.-]+)(?::(\d+))?(?:/|\z)}i) {
154 # A relative controlURL is resolved against the host of the
155 # description URL, always separated by exactly one "/"
156 # (RT #21076).
157 11 ▶ my $port = $2 || 80;
Condition100%2/2
$2 || 80
l!l
65
MC/DC100%1/1
$2 || 80
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
158 11 $ctrl_url = _joinurl('http://' . $1 . ':' . $port, $ctrl_url);
159 }
160 else {
161 0 $ctrl_url = _joinurl($location_url, $ctrl_url);
162 }
163 }
164
165 22 return $ctrl_url;
166 }
167  
168 17 ▶sub _joinurl {
Subroutine _joinurl: called CC=7 SCAR=20.2
169 17 my ($base, $path) = @_;
170 17 ▶ return $base unless defined($path) && length($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 17 17
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
171 17 my $base_slash = substr($base, -1) eq '/';
172 17 my $path_slash = index($path, '/') == 0;
173 17 ▶ return $base . substr($path, 1) if $base_slash && $path_slash;
Branch: 1 branch
BranchTrueFalseTotal
if $base_slash and $path_slash 5 12 17
Condition66%2/3
$base_slash and $path_slash
!ll&&!rl&&r
1205
MC/DC50%1/2
$base_slash and $path_slash
$base_slash $path_slash
Truth table66%2/3
A and B
A= $base_slashB= $path_slash
ABresult
0X0
100
111
174 12 ▶ return $base . $path if $base_slash || $path_slash;
Branch: 1 branch
BranchTrueFalseTotal
if $base_slash or $path_slash 8 4 12
Condition66%2/3
$base_slash or $path_slash
l!l&&r!l&&!r
084
MC/DC50%1/2
$base_slash or $path_slash
$base_slash $path_slash
Truth table66%2/3
A or B
A= $base_slashB= $path_slash
ABresult
1X1
011
000
175 4 return $base . '/' . $path;
176 }
177  
178 #------------------------------
179 # postaction
180 #------------------------------
181  
182 9 ▶sub postaction() {
Subroutine postaction: called CC=10 SCAR=24.9
Pod documented
183 9 my($this) = shift;
184 9 my ($action_name, $action_arg, $http_options) = @_;
185 my (
186 9 $dev,
187 $ctrl_url,
188 $service_type,
189 $soap_action,
190 $soap_content,
191 $arg_name,
192 $arg_value,
193 $post_addr,
194 $post_port,
195 $post_path,
196 $http_req,
197 $post_res,
198 $action_res,
199 $key,
200 );
201
202 9 $action_res = Net::UPnP::ActionResponse->new();
203
204 9 $dev = $this->getdevice();
205 9 $ctrl_url = $this->getcontrolurl();
206 9 $ctrl_url = $this->getposturl($ctrl_url);
207
208 9 ▶ unless ($ctrl_url =~ m{http://([0-9a-z.-]+)(?::(\d+))?/(.*)}i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctrl_url =~ m[http://([0-9a-z.-]+)(?::(\d+))?/(.*)]i) 0 9 9
209 #print "Invalid URL : $ctrl_url\n";
210 0 $post_res = Net::UPnP::HTTPResponse->new();
211 0 $action_res->sethttpresponse($post_res);
212 0 return $action_res;
213 }
214 9 $post_addr = $1;
215 9 ▶ $post_port = $2 || 80;
Condition100%2/2
$2 || 80
l!l
36
MC/DC100%1/1
$2 || 80
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
216 9 ▶ if (index($3, '/') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($3, '/') == 0) { } 0 9 9
217 0 $post_path = $3;
218 }
219 else {
220 9 $post_path = '/' . $3;
221 }
222  
223 9 $service_type = $this->getservicetype();
224 9 $soap_action = '"' . $service_type . '#' . $action_name . '"';
225  
226  
227 9 $soap_content = <<"SOAP_CONTENT";
228 <?xml version=\"1.0\" encoding=\"utf-8\"?>
229 <s:Envelope xmlns:s=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" s:encodingStyle=\"http:\/\/schemas.xmlsoap.org\/soap\/encoding/\">
230 \t<s:Body>
231 \t\t<u:$action_name xmlns:u=\"$service_type\">
232 SOAP_CONTENT
233  
234 9 ▶ if (ref $action_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $action_arg) 9 0 9
235 9 ▶ while (($arg_name, $arg_value) = each (%{$action_arg} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$action_arg;}) 0 9 9
236 0 ▶ if (length($arg_value) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $arg_value <= 0) 0 0 0
237 0 $soap_content .= "\t\t\t<$arg_name \/>\n";
238 0 next;
239 }
240 0 $soap_content .= "\t\t\t<$arg_name>$arg_value<\/$arg_name>\n";
241 }
242 }
243  
244 9 $soap_content .= <<"SOAP_CONTENT";
245 \t\t</u:$action_name>
246 \t</s:Body>
247 </s:Envelope>
248 SOAP_CONTENT
249  
250 9 ▶ my %options = %{$http_options || {}};
Condition100%2/2
$http_options || {}
l!l
27
MC/DC100%1/1
$http_options || {}
$http_options
Truth table100%2/2
A
A= $http_options
Aresult
11
00
251 9 my $allowed = $dev->getallowedaddresses();
252 9 ▶ $options{allowed_addresses} = $allowed if defined $allowed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $allowed 7 2 9
253 9 $http_req = Net::UPnP::HTTP->new(%options);
254 9 $post_res = $http_req->postsoap($post_addr, $post_port, $post_path, $soap_action, $soap_content);
255 9 ▶ $post_res ||= Net::UPnP::HTTPResponse->new();
Condition66%2/3
$post_res ||= "Net::UPnP::HTTPResponse"->new
l!l&&r!l&&!r
180
MC/DC0%0/2
$post_res ||= "Net::UPnP::HTTPResponse"->new
$post_res "Net::UPnP::HTTPResponse"->new
Truth table66%2/3
A ||= B
A= $post_resB= "Net::UPnP::HTTPResponse"->new
ABresult
1X1
011
000
256  
257 9 $action_res->sethttpresponse($post_res);
258
259 9 return $action_res;
260 }
261  
262 #------------------------------
263 # postcontrol
264 #------------------------------
265  
266 3 ▶sub postcontrol() {
Subroutine postcontrol: called CC=1 SCAR=0.0
Pod undocumented
267 3 my($this) = shift;
268 3 return $this->postaction(@_);
269 }
270  
271 #------------------------------
272 # postquery
273 #------------------------------
274  
275 3 ▶sub postquery() {
Subroutine postquery: called CC=7 SCAR=20.3
Pod documented
276 3 my($this) = shift;
277 3 my ($var_name) = @_;
278 my (
279 3 $dev,
280 $ctrl_url,
281 $service_type,
282 $soap_action,
283 $soap_content,
284 $post_addr,
285 $post_port,
286 $post_path,
287 $http_req,
288 $post_res,
289 $query_res,
290 );
291
292 3 $query_res = Net::UPnP::QueryResponse->new();
293
294 3 $dev = $this->getdevice();
295 3 $ctrl_url = $this->getcontrolurl();
296 3 $ctrl_url = $this->getposturl($ctrl_url);
297
298 3 ▶ print "ctrl_url=$ctrl_url\n" if ($Net::UPnP::DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $Net::UPnP::DEBUG 0 3 3
299 3 ▶ unless ($ctrl_url =~ m{http://([0-9a-z.-]+)(?::(\d+))?/(.*)}i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctrl_url =~ m[http://([0-9a-z.-]+)(?::(\d+))?/(.*)]i) 0 3 3
300 0 $post_res = Net::UPnP::HTTPResponse->new();
301 0 $query_res->sethttpresponse($post_res);
302 0 return $query_res;
303 }
304 3 $post_addr = $1;
305 3 ▶ $post_port = $2 || 80;
Condition100%2/2
$2 || 80
l!l
12
MC/DC100%1/1
$2 || 80
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
306 3 ▶ if (index($3, '/') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($3, '/') == 0) { } 0 3 3
307 0 $post_path = $3;
308 }
309 else {
310 3 $post_path = '/' . $3;
311 }
312 3 $service_type = $this->getservicetype();
313 3 $soap_action = '"urn:schemas-upnp-org:control-1-0#QueryStateVariable"';
314  
315  
316 3 $soap_content = <<"SOAP_CONTENT";
317 <?xml version=\"1.0\" encoding=\"utf-8\"?>
318 <s:Envelope xmlns:s=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" s:encodingStyle=\"http:\/\/schemas.xmlsoap.org\/soap\/encoding/\">
319 \t<s:Body>
320 \t\t<u:QueryStateVariable xmlns:u=\"urn:schemas-upnp-org:control-1-0\">
321 \t\t\t<u:varName>$var_name</u:varName>
322 \t\t</u:QueryStateVariable>
323 \t</s:Body>
324 </s:Envelope>
325 SOAP_CONTENT
326  
327 3 my $allowed = $dev->getallowedaddresses();
328 3 ▶ $http_req = Net::UPnP::HTTP->new(defined($allowed) ? (allowed_addresses => $allowed) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $allowed ? : 2 1 3
329 3 $post_res = $http_req->postsoap($post_addr, $post_port, $post_path, $soap_action, $soap_content);
330 3 ▶ $post_res ||= Net::UPnP::HTTPResponse->new();
Condition33%1/3
$post_res ||= "Net::UPnP::HTTPResponse"->new
l!l&&r!l&&!r
030
MC/DC0%0/2
$post_res ||= "Net::UPnP::HTTPResponse"->new
$post_res "Net::UPnP::HTTPResponse"->new
Truth table33%1/3
A ||= B
A= $post_resB= "Net::UPnP::HTTPResponse"->new
ABresult
1X1
011
000
331  
332 3 $query_res->sethttpresponse($post_res);
333
334 3 return $query_res;
335 }
336  
337 8 1;
338  
339 __END__
« blib/lib/Net/UPnP/GW/Gateway.pm ↑ Summary blib/lib/Net/UPnP/AV/RSS.pm »