RPC/Dispatch.pm

filter: Statementstmt 95.1% 117 / 123 Branchbran 85.7% 36 / 42 Conditioncond 72.7% 8 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 5 totaltotal 88.7% 182 / 205 CC 27 SCAR 14.7
CC
27
Cov
91%
handle_psgi
10.2
__ANON__
4.0
new
4.0
CRAP 4.4
« RPC/Legacy/Server/Daemon.pm ↑ Summary RPC/Legacy/Server/CGI.pm »
1 package JSON::RPC::Dispatch;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use JSON::RPC::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use JSON::RPC::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON::RPC::Procedure;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Router::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use Class::Accessor::Lite
11 2 ▶ rw => [ qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 coder
13 handlers
14 parser
15 prefix
16 router
17 ) ]
18 2 ;
19  
20 2 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
21 2 my ($class, @args) = @_;
22 2 my $self = bless {
23 handlers => {},
24 @args,
25 }, $class;
26 2 ▶ if (! $self->{coder}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"coder"}) 1 1 2
27 1 require JSON;
28 1 $self->{coder} = JSON->new->utf8;
29 }
30 2 ▶ if (! $self->{parser}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"parser"}) 1 1 2
31 1 $self->{parser} = JSON::RPC::Parser->new( coder => $self->coder )
32 }
33 2 ▶ if (! $self->{router}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"router"}) 1 1 2
34 1 $self->{router} = Router::Simple->new;
35 }
36 2 return $self;
37 }
38  
39 10 ▶sub guess_handler_class {
Subroutine guess_handler_class: called CC=2 SCAR=7.1
Pod undocumented
40 10 my ($self, $klass) = @_;
41  
42 10 ▶ my $prefix = $self->prefix || '';
Condition50%1/2
$self->prefix || ''
l!l
100
MC/DC0%0/1
$self->prefix || ''
$self->prefix
Truth table50%1/2
A
A= $self->prefix
Aresult
11
00
43 10 return "$prefix\::$klass";
44 }
45  
46 10 ▶sub construct_handler {
Subroutine construct_handler: called CC=3 SCAR=11.0
Pod undocumented
47 10 my ($self, $klass) = @_;
48  
49 10 my $handler = $self->handlers->{ $klass };
50 10 ▶ if (! $handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 1 9 10
51 1 eval "require $klass";
52 1 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
53 1 $handler = $klass->new();
54 1 $self->handlers->{$klass} = $handler;
55 }
56 10 return $handler;
57 }
58  
59 12 ▶sub get_handler {
Subroutine get_handler: called CC=3 SCAR=11.0
Pod undocumented
60 12 my ($self, $klass) = @_;
61  
62 12 ▶ if ( Scalar::Util::blessed( $klass )){
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($klass)) 2 10 12
63 2 if (JSONRPC_DEBUG > 1) {
64 warn "Handler is already object : $klass";
65 }
66 2 return $klass;
67 }
68  
69 10 ▶ if ($klass !~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($klass =~ s/^\+//) 10 0 10
70 10 $klass = $self->guess_handler_class( $klass );
71 }
72  
73 10 my $handler = $self->construct_handler( $klass );
74 10 if (JSONRPC_DEBUG > 1) {
75 warn "$klass -> $handler";
76 }
77 10 return $handler;
78 }
79  
80 17 ▶sub handle_psgi {
Subroutine handle_psgi: called CC=10 SCAR=23.2
Pod undocumented
81 17 my ($self, $req, @args) = @_;
82  
83 17 ▶ if ( ! Scalar::Util::blessed($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($req)) 17 0 17
84 # assume it's a PSGI hash
85 17 require Plack::Request;
86 17 $req = Plack::Request->new($req);
87 }
88  
89 17 my $is_batch = 0;
90 17 my @response;
91 my $procedures;
92 17 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=13.9
93 17 $procedures = $self->parser->construct_from_req( $req );
94 14 ▶ if (ref $procedures eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $procedures eq 'ARRAY') { } 4 10 14
95 4 $is_batch = 1;
96 } else {
97 10 $procedures = [$procedures];
98 }
99 14 ▶ if (@$procedures <= 0 || not defined $procedures->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$procedures <= 0 or not defined $procedures->[0]) 1 13 14
Condition66%2/3
@$procedures <= 0 or not defined $procedures->[0]
l!l&&r!l&&!r
1013
MC/DC50%1/2
@$procedures <= 0 or not defined $procedures->[0]
@$procedures <= 0 not defined $procedures->[0]
Truth table66%2/3
A or not(B)
A= @$procedures <= 0B= not defined $procedures->[0]
ABresult
1X1
011
000
100 1 $is_batch = 0;
101 1 push @response, {
102 error => {
103 code => RPC_INVALID_REQUEST,
104 message => "Could not find any procedures"
105 }
106 };
107 }
108 3 ▶ } catch {
Subroutine __ANON__: called CC=3 SCAR=11.0
109 3 my $e = $_;
110 3 if (JSONRPC_DEBUG) {
111 warn "error while creating jsonrpc request: $e";
112 }
113 3 ▶ if ($e =~ /Invalid parameter/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e =~ /Invalid parameter/) { } 1 2 3
elsif ($e =~ /parse error/) { } 1 1 2
114 1 push @response, {
115 error => {
116 code => RPC_INVALID_PARAMS,
117 message => "Invalid parameters",
118 }
119 };
120 } elsif ( $e =~ /parse error/ ) {
121 1 push @response, {
122 error => {
123 code => RPC_PARSE_ERROR,
124 message => "Failed to parse json",
125 }
126 };
127 } else {
128 1 push @response, {
129 error => {
130 code => RPC_INVALID_REQUEST,
131 message => $e
132 }
133 }
134 }
135 17 };
136  
137 17 my $router = $self->router;
138 17 foreach my $procedure (@$procedures) {
139 15 ▶ if ( ! $procedure->{method} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($procedure->{"method"}) 0 15 15
140 0 my $message = "Procedure name not given";
141 0 if (JSONRPC_DEBUG) {
142 warn $message;
143 }
144 0 push @response, {
145 error => {
146 code => RPC_METHOD_NOT_FOUND,
147 message => $message,
148 }
149 };
150 0 next;
151 }
152  
153 15 ▶ my $is_notification = defined $procedure->jsonrpc && $procedure->jsonrpc eq '2.0' && !$procedure->has_id;
Condition83%5/6
defined $procedure->jsonrpc && $procedure->jsonrpc eq '2.0' && !$procedure->has_id
!ll&&!rl&&r
654
defined $procedure->jsonrpc and $procedure->jsonrpc eq "2.0"
!ll&&!rl&&r
609
MC/DC66%2/3
defined $procedure->jsonrpc && $procedure->jsonrpc eq '2.0' && !$procedure->has_id
defined $procedure->jsonrpc $procedure->jsonrpc eq "2.0" !$procedure->has_id
Truth table75%3/4
A and B && not(C)
A= defined $procedure->jsonrpcB= $procedure->jsonrpc eq "2.0"C= !$procedure->has_id
ABCresult
0XX0
10X0
1100
1111
154 15 my $matched = $router->match( $procedure->{method} );
155 15 ▶ if (! $matched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 3 12 15
156 3 my $message = "Procedure '$procedure->{method}' not found";
157 3 if (JSONRPC_DEBUG) {
158 warn $message;
159 }
160 3 ▶ if (!$is_notification) { # must not respond to a valid JSON-RPC notification
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_notification) 2 1 3
161 2 push @response, {
162 error => {
163 code => RPC_METHOD_NOT_FOUND,
164 message => $message,
165 }
166 };
167 }
168 3 next;
169 }
170  
171 12 my $action = $matched->{action};
172 12 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.4
173 12 my ($ip, $ua);
174 12 if (JSONRPC_DEBUG > 1) {
175 warn "Procedure '$procedure->{method}' maps to action $action";
176 $ip = $req->address || 'N/A';
177 $ua = $req->user_agent || 'N/A';
178 }
179 12 my $params = $procedure->params;
180 12 my $handler = $self->get_handler( $matched->{handler} );
181  
182 12 my $code = $handler->can( $action );
183 12 ▶ if (! $code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 12 12
184 0 if ( JSONRPC_DEBUG ) {
185 warn "[INFO] handler $handler does not implement method $action!.";
186 }
187 0 die "Internal Error";
188 }
189 12 my $result = $code->( $handler, $procedure->params, $procedure, @args );
190 5 if (JSONRPC_DEBUG) {
191 warn "[INFO] action=$action "
192 . "params=["
193 . (ref $params ? $self->{coder}->encode($params) : $params)
194 . "] ret="
195 . (ref $result ? $self->{coder}->encode($result) : $result)
196 . " IP=$ip UA=$ua";
197 }
198  
199 # respond unless we are sure a procedure is a notification
200 5 ▶ if (!$is_notification) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_notification) 5 0 5
201 5 push @response, {
202 jsonrpc => '2.0',
203 result => $result,
204 id => $procedure->id,
205 };
206 }
207 7 ▶ } catch {
Subroutine __ANON__: called CC=3 SCAR=11.0
208 7 my $e = $_;
209 7 if (JSONRPC_DEBUG) {
210 warn "Error while executing $action: $e";
211 }
212 # can't respond to notifications even in case of errors
213 7 ▶ if (!$is_notification) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_notification) 4 3 7
214 4 my $error = {code => RPC_INTERNAL_ERROR} ;
215 4 ▶ if (ref $e eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq 'HASH') { } 1 3 4
216 $error->{message} = $e->{message},
217 $error->{data} = $e->{data},
218 1 } else {
219 3 $error->{message} = $e,
220 }
221 4 push @response, {
222 jsonrpc => '2.0',
223 id => $procedure->id,
224 error => $error,
225 };
226 }
227 12 };
228 }
229  
230 17 my $res;
231 17 ▶ if (scalar @response) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @response) { } 15 2 17
232 15 $res = $req->new_response(200);
233 15 $res->content_type( 'application/json; charset=utf8' );
234 15 ▶ $res->body(
Branch: 1 branch
BranchTrueFalseTotal
$is_batch ? : 2 13 15
235 $self->coder->encode( ($is_batch) ? \@response : $response[0] )
236 );
237 15 return $res->finalize;
238 } else { # no content
239 2 $res = $req->new_response(204);
240 }
241  
242 2 return $res->finalize;
243 }
244  
245 2 ▶no Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
246  
247 2 1;
248  
249 __END__
« RPC/Legacy/Server/Daemon.pm ↑ Summary RPC/Legacy/Server/CGI.pm »