Lim/RPC/Protocol/JSONRPC1.pm

filter: Statementstmt 51.0% 70 / 137 Branchbran 18.3% 11 / 60 Conditioncond 25.0% 6 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 84.2% 16 / 19 Podpod 100% 7 / 7 totaltotal 41.9% 110 / 262 CC 39 SCAR 39.0
CC
39
Cov
39%
response
90.0
handle
88.6
request
30.0
CRAP 49.6
« Lim/RPC/Value.pm ↑ Summary Lim/RPC/Transport/Client/HTTP.pm »
1 package Lim::RPC::Protocol::JSONRPC1;
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 'jsonrpc1';
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=18 SCAR=44.8
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->{id} and exists $jsonreq->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $jsonreq eq 'HASH' and exists $jsonreq->{'id'} and exists $jsonreq->{'method'}) { } 1 0 1
Condition33%2/6
ref $jsonreq eq "HASH" and exists $jsonreq->{"id"} and exists $jsonreq->{"method"}
!ll&&!rl&&r
001
ref $jsonreq eq "HASH" and exists $jsonreq->{"id"}
!ll&&!rl&&r
001
MC/DC0%0/3
ref $jsonreq eq "HASH" and exists $jsonreq->{"id"} and exists $jsonreq->{"method"}
ref $jsonreq eq "HASH" exists $jsonreq->{"id"} exists $jsonreq->{"method"}
Truth table25%1/4
A and B and C
A= ref $jsonreq eq "HASH"B= exists $jsonreq->{"id"}C= exists $jsonreq->{"method"}
ABCresult
0XX0
10X0
1100
1111
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 result => undef,
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 result => $result,
145 error => undef,
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 result => undef,
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 result => undef,
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 result => undef,
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 id => $ID++,
250 method => $args{call},
251 params => $args{data}
252 0 }));
253 };
254 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
255 0 confess 'JSON encoding of data failed: '.$@;
256 }
257 }
258 else {
259 $request->content($JSON->encode({
260 id => $ID++,
261 method => $args{call}
262 0 }));
263 }
264 0 $request->header('Content-Type' => 'application/json; charset=utf-8');
265 0 $request->header('Content-Length' => length($request->content));
266  
267 0 ▶ if ($ID == $ID_OVERFLOW) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lim::RPC::Protocol::JSONRPC1::ID == $Lim::RPC::Protocol::JSONRPC1::ID_OVERFLOW) 0 0 0
268 0 $ID = 1;
269 }
270  
271 0 return $request;
272 }
273  
274 =head2 response
275  
276 =cut
277  
278 0 ▶sub response {
Subroutine response: not called CC=9 SCAR=45.0
Pod documented
279 0 my ($self, $response) = @_;
280 0 my $data = {};
281  
282 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
283 0 return;
284 }
285  
286 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
287 0 eval {
288 0 $data = $JSON->decode($response->decoded_content);
289 };
290 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
291 0 return Lim::Error->new(
292 message => 'JSON decode error: '.$@,
293 module => $self
294 );
295 }
296  
297 0 ▶ if (ref($data) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "HASH") 0 0 0
298 0 return Lim::Error->new(
299 message => 'Invalid data returned, not a hash',
300 module => $self
301 );
302 }
303  
304 0 ▶ unless ($response->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->code == 200) 0 0 0
305 0 ▶ if (ref($data->{error}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"error"} eq "HASH") 0 0 0
306 0 return Lim::Error->new->set({ 'Lim::Error' => $data->{error} });
307 }
308 0 return Lim::Error->new(
309 code => $response->code,
310 module => $self
311 );
312 }
313 }
314 else {
315 0 return Lim::Error->new(
316 message => 'Unknown content type ['.$response->header('Content-Type').'] returned',
317 module => $self
318 );
319 }
320  
321 # TODO: How can we check id?
322  
323 0 ▶ return defined $data->{result} ? $data->{result} : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'result'} ? : 0 0 0
324 }
325  
326 =head1 AUTHOR
327  
328 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
329  
330 =head1 BUGS
331  
332 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
333  
334 =head1 SUPPORT
335  
336 You can find documentation for this module with the perldoc command.
337  
338 perldoc Lim
339  
340 You can also look for information at:
341  
342 =over 4
343  
344 =item * Lim issue tracker (report bugs here)
345  
346 L<https://github.com/jelu/lim/issues>
347  
348 =back
349  
350 =head1 ACKNOWLEDGEMENTS
351  
352 =head1 LICENSE AND COPYRIGHT
353  
354 Copyright 2012-2013 Jerry Lundström.
355  
356 This program is free software; you can redistribute it and/or modify it
357 under the terms of either: the GNU General Public License as published
358 by the Free Software Foundation; or the Artistic License.
359  
360 See http://dev.perl.org/licenses/ for more information.
361  
362  
363 =cut
364  
365 5 1; # End of Lim::RPC::Protocol::JSONRPC1
« Lim/RPC/Value.pm ↑ Summary Lim/RPC/Transport/Client/HTTP.pm »