Lim/RPC/Protocol/XMLRPC.pm

filter: Statementstmt 80.4% 107 / 133 Branchbran 42.8% 18 / 42 Conditioncond 33.3% 12 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 95.2% 20 / 21 Podpod 100% 6 / 6 totaltotal 63.1% 163 / 258 CC 36 SCAR 25.4
CC
36
Cov
65%
precall
26.0
handle
21.1
__xmlrpc_result
15.0
CRAP 12.6
« Lim/Component/CLI.pm ↑ Summary Lim/Agent/CLI.pm »
1 package Lim::RPC::Protocol::XMLRPC;
2  
3 5 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use XMLRPC::Lite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use XMLRPC::Transport::HTTP::Server ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Lim::RPC::Callback ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use base qw(Lim::RPC::Protocol);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 ...
25  
26 =head1 VERSION
27  
28 See L<Lim> for version.
29  
30 =cut
31  
32 5 our $VERSION = $Lim::VERSION;
33  
34 =head1 SYNOPSIS
35  
36 ...
37  
38 =head1 SUBROUTINES/METHODS
39  
40 =head2 Init
41  
42 =cut
43  
44 1 ▶sub Init {
Subroutine Init: called CC=1 SCAR=0.0
Pod documented
45 }
46  
47 =head2 Destroy
48  
49 =cut
50  
51 1 ▶sub Destroy {
Subroutine Destroy: called CC=1 SCAR=0.0
Pod documented
52 1 my ($self) = @_;
53
54 1 delete $self->{xmlrpc};
55 }
56  
57 =head2 name
58  
59 =cut
60  
61 9 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
62 9 'xmlrpc';
63 }
64  
65 =head2 serve
66  
67 =cut
68  
69 1 ▶sub serve {
Subroutine serve: called CC=2 SCAR=7.0
Pod documented
70 1 my ($self, $module, $module_shortname) = @_;
71 1 my ($calls, $tns, $xmlrpc, $obj, $obj_class);
72
73 1 $calls = $module->Calls;
74 1 $tns = $module.'::Server';
75  
76 1 $xmlrpc = XMLRPC::Transport::HTTP::Server->new;
77 1 $obj = $self->server->module_obj_by_protocol($module_shortname, $self->name);
78 1 $obj_class = ref($obj);
79 # TODO: check if $obj_class alread is a XMLRPC::Server::Parameters
80 1 eval "push(\@${obj_class}::ISA, 'XMLRPC::Server::Parameters');";
81 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
82 0 die $@;
83 }
84 1 $self->{xmlrpc}->{$module} = $xmlrpc;
85 }
86  
87 =head2 handle
88  
89 =cut
90  
91 1 ▶sub handle {
Subroutine handle: called CC=12 SCAR=30.5
Pod documented
92 1 my ($self, $cb, $request, $transport) = @_;
93
94 1 ▶ unless (blessed($request) and $request->isa('HTTP::Request')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($request) and $request->isa("HTTP::Request")) 0 1 1
Condition33%1/3
builtin::blessed($request) and $request->isa("HTTP::Request")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($request) and $request->isa("HTTP::Request")
builtin::blessed($request) $request->isa("HTTP::Request")
Truth table33%1/3
A and B
A= builtin::blessed($request)B= $request->isa("HTTP::Request")
ABresult
0X0
100
111
95 0 return;
96 }
97  
98 1 ▶ if ($request->header('Content-Type') =~ /(?:^|\s)text\/xml(?:$|\s|;)/o and $request->uri =~ /^\/([a-zA-Z]+)\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->header("Content-Type") =~ m[(?:^|\s)text/xml(?:$|\s|;)]ou and $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou) 1 0 1
Condition33%1/3
$request->header("Content-Type") =~ m[(?:^|\s)text/xml(?:$|\s|;)]ou and $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou
!ll&&!rl&&r
001
MC/DC0%0/2
$request->header("Content-Type") =~ m[(?:^|\s)text/xml(?:$|\s|;)]ou and $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou
$request->header("Content-Type") =~ m[(?:^|\s)text/xml(?:$|\s|;)]ou $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou
Truth table33%1/3
A and B
A= $request->header("Content-Type") =~ m[(?:^|\s)text/xml(?:$|\s|;)]ouB= $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou
ABresult
0X0
100
111
99 1 my ($module) = ($1);
100 1 my $response = HTTP::Response->new;
101 1 my $http_request = $request;
102 1 $response->request($request);
103 1 $response->protocol($request->protocol);
104
105 1 $module = lc($module);
106 1 my $server = $self->server;
107 1 ▶ if (defined $server and $server->have_module($module) and exists $self->{xmlrpc}->{$server->module_class($module)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $server and $server->have_module($module) and exists $self->{'xmlrpc'}{$server->module_class($module)}) { } 1 0 1
Condition33%2/6
defined $server and $server->have_module($module) and exists $self->{"xmlrpc"}{$server->module_class($module)}
!ll&&!rl&&r
001
defined $server and $server->have_module($module)
!ll&&!rl&&r
001
MC/DC0%0/3
defined $server and $server->have_module($module) and exists $self->{"xmlrpc"}{$server->module_class($module)}
defined $server $server->have_module($module) exists $self->{"xmlrpc"}{$server->module_class($module)}
Truth table25%1/4
A and B and C
A= defined $serverB= $server->have_module($module)C= exists $self->{"xmlrpc"}{$server->module_class($module)}
ABCresult
0XX0
10X0
1100
1111
108 1 my ($action, $method_uri, $method_name);
109 1 my $real_self = $self;
110 1 my $xmlrpc = $self->{xmlrpc}->{$server->module_class($module)};
111 1 my $protocol_obj = $server->module_obj_by_protocol($module, $self->name);
112 1 weaken($self);
113 1 weaken($xmlrpc);
114  
115 1 $method_uri = 'urn:'.ref($protocol_obj);
116  
117 1 ▶ Lim::RPC_DEBUG and $self->{logger}->debug('XMLRPC dispatch to module ', $server->module_class($module), ' obj ', $server->module_obj($module), ' proto obj ', $protocol_obj);
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug("XMLRPC dispatch to module ", $server->module_class($module), " obj ", $server->module_obj($module), " proto obj ", $protocol_obj) 1 0 1
118  
119 1 ▶ $xmlrpc->on_dispatch(sub {
Subroutine __ANON__: called CC=4 SCAR=16.6
120 1 my ($request) = @_;
121
122 1 ▶ unless (defined $self and defined $xmlrpc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and defined $xmlrpc) 0 1 1
Condition33%1/3
defined $self and defined $xmlrpc
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self and defined $xmlrpc
defined $self defined $xmlrpc
Truth table33%1/3
A and B
A= defined $selfB= defined $xmlrpc
ABresult
0X0
100
111
123 0 return;
124 }
125
126 $request->{__lim_rpc_protocol_xmlrpc_cb} = Lim::RPC::Callback->new(
127 request => $http_request,
128 1 ▶ cb => sub {
Subroutine __ANON__: called CC=6 SCAR=20.9
129 1 my ($data) = @_;
130
131 1 ▶ unless (defined $self and defined $xmlrpc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and defined $xmlrpc) 0 1 1
Condition33%1/3
defined $self and defined $xmlrpc
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self and defined $xmlrpc
defined $self defined $xmlrpc
Truth table33%1/3
A and B
A= defined $selfB= defined $xmlrpc
ABresult
0X0
100
111
132 0 return;
133 }
134
135 1 ▶ if (blessed $data and $data->isa('Lim::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data) and $data->isa('Lim::Error')) { } 0 1 1
Condition33%1/3
builtin::blessed($data) and $data->isa("Lim::Error")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($data) and $data->isa("Lim::Error")
builtin::blessed($data) $data->isa("Lim::Error")
Truth table33%1/3
A and B
A= builtin::blessed($data)B= $data->isa("Lim::Error")
ABresult
0X0
100
111
136 0 $xmlrpc->make_fault($data->code, $data->message);
137 }
138 else {
139 1 my $result;
140
141 1 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 1 0 1
142 1 $result = $xmlrpc->serializer
143 ->prefix('s')
144 ->uri($method_uri)
145 ->envelope(response => $method_name . 'Response', __xmlrpc_result('base', $data));
146 }
147 else {
148 0 $result = $xmlrpc->serializer
149 ->prefix('s')
150 ->uri($method_uri)
151 ->envelope(response => $method_name . 'Response');
152 }
153
154 1 $xmlrpc->make_response($XMLRPC::Constants::HTTP_ON_SUCCESS_CODE, $result);
155 }
156
157 1 $response = $xmlrpc->response;
158 1 $response->header(
159 'Cache-Control' => 'no-cache',
160 'Pragma' => 'no-cache'
161 );
162
163 1 $cb->cb->($response);
164 1 return;
165 },
166 0 ▶ reset_timeout => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 0 $cb->reset_timeout;
168 1 });
169  
170 1 ▶ unless ($request->method =~ /^\w+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->method =~ /^\w+$/ou) 0 1 1
171 0 $request->{__lim_rpc_protocol_xmlrpc_cb}->(Lim::Error->new(500, 'Invalid characters in method name'));
172 0 return;
173 }
174  
175 1 return ($method_uri, ($method_name = $request->method));
176 1 });
177  
178 1 $xmlrpc->dispatch_to($protocol_obj);
179  
180 1 eval {
181 1 $xmlrpc->request($request);
182 1 $xmlrpc->handle;
183 };
184 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
185 0 ▶ Lim::WARN and $self->{logger}->warn('XMLRPC action failed: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("XMLRPC action failed: ", $@) 0 0 0
186 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
187 }
188 else {
189 1 ▶ if ($xmlrpc->response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlrpc->response) 1 0 1
190 1 $cb->cb->($xmlrpc->response);
191 }
192 1 return 1;
193 }
194 }
195 else {
196 0 return;
197 }
198  
199 0 $cb->cb->($response);
200 0 return 1;
201 }
202 0 return;
203 }
204  
205 =head2 __xmlrpc_result
206  
207 =cut
208  
209 1 ▶sub __xmlrpc_result {
Subroutine __xmlrpc_result: called CC=7 SCAR=27.0
210 1 my @a;
211
212 1 foreach my $k (keys %{$_[1]}) {
213 1 ▶ if (ref($_[1]->{$k}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[1]{$k} eq 'ARRAY') { } 0 1 1
elsif (ref $_[1]{$k} eq 'HASH') { } 0 1 1
214 0 foreach my $v (@{$_[1]->{$k}}) {
215 0 ▶ if (ref($v) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'HASH') { } 0 0 0
216 0 push(@a,
217 XMLRPC::Data->new->value({ $k => Lim::RPC::__xmlrpc_result($_[0].'.'.$k, $v) })
218 );
219 }
220 else {
221 0 push(@a,
222 XMLRPC::Data->new->value({ $k => $v })
223 );
224 }
225 }
226 }
227 elsif (ref($_[1]->{$k}) eq 'HASH') {
228 push(@a,
229 0 XMLRPC::Data->new->value({ $k => Lim::RPC::__xmlrpc_result($_[0].'.'.$k, $_[1]->{$k}) })
230 );
231 }
232 else {
233 push(@a,
234 1 XMLRPC::Data->new->value({ $k => $_[1]->{$k} })
235 );
236 }
237 }
238  
239 1 ▶ if ($_[0] eq 'base') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq 'base') { } 1 0 1
240 1 return @a;
241 }
242 else {
243 0 return \@a;
244 }
245 }
246  
247 =head2 precall
248  
249 =cut
250  
251 1 ▶sub precall {
Subroutine precall: called CC=10 SCAR=32.6
Pod documented
252 1 my ($self, $call, $object, $som) = @_;
253
254 1 ▶ unless (ref($call) eq '' and blessed($object) and blessed($som) and $som->isa('XMLRPC::SOM')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $call eq "" and builtin::blessed($object) and builtin::blessed($som) and $som->isa("XMLRPC::SOM")) 0 1 1
Condition33%3/9
ref $call eq "" and builtin::blessed($object) and builtin::blessed($som) and $som->isa("XMLRPC::SOM")
!ll&&!rl&&r
001
ref $call eq "" and builtin::blessed($object) and builtin::blessed($som)
!ll&&!rl&&r
001
ref $call eq "" and builtin::blessed($object)
!ll&&!rl&&r
001
MC/DC0%0/4
ref $call eq "" and builtin::blessed($object) and builtin::blessed($som) and $som->isa("XMLRPC::SOM")
ref $call eq "" builtin::blessed($object) builtin::blessed($som) $som->isa("XMLRPC::SOM")
Truth table20%1/5
A and B and C and D
A= ref $call eq ""B= builtin::blessed($object)C= builtin::blessed($som)D= $som->isa("XMLRPC::SOM")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
255 0 confess __PACKAGE__, ': Invalid XMLRPC call';
256 }
257  
258 1 ▶ unless (exists $som->{__lim_rpc_protocol_xmlrpc_cb} and blessed($som->{__lim_rpc_protocol_xmlrpc_cb}) and $som->{__lim_rpc_protocol_xmlrpc_cb}->isa('Lim::RPC::Callback')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $som->{"__lim_rpc_protocol_xmlrpc_cb"} and builtin::blessed($som->{"__lim_rpc_protocol_xmlrpc_cb"}) and $som->{"__lim_rpc_protocol_xmlrpc_cb"}->isa("Lim::RPC::Callback")) 0 1 1
Condition33%2/6
exists $som->{"__lim_rpc_protocol_xmlrpc_cb"} and builtin::blessed($som->{"__lim_rpc_protocol_xmlrpc_cb"}) and $som->{"__lim_rpc_protocol_xmlrpc_cb"}->isa("Lim::RPC::Callback")
!ll&&!rl&&r
001
exists $som->{"__lim_rpc_protocol_xmlrpc_cb"} and builtin::blessed($som->{"__lim_rpc_protocol_xmlrpc_cb"})
!ll&&!rl&&r
001
MC/DC0%0/3
exists $som->{"__lim_rpc_protocol_xmlrpc_cb"} and builtin::blessed($som->{"__lim_rpc_protocol_xmlrpc_cb"}) and $som->{"__lim_rpc_protocol_xmlrpc_cb"}->isa("Lim::RPC::Callback")
exists $som->{"__lim_rpc_protocol_xmlrpc_cb"} builtin::blessed($som->{"__lim_rpc_protocol_xmlrpc_cb"}) $som->{"__lim_rpc_protocol_xmlrpc_cb"}->isa("Lim::RPC::Callback")
Truth table25%1/4
A and B and C
A= exists $som->{"__lim_rpc_protocol_xmlrpc_cb"}B= builtin::blessed($som->{"__lim_rpc_protocol_xmlrpc_cb"})C= $som->{"__lim_rpc_protocol_xmlrpc_cb"}->isa("Lim::RPC::Callback")
ABCresult
0XX0
10X0
1100
1111
259 0 confess __PACKAGE__, ': XMLRPC::SOM does not contain lim rpc callback or invalid';
260 }
261 1 my $cb = delete $som->{__lim_rpc_protocol_xmlrpc_cb};
262 1 my $valueof = $som->valueof('//'.$call.'/');
263
264 1 ▶ if ($valueof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valueof) { } 0 1 1
265 0 ▶ unless (ref($valueof) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $valueof eq "HASH") 0 0 0
266 0 confess __PACKAGE__, ': Invalid data in XMLRPC call';
267 }
268 }
269 else {
270 1 undef($valueof);
271 }
272  
273 1 return ($object, $cb, $valueof);
274 }
275  
276 =head1 AUTHOR
277  
278 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
279  
280 =head1 BUGS
281  
282 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
283  
284 =head1 SUPPORT
285  
286 You can find documentation for this module with the perldoc command.
287  
288 perldoc Lim
289  
290 You can also look for information at:
291  
292 =over 4
293  
294 =item * Lim issue tracker (report bugs here)
295  
296 L<https://github.com/jelu/lim/issues>
297  
298 =back
299  
300 =head1 ACKNOWLEDGEMENTS
301  
302 =head1 LICENSE AND COPYRIGHT
303  
304 Copyright 2012-2013 Jerry Lundström.
305  
306 This program is free software; you can redistribute it and/or modify it
307 under the terms of either: the GNU General Public License as published
308 by the Free Software Foundation; or the Artistic License.
309  
310 See http://dev.perl.org/licenses/ for more information.
311  
312  
313 =cut
314  
315 5 1; # End of Lim::RPC::Protocol::XMLRPC
« Lim/Component/CLI.pm ↑ Summary Lim/Agent/CLI.pm »