Lim/RPC/Protocol/XMLRPC.pm

filter: Statementstmt 80.3% 106 / 132 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.0% 162 / 257 SCAR 45.3
CC
36
Cov
65%
precall
26.0
handle
21.4
__xmlrpc_result
15.0
CRAP 92.7
« Lim/RPC/Protocol/JSONRPC1.pm ↑ Summary Lim/RPC/URIMaps.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 ▶sub Init {
Subroutine Init: called CC=1 SCAR=0.0
Pod documented
45 }
46  
47 =head2 Destroy
48  
49 =cut
50  
51 sub Destroy {
52 1 ▶ my ($self) = @_;
Subroutine Destroy: called CC=1 SCAR=0.0
Pod documented
53
54 1 delete $self->{xmlrpc};
55 }
56  
57 =head2 name
58  
59 =cut
60  
61 sub name {
62 8 ▶ 'xmlrpc';
Subroutine name: called CC=1 SCAR=0.0
Pod documented
63 }
64  
65 =head2 serve
66  
67 =cut
68  
69 sub serve {
70 1 ▶ my ($self, $module, $module_shortname) = @_;
Subroutine serve: called CC=2 SCAR=7.0
Pod documented
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 sub handle {
92 1 ▶ my ($self, $cb, $request, $transport) = @_;
Subroutine handle: called CC=12 SCAR=30.6
Pod documented
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 $response->request($request);
102 1 $response->protocol($request->protocol);
103
104 1 $module = lc($module);
105 1 my $server = $self->server;
106 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
107 1 my ($action, $method_uri, $method_name);
108 1 my $real_self = $self;
109 1 my $xmlrpc = $self->{xmlrpc}->{$server->module_class($module)};
110 1 my $protocol_obj = $server->module_obj_by_protocol($module, $self->name);
111 1 weaken($self);
112 1 weaken($xmlrpc);
113  
114 1 $method_uri = 'urn:'.ref($protocol_obj);
115  
116 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
117  
118 $xmlrpc->on_dispatch(sub {
119 1 ▶ my ($request) = @_;
Subroutine __ANON__: called CC=4 SCAR=16.6
120
121 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
122 0 return;
123 }
124
125 $request->{__lim_rpc_protocol_xmlrpc_cb} = Lim::RPC::Callback->new(
126 cb => sub {
127 1 ▶ my ($data) = @_;
Subroutine __ANON__: called CC=6 SCAR=20.9
128
129 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
130 0 return;
131 }
132
133 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
134 0 $xmlrpc->make_fault($data->code, $data->message);
135 }
136 else {
137 1 my $result;
138
139 1 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 1 0 1
140 1 $result = $xmlrpc->serializer
141 ->prefix('s')
142 ->uri($method_uri)
143 ->envelope(response => $method_name . 'Response', __xmlrpc_result('base', $data));
144 }
145 else {
146 0 $result = $xmlrpc->serializer
147 ->prefix('s')
148 ->uri($method_uri)
149 ->envelope(response => $method_name . 'Response');
150 }
151
152 1 $xmlrpc->make_response($XMLRPC::Constants::HTTP_ON_SUCCESS_CODE, $result);
153 }
154
155 1 $response = $xmlrpc->response;
156 1 $response->header(
157 'Cache-Control' => 'no-cache',
158 'Pragma' => 'no-cache'
159 );
160
161 1 $cb->cb->($response);
162 1 return;
163 },
164 reset_timeout => sub {
165 0 ▶ $cb->reset_timeout;
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 1 });
167  
168 1 ▶ unless ($request->method =~ /^\w+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->method =~ /^\w+$/ou) 0 1 1
169 0 $request->{__lim_rpc_protocol_xmlrpc_cb}->(Lim::Error->new(500, 'Invalid characters in method name'));
170 0 return;
171 }
172  
173 1 return ($method_uri, ($method_name = $request->method));
174 1 });
175  
176 1 $xmlrpc->dispatch_to($protocol_obj);
177  
178 1 eval {
179 1 $xmlrpc->request($request);
180 1 $xmlrpc->handle;
181 };
182 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
183 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
184 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
185 }
186 else {
187 1 ▶ if ($xmlrpc->response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlrpc->response) 1 0 1
188 1 $cb->cb->($xmlrpc->response);
189 }
190 1 return 1;
191 }
192 }
193 else {
194 0 return;
195 }
196  
197 0 $cb->cb->($response);
198 0 return 1;
199 }
200 0 return;
201 }
202  
203 =head2 __xmlrpc_result
204  
205 =cut
206  
207 sub __xmlrpc_result {
208 1 ▶ my @a;
Subroutine __xmlrpc_result: called CC=7 SCAR=27.0
209
210 1 foreach my $k (keys %{$_[1]}) {
211 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
212 0 foreach my $v (@{$_[1]->{$k}}) {
213 0 ▶ if (ref($v) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'HASH') { } 0 0 0
214 0 push(@a,
215 XMLRPC::Data->new->value({ $k => Lim::RPC::__xmlrpc_result($_[0].'.'.$k, $v) })
216 );
217 }
218 else {
219 0 push(@a,
220 XMLRPC::Data->new->value({ $k => $v })
221 );
222 }
223 }
224 }
225 elsif (ref($_[1]->{$k}) eq 'HASH') {
226 push(@a,
227 0 XMLRPC::Data->new->value({ $k => Lim::RPC::__xmlrpc_result($_[0].'.'.$k, $_[1]->{$k}) })
228 );
229 }
230 else {
231 push(@a,
232 1 XMLRPC::Data->new->value({ $k => $_[1]->{$k} })
233 );
234 }
235 }
236  
237 1 ▶ if ($_[0] eq 'base') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq 'base') { } 1 0 1
238 1 return @a;
239 }
240 else {
241 0 return \@a;
242 }
243 }
244  
245 =head2 precall
246  
247 =cut
248  
249 sub precall {
250 1 ▶ my ($self, $call, $object, $som) = @_;
Subroutine precall: called CC=10 SCAR=32.6
Pod documented
251
252 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
253 0 confess __PACKAGE__, ': Invalid XMLRPC call';
254 }
255  
256 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
257 0 confess __PACKAGE__, ': XMLRPC::SOM does not contain lim rpc callback or invalid';
258 }
259 1 my $cb = delete $som->{__lim_rpc_protocol_xmlrpc_cb};
260 1 my $valueof = $som->valueof('//'.$call.'/');
261
262 1 ▶ if ($valueof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valueof) { } 0 1 1
263 0 ▶ unless (ref($valueof) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $valueof eq "HASH") 0 0 0
264 0 confess __PACKAGE__, ': Invalid data in XMLRPC call';
265 }
266 }
267 else {
268 1 undef($valueof);
269 }
270  
271 1 return ($object, $cb, $valueof);
272 }
273  
274 =head1 AUTHOR
275  
276 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
277  
278 =head1 BUGS
279  
280 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
281  
282 =head1 SUPPORT
283  
284 You can find documentation for this module with the perldoc command.
285  
286 perldoc Lim
287  
288 You can also look for information at:
289  
290 =over 4
291  
292 =item * Lim issue tracker (report bugs here)
293  
294 L<https://github.com/jelu/lim/issues>
295  
296 =back
297  
298 =head1 ACKNOWLEDGEMENTS
299  
300 =head1 LICENSE AND COPYRIGHT
301  
302 Copyright 2012-2013 Jerry Lundström.
303  
304 This program is free software; you can redistribute it and/or modify it
305 under the terms of either: the GNU General Public License as published
306 by the Free Software Foundation; or the Artistic License.
307  
308 See http://dev.perl.org/licenses/ for more information.
309  
310  
311 =cut
312  
313 5 1; # End of Lim::RPC::Protocol::XMLRPC
« Lim/RPC/Protocol/JSONRPC1.pm ↑ Summary Lim/RPC/URIMaps.pm »