lib/MojoX/JSON/RPC/Dispatcher.pm

filter: Statementstmt 88.3% 99 / 112 Branchbran 80.9% 68 / 84 Conditioncond 76.0% 38 / 50 MC/DCmcdc 48.2% 14 / 29 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 80.2% 228 / 284 CC 62 SCAR 35.1
CC
62
Cov
83%
_acquire_methods
49.2
_handle_methods
25.0
call
4.0
CRAP 33.4
↑ Summary lib/MojoX/JSON/RPC/Client.pm »
1 package MojoX::JSON::RPC::Dispatcher;
2  
3 3 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mojo::JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use MojoX::JSON::RPC::Dispatcher::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use MojoX::JSON::RPC::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # process JSON-RPC call
9 37 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
10 37 my ($self) = @_;
11  
12 37 my $rpc_response = $self->_handle_methods( $self->_acquire_methods );
13 37 ▶ if ( !$rpc_response ) { # is notification
Branch: 1 branch
BranchTrueFalseTotal
unless ($rpc_response) 3 34 37
14 3 $self->tx->res->code(204);
15 3 return $self->rendered;
16 }
17 34 my $res = $self->tx->res;
18 $res->code(
19 $self->_translate_error_code_to_status(
20 ref $rpc_response eq 'HASH' && exists $rpc_response->{error}
21 ? $rpc_response->{error}->{code}
22 34 ▶ : q{}
Branch: 1 branch
BranchTrueFalseTotal
ref $rpc_response eq 'HASH' && exists $rpc_response->{'error'} ? : 10 24 34
Condition100%3/3
ref $rpc_response eq "HASH" and exists $rpc_response->{"error"}
!ll&&!rl&&r
22210
MC/DC100%2/2
ref $rpc_response eq "HASH" and exists $rpc_response->{"error"}
ref $rpc_response eq "HASH" exists $rpc_response->{"error"}
Truth table100%3/3
A and B
A= ref $rpc_response eq "HASH"B= exists $rpc_response->{"error"}
ABresult
0X0
100
111
23 )
24 );
25 34 $res->headers->content_type('application/json-rpc');
26 34 return $self->render(json => $rpc_response);
27 }
28  
29 37 ▶sub _acquire_methods {
Subroutine _acquire_methods: called CC=34 SCAR=39.0
30 37 my ($self) = @_;
31  
32 37 my $log = $self->app->log;
33 37 my $req = $self->req; # Mojo::Message::Request object
34 37 my $method = $req->method; # GET / POST
35 37 my $request;
36  
37 37 ▶ if ( $method eq 'POST' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'POST') { } 35 2 37
elsif ($method eq 'GET') { } 2 0 2
38 35 ▶ if ( $log->is_level('debug') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log->is_level("debug")) 0 35 35
39 0 $log->debug( 'REQUEST: BODY> ' . $req->body );
40 }
41
42 35 my $decode_error;
43 35 ▶ eval{ $request = decode_json( $req->body ); 1; } or $decode_error = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $request = decode_json($req->body); 1 } } or $decode_error = $@ 33 2 35
44 35 ▶ if ( $decode_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decode_error) 2 33 35
45 2 $log->debug( 'REQUEST: JSON error> ' . $decode_error );
46 2 return MojoX::JSON::RPC::Dispatcher::Method->new->parse_error(
47 $decode_error );
48 }
49 }
50 elsif ( $method eq 'GET' ) {
51 2 my $params = $req->query_params->to_hash;
52 2 my $decoded_params;
53  
54 2 ▶ if ( exists $params->{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"params"}) 0 2 2
55 0 my $decode_error;
56 0 ▶ eval{ $decoded_params = decode_json( $params->{params} ); 1; } or $decode_error = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $decoded_params = decode_json($params->{"params"}); 1 } } or $decode_error = $@ 0 0 0
57
58 0 ▶ if ( $decode_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decode_error) 0 0 0
59 0 return MojoX::JSON::RPC::Dispatcher::Method->new->parse_error(
60 $decode_error );
61 }
62 }
63 $request = {
64 method => $params->{method},
65 2 ▶ exists $params->{id} ? ( id => $params->{id} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
exists $params->{'id'} ? : 0 2 2
defined $decoded_params ? : 0 2 2
66 defined $decoded_params ? ( params => $decoded_params ) : ()
67 };
68 }
69 else {
70 0 return MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request(
71 qq{Invalid method type: $method});
72 }
73  
74 35 ▶ if ( ref $request ne 'HASH' && ref $request ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request ne "HASH" and ref $request ne "ARRAY") 0 35 35
Condition66%2/3
ref $request ne "HASH" and ref $request ne "ARRAY"
!ll&&!rl&&r
2780
MC/DC0%0/2
ref $request ne "HASH" and ref $request ne "ARRAY"
ref $request ne "HASH" ref $request ne "ARRAY"
Truth table66%2/3
A and B
A= ref $request ne "HASH"B= ref $request ne "ARRAY"
ABresult
0X0
100
111
75 0 return MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request(
76 $request);
77 }
78  
79 35 my @methods = ();
80 METHOD:
81 35 ▶ foreach my $obj ( ref $request eq 'HASH' ? $request : @{$request} ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $request eq 'HASH' ? : 27 8 35
82 42 ▶ if ( ref $obj ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj ne "HASH") 4 38 42
83 4 push @methods,
84 MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request;
85 4 next METHOD;
86 }
87 38 ▶ my $method = $obj->{method} if exists $obj->{method};
Branch: 1 branch
BranchTrueFalseTotal
if exists $obj->{"method"} 37 1 38
88 38 ▶ if (!( defined $method
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method and $method =~ /^[A-Za-z_\.][A-Za-z0-9_\.]*$/msux) 4 34 38
Condition100%3/3
defined $method and $method =~ /^[A-Za-z_\.][A-Za-z0-9_\.]*$/msux
!ll&&!rl&&r
2234
MC/DC100%2/2
defined $method and $method =~ /^[A-Za-z_\.][A-Za-z0-9_\.]*$/msux
defined $method $method =~ /^[A-Za-z_\.][A-Za-z0-9_\.]*$/msux
Truth table100%3/3
A and B
A= defined $methodB= $method =~ /^[A-Za-z_\.][A-Za-z0-9_\.]*$/msux
ABresult
0X0
100
111
89 && $method =~ m/^[A-Za-z_\.][A-Za-z0-9_\.]*$/xms
90 )
91 )
92 {
93 4 push @methods,
94 MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request(
95 $method);
96 4 next METHOD;
97 }
98 34 ▶ my $params = $obj->{params} if exists $obj->{params};
Branch: 1 branch
BranchTrueFalseTotal
if exists $obj->{"params"} 27 7 34
99 34 ▶ if ( defined $params
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params and ref $params ne "ARRAY" and ref $params ne "HASH") 1 33 34
Condition100%6/6
defined $params and ref $params ne "ARRAY" and ref $params ne "HASH"
!ll&&!rl&&r
2941
defined $params and ref $params ne "ARRAY"
!ll&&!rl&&r
7225
MC/DC100%3/3
defined $params and ref $params ne "ARRAY" and ref $params ne "HASH"
defined $params ref $params ne "ARRAY" ref $params ne "HASH"
Truth table100%4/4
A and B and C
A= defined $paramsB= ref $params ne "ARRAY"C= ref $params ne "HASH"
ABCresult
0XX0
10X0
1100
1111
100 && ref $params ne 'ARRAY'
101 && ref $params ne 'HASH' )
102 {
103 1 push @methods,
104 MojoX::JSON::RPC::Dispatcher::Method->new->invalid_params(
105 'NOT array or hash: ' . $params );
106 1 next METHOD;
107 }
108  
109 ## Hack to support JSON-RPC 1.1
110 33 ▶ if ( !( exists $obj->{id} && defined $obj->{id} )
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $obj->{"id"} && defined $obj->{"id"} and exists $obj->{"version"} and defined $obj->{"version"} and $obj->{"version"} eq "1.1") 0 33 33
Condition66%8/12
not exists $obj->{"id"} && defined $obj->{"id"} and exists $obj->{"version"} and defined $obj->{"version"} and $obj->{"version"} eq "1.1"
!ll&&!rl&&r
2850
not exists $obj->{"id"} && defined $obj->{"id"} and exists $obj->{"version"} and defined $obj->{"version"}
!ll&&!rl&&r
2805
not exists $obj->{"id"} && defined $obj->{"id"} and exists $obj->{"version"}
!ll&&!rl&&r
2805
exists $obj->{'id'} && defined $obj->{'id'}
!ll&&!rl&&r
5028
MC/DC0%0/5
not exists $obj->{"id"} && defined $obj->{"id"} and exists $obj->{"version"} and defined $obj->{"version"} and $obj->{"version"} eq "1.1"
exists $obj->{'id'} defined $obj->{'id'} exists $obj->{"version"} defined $obj->{"version"} $obj->{"version"} eq "1.1"
Truth table22%2/9
not(A && B) and C and D and E
A= exists $obj->{'id'}B= defined $obj->{'id'}C= exists $obj->{"version"}D= defined $obj->{"version"}E= $obj->{"version"} eq "1.1"
ABCDEresult
11XXX0
0X0XX0
100XX0
0X10X0
1010X0
0X1100
101100
0X1111
101111
111 && exists $obj->{version}
112 && defined $obj->{version}
113 && $obj->{version} eq '1.1' )
114 {
115 0 $obj->{id} = '#'; # TODO: fix this
116 }
117  
118 ## Create method object
119 my $m = MojoX::JSON::RPC::Dispatcher::Method->new(
120 method => $method,
121 id => exists $obj->{id} ? $obj->{id} : undef
122 33 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'id'} ? : 28 5 33
123 33 ▶ if ( !( exists $obj->{id} && defined $obj->{id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $obj->{"id"} and defined $obj->{"id"}) 5 28 33
Condition66%2/3
exists $obj->{"id"} and defined $obj->{"id"}
!ll&&!rl&&r
5028
MC/DC50%1/2
exists $obj->{"id"} and defined $obj->{"id"}
exists $obj->{"id"} defined $obj->{"id"}
Truth table66%2/3
A and B
A= exists $obj->{"id"}B= defined $obj->{"id"}
ABresult
0X0
100
111
124 5 $m->is_notification(1);
125 }
126 33 ▶ if ( defined $params ) { # process parameters
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params) 26 7 33
127 26 ▶ if ( ref $params ne 'ARRAY' && ref $params ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params ne 'ARRAY' and ref $params ne 'HASH') { } 0 26 26
Condition66%2/3
ref $params ne "ARRAY" and ref $params ne "HASH"
!ll&&!rl&&r
2240
MC/DC0%0/2
ref $params ne "ARRAY" and ref $params ne "HASH"
ref $params ne "ARRAY" ref $params ne "HASH"
Truth table66%2/3
A and B
A= ref $params ne "ARRAY"B= ref $params ne "HASH"
ABresult
0X0
100
111
128 0 $m->invalid_params($params);
129 }
130 else {
131 26 $m->params($params);
132 }
133 }
134 33 push @methods, $m;
135 }
136 35 return \@methods;
137 }
138  
139 37 ▶sub _handle_methods {
Subroutine _handle_methods: called CC=24 SCAR=32.2
140 37 my ( $self, $methods ) = @_;
141  
142 37 my $log = $self->app->log;
143  
144 37 ▶ if ( ref $methods eq 'MojoX::JSON::RPC::Dispatcher::Method' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $methods eq "MojoX::JSON::RPC::Dispatcher::Method") 2 35 37
145 2 ▶ if ( $methods->has_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($methods->has_error) 2 0 2
146 2 return $methods->response;
147 }
148 0 $methods = [$methods];
149 }
150 35 ▶ if ( scalar @{$methods} == 0 ) { # empty
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$methods;} == 0) 1 34 35
151 return
152 1 MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request
153 ->response;
154 }
155  
156 34 my @responses;
157 34 my $service = $self->stash('service');
158 34 my $rpcs = $service->{_rpcs};
159 METHOD:
160 34 ▶ foreach my $m ( ref $methods eq 'ARRAY' ? @{$methods} : $methods ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $methods eq 'ARRAY' ? : 34 0 34
161 42 ▶ if ( $m->has_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->has_error) 9 33 42
162 9 push @responses, $m->response;
163 9 next METHOD;
164 }
165 my $rpc = $rpcs->{ $m->method }
166 33 ▶ if exists $rpcs->{ $m->method };
Branch: 1 branch
BranchTrueFalseTotal
if exists $$rpcs{$m->method} 31 2 33
167 my $code_ref = $rpc->{method}
168 33 ▶ if defined $rpc && exists $rpc->{method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rpc and exists $rpc->{"method"} 31 2 33
Condition66%2/3
defined $rpc and exists $rpc->{"method"}
!ll&&!rl&&r
2031
MC/DC50%1/2
defined $rpc and exists $rpc->{"method"}
defined $rpc exists $rpc->{"method"}
Truth table66%2/3
A and B
A= defined $rpcB= exists $rpc->{"method"}
ABresult
0X0
100
111
169 33 ▶ if ( defined $code_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code_ref) { } 31 2 33
170  
171 # deal with params and calling
172 # pass in svc obj and mojo tx if necessary
173 31 eval {
174 $m->result(
175 $code_ref->(
176 exists $rpc->{with_svc_obj}
177 && $rpc->{with_svc_obj} ? $service : (),
178 exists $rpc->{with_mojo_tx}
179 && $rpc->{with_mojo_tx} ? $self->tx : (),
180 exists $rpc->{with_self}
181 && $rpc->{with_self} ? $self : (),
182 defined $m->params
183 ? ref $m->params eq 'ARRAY'
184 31 ▶ ? @{ $m->params }
Branch: 5 branches
BranchTrueFalseTotal
exists $rpc->{'with_svc_obj'} && $rpc->{'with_svc_obj'} ? : 14 17 31
exists $rpc->{'with_mojo_tx'} && $rpc->{'with_mojo_tx'} ? : 3 28 31
exists $rpc->{'with_self'} && $rpc->{'with_self'} ? : 3 28 31
defined $m->params ? : 25 6 31
ref $m->params eq 'ARRAY' ? : 22 3 25
Condition66%6/9
exists $rpc->{"with_svc_obj"} and $rpc->{"with_svc_obj"}
!ll&&!rl&&r
17014
exists $rpc->{"with_mojo_tx"} and $rpc->{"with_mojo_tx"}
!ll&&!rl&&r
2803
exists $rpc->{"with_self"} and $rpc->{"with_self"}
!ll&&!rl&&r
2803
MC/DC50%1/2
exists $rpc->{"with_svc_obj"} and $rpc->{"with_svc_obj"}
exists $rpc->{"with_svc_obj"} $rpc->{"with_svc_obj"}
MC/DC50%1/2
exists $rpc->{"with_mojo_tx"} and $rpc->{"with_mojo_tx"}
exists $rpc->{"with_mojo_tx"} $rpc->{"with_mojo_tx"}
MC/DC50%1/2
exists $rpc->{"with_self"} and $rpc->{"with_self"}
exists $rpc->{"with_self"} $rpc->{"with_self"}
Truth table66%2/3
A and B
A= exists $rpc->{"with_svc_obj"}B= $rpc->{"with_svc_obj"}
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= exists $rpc->{"with_mojo_tx"}B= $rpc->{"with_mojo_tx"}
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= exists $rpc->{"with_self"}B= $rpc->{"with_self"}
ABresult
0X0
100
111
185 : $m->params
186 : ()
187 )
188 );
189 };
190 31 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 30 31
191 1 my $err = $@;
192 1 my $handler = $self->stash('exception_handler');
193 1 ▶ if (ref $handler eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq 'CODE') { } 1 0 1
194 1 $handler->($self, $err, $m);
195 }
196 else {
197 0 $m->internal_error($@);
198 }
199 }
200 }
201 else {
202 2 $m->method_not_found( $m->method );
203 }
204 33 ▶ if ( !$m->is_notification || $m->has_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $m->is_notification or $m->has_error) 28 5 33
Condition66%2/3
not $m->is_notification or $m->has_error
l!l&&r!l&&!r
2805
MC/DC50%1/2
not $m->is_notification or $m->has_error
not $m->is_notification $m->has_error
Truth table66%2/3
not(A) or B
A= not $m->is_notificationB= $m->has_error
ABresult
1X1
011
000
205 28 push @responses, $m->response;
206 }
207 }
208 34 ▶ return scalar @responses > 1 ? \@responses : $responses[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @responses > 1 ? : 2 32 34
209 }
210  
211 # Translate JSON-RPC error code to HTTP status.
212 34 ▶sub _translate_error_code_to_status {
Subroutine _translate_error_code_to_status: called CC=3 SCAR=11.0
213 34 my ( $self, $code ) = @_;
214 34 my %trans = (
215 q{} => 200,
216 '-32600' => 400,
217 '-32601' => 404,
218 '-32602' => 200, # wants the user to get the rpc error
219 '-32700' => 400
220 );
221 34 ▶ $code ||= q{};
Condition100%2/2
$code ||= ""
l!l
1024
MC/DC100%1/1
$code ||= ""
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
222 34 ▶ return exists $trans{$code} ? $trans{$code} : 500;
Branch: 1 branch
BranchTrueFalseTotal
exists $trans{$code} ? : 34 0 34
223 }
224  
225 3 1;
226  
227 __END__
↑ Summary lib/MojoX/JSON/RPC/Client.pm »