Lim/RPC/Protocol/JSONRPC1.pm

filter: Statementstmt 65.9% 64 / 97 Branchbran 27.7% 10 / 36 Conditioncond 28.5% 6 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 93.7% 15 / 16 Podpod 100% 5 / 5 totaltotal 53.1% 100 / 188 SCAR 46.2
CC
26
Cov
52%
handle
88.6
__ANON__
27.9
__ANON__
2.0
CRAP 101.0
« Lim/RPC/Protocol/JSONRPC2.pm ↑ Summary Lim/RPC/Protocol/XMLRPC.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 Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Lim::RPC::Callback ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use base qw(Lim::RPC::Protocol);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 ...
22  
23 =head1 VERSION
24  
25 See L<Lim> for version.
26  
27 =cut
28  
29 5 our $VERSION = $Lim::VERSION;
30 5 our $JSON = JSON::XS->new->ascii->convert_blessed;
31  
32 =head1 SYNOPSIS
33  
34 ...
35  
36 =head1 SUBROUTINES/METHODS
37  
38 =head2 Init
39  
40 =cut
41  
42 ▶sub Init {
Subroutine Init: called CC=1 SCAR=0.0
Pod documented
43 }
44  
45 =head2 Destroy
46  
47 =cut
48  
49 ▶sub Destroy {
Subroutine Destroy: called CC=1 SCAR=0.0
Pod documented
50 }
51  
52 =head2 name
53  
54 =cut
55  
56 sub name {
57 7 ▶ 'jsonrpc1';
Subroutine name: called CC=1 SCAR=0.0
Pod documented
58 }
59  
60 =head2 serve
61  
62 =cut
63  
64 ▶sub serve {
Subroutine serve: called CC=1 SCAR=0.0
Pod documented
65 }
66  
67 =head2 handle
68  
69 =cut
70  
71 sub handle {
72 1 ▶ my ($self, $cb, $request, $transport) = @_;
Subroutine handle: called CC=18 SCAR=44.8
Pod documented
73
74 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
75 0 return;
76 }
77  
78 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
79 1 my ($module) = ($1);
80 1 my $response = HTTP::Response->new;
81 1 $response->request($request);
82 1 $response->protocol($request->protocol);
83
84 1 $module = lc($module);
85 1 my $server = $self->server;
86 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
87 1 my ($jsonreq, $jsonresp);
88
89 1 eval {
90 1 $jsonreq = $JSON->decode($request->content);
91 };
92 1 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 1 0 1
93 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
94 1 my $id = $jsonreq->{id};
95 1 my $call = $jsonreq->{method};
96
97 1 ▶ if ($server->have_module_call($module, $call)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->have_module_call($module, $call)) { } 1 0 1
98 1 my $obj = $server->module_obj_by_protocol($module, $self->name);
99 1 my $real_self = $self;
100 1 weaken($self);
101
102 $obj->$call(Lim::RPC::Callback->new(
103 cb => sub {
104 1 ▶ my ($result) = @_;
Subroutine __ANON__: called CC=9 SCAR=33.3
105
106 1 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 1 1
107 0 return;
108 }
109
110 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
111 0 $response->code($result->code);
112 0 eval {
113 0 $response->content($JSON->encode({
114 result => undef,
115 error => {
116 code => $result->code,
117 message => $result->message
118 },
119 id => $id
120 }));
121 };
122 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
123 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
124 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
125 }
126 else {
127 0 $response->header(
128 'Content-Type' => 'application/json; charset=utf-8',
129 'Cache-Control' => 'no-cache',
130 'Pragma' => 'no-cache'
131 );
132 }
133 }
134 elsif (ref($result) eq 'HASH') {
135 1 eval {
136 1 $response->content($JSON->encode({
137 result => $result,
138 error => undef,
139 id => $id
140 }));
141 };
142 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
143 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
144 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
145 }
146 else {
147 1 $response->header(
148 'Content-Type' => 'application/json; charset=utf-8',
149 'Cache-Control' => 'no-cache',
150 'Pragma' => 'no-cache'
151 );
152 1 $response->code(HTTP_OK);
153 }
154 }
155 else {
156 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
157 0 $self->{logger}->debug('Invalid result from JSONRPC call ', $call);
158 }
159
160 1 $cb->cb->($response);
161 1 return;
162 },
163 reset_timeout => sub {
164 0 ▶ $cb->reset_timeout;
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 1 }), $jsonreq->{params});
166 1 return 1;
167 }
168 else {
169 0 $response->code(HTTP_NOT_FOUND);
170 0 $jsonresp = {
171 result => undef,
172 error => {
173 code => -32601,
174 message => 'Method not found'
175 },
176 id => $id
177 };
178 }
179 }
180 else {
181 0 $response->code(HTTP_BAD_REQUEST);
182 0 $jsonresp = {
183 result => undef,
184 error => {
185 code => -32600,
186 message => 'Invalid Request'
187 },
188 id => undef
189 };
190 }
191 }
192 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
193 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
194 0 $jsonresp = {
195 result => undef,
196 error => {
197 code => -32700,
198 message => 'Parse error'
199 },
200 id => undef
201 };
202 }
203 0 ▶ if (defined $jsonresp) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $jsonresp) { } 0 0 0
elsif (not $response->code) { } 0 0 0
204 0 eval {
205 0 $response->content($JSON->encode($jsonresp));
206 };
207 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
208 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
209 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
210 }
211 }
212 elsif (!$response->code) {
213 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
214 0 $self->{logger}->debug('Unknown response, setting HTTP_INTERNAL_SERVER_ERROR');
215 }
216 }
217 else {
218 0 return;
219 }
220  
221 0 $cb->cb->($response);
222 0 return 1;
223 }
224 0 return;
225 }
226  
227 =head1 AUTHOR
228  
229 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
230  
231 =head1 BUGS
232  
233 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
234  
235 =head1 SUPPORT
236  
237 You can find documentation for this module with the perldoc command.
238  
239 perldoc Lim
240  
241 You can also look for information at:
242  
243 =over 4
244  
245 =item * Lim issue tracker (report bugs here)
246  
247 L<https://github.com/jelu/lim/issues>
248  
249 =back
250  
251 =head1 ACKNOWLEDGEMENTS
252  
253 =head1 LICENSE AND COPYRIGHT
254  
255 Copyright 2012-2013 Jerry Lundström.
256  
257 This program is free software; you can redistribute it and/or modify it
258 under the terms of either: the GNU General Public License as published
259 by the Free Software Foundation; or the Artistic License.
260  
261 See http://dev.perl.org/licenses/ for more information.
262  
263  
264 =cut
265  
266 5 1; # End of Lim::RPC::Protocol::JSONRPC1
« Lim/RPC/Protocol/JSONRPC2.pm ↑ Summary Lim/RPC/Protocol/XMLRPC.pm »