Lim/RPC/Protocol/JSONRPC2.pm

filter: Statementstmt 51.0% 70 / 137 Branchbran 18.3% 11 / 60 Conditioncond 26.6% 8 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 84.2% 16 / 19 Podpod 100% 7 / 7 totaltotal 41.4% 112 / 270 CC 41 SCAR 40.5
CC
41
Cov
39%
handle
108.4
response
90.0
request
30.0
CRAP 57.6
« Lim.pm ↑ Summary Lim/RPC/Value.pm »
1 package Lim::RPC::Protocol::JSONRPC2;
2  
3 5 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 5 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Lim::RPC::Callback ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use base qw(Lim::RPC::Protocol);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =encoding utf8
19  
20 =head1 NAME
21  
22 ...
23  
24 =head1 VERSION
25  
26 See L<Lim> for version.
27  
28 =cut
29  
30 5 our $VERSION = $Lim::VERSION;
31 5 our $JSON = JSON::XS->new->utf8->convert_blessed;
32 5 our $ID = 1;
33 5 our $ID_OVERFLOW = 2^32;
34  
35 =head1 SYNOPSIS
36  
37 ...
38  
39 =head1 SUBROUTINES/METHODS
40  
41 =head2 Init
42  
43 =cut
44  
45 1 ▶sub Init {
Subroutine Init: called CC=2 SCAR=9.2
Pod documented
46 1 ▶ if (Lim::Config->{rpc}->{json}->{pretty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Lim::Config()->{"rpc"}{"json"}{"pretty"}) 0 1 1
47 0 $JSON->pretty(1);
48 }
49 }
50  
51 =head2 Destroy
52  
53 =cut
54  
55 1 ▶sub Destroy {
Subroutine Destroy: called CC=1 SCAR=0.0
Pod documented
56 }
57  
58 =head2 name
59  
60 =cut
61  
62 8 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
63 8 'jsonrpc2';
64 }
65  
66 =head2 serve
67  
68 =cut
69  
70 1 ▶sub serve {
Subroutine serve: called CC=1 SCAR=0.0
Pod documented
71 }
72  
73 =head2 handle
74  
75 =cut
76  
77 1 ▶sub handle {
Subroutine handle: called CC=20 SCAR=46.9
Pod documented
78 1 my ($self, $cb, $request, $transport) = @_;
79
80 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
81 0 return;
82 }
83  
84 1 ▶ if ($request->header('Content-Type') =~ /(?:^|\s)application\/json(?:$|\s|;)/o and $request->uri =~ /^\/([a-zA-Z]+)\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->header("Content-Type") =~ m[(?:^|\s)application/json(?:$|\s|;)]ou and $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou) 1 0 1
Condition33%1/3
$request->header("Content-Type") =~ m[(?:^|\s)application/json(?:$|\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)application/json(?:$|\s|;)]ou and $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou
$request->header("Content-Type") =~ m[(?:^|\s)application/json(?:$|\s|;)]ou $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou
Truth table33%1/3
A and B
A= $request->header("Content-Type") =~ m[(?:^|\s)application/json(?:$|\s|;)]ouB= $request->uri =~ m[^/([a-zA-Z]+)\s*$]ou
ABresult
0X0
100
111
85 1 my ($module) = ($1);
86 1 my $response = HTTP::Response->new;
87 1 $response->request($request);
88 1 $response->protocol($request->protocol);
89
90 1 $module = lc($module);
91 1 my $server = $self->server;
92 1 ▶ if (defined $server and $server->have_module($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $server and $server->have_module($module)) { } 1 0 1
Condition33%1/3
defined $server and $server->have_module($module)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $server and $server->have_module($module)
defined $server $server->have_module($module)
Truth table33%1/3
A and B
A= defined $serverB= $server->have_module($module)
ABresult
0X0
100
111
93 1 my ($jsonreq, $jsonresp);
94
95 1 eval {
96 1 $jsonreq = $JSON->decode($request->content);
97 };
98 1 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 1 0 1
99 1 ▶ if (ref($jsonreq) eq 'HASH' and exists $jsonreq->{jsonrpc} and exists $jsonreq->{id} and exists $jsonreq->{method} and $jsonreq->{jsonrpc} eq '2.0') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $jsonreq eq 'HASH' and exists $jsonreq->{'jsonrpc'} and exists $jsonreq->{'id'} and exists $jsonreq->{'method'} and $jsonreq->{'jsonrpc'} eq '2.0') { } 1 0 1
Condition33%4/12
ref $jsonreq eq "HASH" and exists $jsonreq->{"jsonrpc"} and exists $jsonreq->{"id"} and exists $jsonreq->{"method"} and $jsonreq->{"jsonrpc"} eq "2.0"
!ll&&!rl&&r
001
ref $jsonreq eq "HASH" and exists $jsonreq->{"jsonrpc"} and exists $jsonreq->{"id"} and exists $jsonreq->{"method"}
!ll&&!rl&&r
001
ref $jsonreq eq "HASH" and exists $jsonreq->{"jsonrpc"} and exists $jsonreq->{"id"}
!ll&&!rl&&r
001
ref $jsonreq eq "HASH" and exists $jsonreq->{"jsonrpc"}
!ll&&!rl&&r
001
MC/DC0%0/5
ref $jsonreq eq "HASH" and exists $jsonreq->{"jsonrpc"} and exists $jsonreq->{"id"} and exists $jsonreq->{"method"} and $jsonreq->{"jsonrpc"} eq "2.0"
ref $jsonreq eq "HASH" exists $jsonreq->{"jsonrpc"} exists $jsonreq->{"id"} exists $jsonreq->{"method"} $jsonreq->{"jsonrpc"} eq "2.0"
Truth table16%1/6
A and B and C and D and E
A= ref $jsonreq eq "HASH"B= exists $jsonreq->{"jsonrpc"}C= exists $jsonreq->{"id"}D= exists $jsonreq->{"method"}E= $jsonreq->{"jsonrpc"} eq "2.0"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
100 1 my $id = $jsonreq->{id};
101 1 my $call = $jsonreq->{method};
102
103 1 ▶ if ($server->have_module_call($module, $call)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->have_module_call($module, $call)) { } 1 0 1
104 1 my $obj = $server->module_obj_by_protocol($module, $self->name);
105 1 my $real_self = $self;
106 1 weaken($self);
107
108 $obj->$call(Lim::RPC::Callback->new(
109 request => $request,
110 1 ▶ cb => sub {
Subroutine __ANON__: called CC=9 SCAR=33.3
111 1 my ($result) = @_;
112
113 1 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 1 1
114 0 return;
115 }
116
117 1 ▶ if (blessed $result and $result->isa('Lim::Error')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($result) and $result->isa('Lim::Error')) { } 0 1 1
elsif (ref $result eq 'HASH') { } 1 0 1
Condition33%1/3
builtin::blessed($result) and $result->isa("Lim::Error")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($result) and $result->isa("Lim::Error")
builtin::blessed($result) $result->isa("Lim::Error")
Truth table33%1/3
A and B
A= builtin::blessed($result)B= $result->isa("Lim::Error")
ABresult
0X0
100
111
118 0 $response->code($result->code);
119 0 eval {
120 0 $response->content($JSON->encode({
121 jsonrpc => '2.0',
122 error => {
123 code => $result->code,
124 message => $result->message
125 },
126 id => $id
127 }));
128 };
129 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
130 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
131 0 ▶ Lim::WARN and $self->{logger}->warn('JSON encode error: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("JSON encode error: ", $@) 0 0 0
132 }
133 else {
134 0 $response->header(
135 'Content-Type' => 'application/json; charset=utf-8',
136 'Cache-Control' => 'no-cache',
137 'Pragma' => 'no-cache'
138 );
139 }
140 }
141 elsif (ref($result) eq 'HASH') {
142 1 eval {
143 1 $response->content($JSON->encode({
144 jsonrpc => '2.0',
145 result => $result,
146 id => $id
147 }));
148 };
149 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
150 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
151 0 ▶ Lim::WARN and $self->{logger}->warn('JSON encode error: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("JSON encode error: ", $@) 0 0 0
152 }
153 else {
154 1 $response->header(
155 'Content-Type' => 'application/json; charset=utf-8',
156 'Cache-Control' => 'no-cache',
157 'Pragma' => 'no-cache'
158 );
159 1 $response->code(HTTP_OK);
160 }
161 }
162 else {
163 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
164 0 $self->{logger}->debug('Invalid result from JSONRPC call ', $call);
165 }
166
167 1 $cb->cb->($response);
168 1 return;
169 },
170 0 ▶ reset_timeout => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 $cb->reset_timeout;
172 1 }), $jsonreq->{params});
173 1 return 1;
174 }
175 else {
176 0 $response->code(HTTP_NOT_FOUND);
177 0 $jsonresp = {
178 jsonrpc => '2.0',
179 error => {
180 code => -32601,
181 message => 'Method not found'
182 },
183 id => $id
184 };
185 }
186 }
187 else {
188 0 $response->code(HTTP_BAD_REQUEST);
189 0 $jsonresp = {
190 jsonrpc => '2.0',
191 error => {
192 code => -32600,
193 message => 'Invalid Request'
194 },
195 id => undef
196 };
197 }
198 }
199 0 ▶ if ($@ and !defined $jsonresp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not defined $jsonresp) 0 0 0
Condition0%0/3
$@ and not defined $jsonresp
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not defined $jsonresp
$@ not defined $jsonresp
Truth table0%0/3
A and not(B)
A= $@B= not defined $jsonresp
ABresult
0X0
100
111
200 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
201 0 $jsonresp = {
202 jsonrpc => '2.0',
203 error => {
204 code => -32700,
205 message => 'Parse error'
206 },
207 id => undef
208 };
209 }
210 0 ▶ if (defined $jsonresp) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $jsonresp) { } 0 0 0
elsif (not $response->code) { } 0 0 0
211 0 eval {
212 0 $response->content($JSON->encode($jsonresp));
213 };
214 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
215 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
216 0 ▶ Lim::WARN and $self->{logger}->warn('JSON encode error: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("JSON encode error: ", $@) 0 0 0
217 }
218 }
219 elsif (!$response->code) {
220 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
221 0 $self->{logger}->debug('Unknown response, setting HTTP_INTERNAL_SERVER_ERROR');
222 }
223 }
224 else {
225 0 return;
226 }
227  
228 0 $cb->cb->($response);
229 0 return 1;
230 }
231 0 return;
232 }
233  
234 =head2 request
235  
236 =cut
237  
238 0 ▶sub request {
Subroutine request: not called CC=5 SCAR=34.0
Pod documented
239 0 my $self = shift;
240 0 my %args = ( @_ );
241  
242 0 my ($method, $uri) = Lim::Util::URIize($args{call});
243 0 ▶ $uri = (defined $args{path} ? $args{path} : '' ).'/'.lc($args{plugin});
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'path'} ? : 0 0 0
244 0 my $request = HTTP::Request->new($method, $uri);
245  
246 0 ▶ if (defined $args{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'data'}) { } 0 0 0
247 0 eval {
248 $request->content($JSON->encode({
249 jsonrpc => '2.0',
250 id => $ID++,
251 method => $args{call},
252 params => $args{data}
253 0 }));
254 };
255 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
256 0 confess 'JSON encoding of data failed: '.$@;
257 }
258 }
259 else {
260 $request->content($JSON->encode({
261 jsonrpc => '2.0',
262 id => $ID++,
263 method => $args{call}
264 0 }));
265 }
266 0 $request->header('Content-Type' => 'application/json; charset=utf-8');
267 0 $request->header('Content-Length' => length($request->content));
268  
269 0 ▶ if ($ID == $ID_OVERFLOW) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lim::RPC::Protocol::JSONRPC2::ID == $Lim::RPC::Protocol::JSONRPC2::ID_OVERFLOW) 0 0 0
270 0 $ID = 1;
271 }
272  
273 0 return $request;
274 }
275  
276 =head2 response
277  
278 =cut
279  
280 0 ▶sub response {
Subroutine response: not called CC=9 SCAR=45.0
Pod documented
281 0 my ($self, $response) = @_;
282 0 my $data = {};
283  
284 0 ▶ unless (blessed $response and $response->isa('HTTP::Response')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($response) and $response->isa("HTTP::Response")) 0 0 0
Condition0%0/3
builtin::blessed($response) and $response->isa("HTTP::Response")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($response) and $response->isa("HTTP::Response")
builtin::blessed($response) $response->isa("HTTP::Response")
Truth table0%0/3
A and B
A= builtin::blessed($response)B= $response->isa("HTTP::Response")
ABresult
0X0
100
111
285 0 return;
286 }
287  
288 0 ▶ if ($response->header('Content-Type') =~ /application\/json/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header('Content-Type') =~ m[application/json]iou) { } 0 0 0
289 0 eval {
290 0 $data = $JSON->decode($response->decoded_content);
291 };
292 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
293 0 return Lim::Error->new(
294 message => 'JSON decode error: '.$@,
295 module => $self
296 );
297 }
298  
299 0 ▶ if (ref($data) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "HASH") 0 0 0
300 0 return Lim::Error->new(
301 message => 'Invalid data returned, not a hash',
302 module => $self
303 );
304 }
305  
306 0 ▶ unless ($response->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->code == 200) 0 0 0
307 0 ▶ if (ref($data->{error}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"error"} eq "HASH") 0 0 0
308 0 return Lim::Error->new->set({ 'Lim::Error' => $data->{error} });
309 }
310 0 return Lim::Error->new(
311 code => $response->code,
312 module => $self
313 );
314 }
315 }
316 else {
317 0 return Lim::Error->new(
318 message => 'Unknown content type ['.$response->header('Content-Type').'] returned',
319 module => $self
320 );
321 }
322  
323 # TODO: How can we check id?
324  
325 0 ▶ return defined $data->{result} ? $data->{result} : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'result'} ? : 0 0 0
326 }
327  
328 =head1 AUTHOR
329  
330 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
331  
332 =head1 BUGS
333  
334 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
335  
336 =head1 SUPPORT
337  
338 You can find documentation for this module with the perldoc command.
339  
340 perldoc Lim
341  
342 You can also look for information at:
343  
344 =over 4
345  
346 =item * Lim issue tracker (report bugs here)
347  
348 L<https://github.com/jelu/lim/issues>
349  
350 =back
351  
352 =head1 ACKNOWLEDGEMENTS
353  
354 =head1 LICENSE AND COPYRIGHT
355  
356 Copyright 2012-2013 Jerry Lundström.
357  
358 This program is free software; you can redistribute it and/or modify it
359 under the terms of either: the GNU General Public License as published
360 by the Free Software Foundation; or the Artistic License.
361  
362 See http://dev.perl.org/licenses/ for more information.
363  
364  
365 =cut
366  
367 5 1; # End of Lim::RPC::Protocol::JSONRPC2
« Lim.pm ↑ Summary Lim/RPC/Value.pm »