| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MojoX::JSON::RPC::Dispatcher; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
16
|
use Mojo::Base 'Mojolicious::Controller'; |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
13
|
|
|
4
|
3
|
|
|
3
|
|
345
|
use Mojo::JSON qw(decode_json); |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
127
|
|
|
5
|
3
|
|
|
3
|
|
970
|
use MojoX::JSON::RPC::Dispatcher::Method; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
13
|
|
|
6
|
3
|
|
|
3
|
|
74
|
use MojoX::JSON::RPC::Service; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# process JSON-RPC call |
|
9
|
|
|
|
|
|
|
sub call { |
|
10
|
37
|
|
|
37
|
1
|
143023
|
my ($self) = @_; |
|
11
|
|
|
|
|
|
|
|
|
12
|
37
|
|
|
|
|
123
|
my $rpc_response = $self->_handle_methods( $self->_acquire_methods ); |
|
13
|
37
|
100
|
|
|
|
151
|
if ( !$rpc_response ) { # is notification |
|
14
|
3
|
|
|
|
|
8
|
$self->tx->res->code(204); |
|
15
|
3
|
|
|
|
|
26
|
return $self->rendered; |
|
16
|
|
|
|
|
|
|
} |
|
17
|
34
|
|
|
|
|
84
|
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
|
100
|
100
|
|
|
307
|
: q{} |
|
23
|
|
|
|
|
|
|
) |
|
24
|
|
|
|
|
|
|
); |
|
25
|
34
|
|
|
|
|
192
|
$res->headers->content_type('application/json-rpc'); |
|
26
|
34
|
|
|
|
|
454
|
return $self->render(json => $rpc_response); |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _acquire_methods { |
|
30
|
37
|
|
|
37
|
|
68
|
my ($self) = @_; |
|
31
|
|
|
|
|
|
|
|
|
32
|
37
|
|
|
|
|
97
|
my $log = $self->app->log; |
|
33
|
37
|
|
|
|
|
209
|
my $req = $self->req; # Mojo::Message::Request object |
|
34
|
37
|
|
|
|
|
271
|
my $method = $req->method; # GET / POST |
|
35
|
37
|
|
|
|
|
157
|
my $request; |
|
36
|
|
|
|
|
|
|
|
|
37
|
37
|
100
|
|
|
|
88
|
if ( $method eq 'POST' ) { |
|
|
|
50
|
|
|
|
|
|
|
38
|
35
|
50
|
|
|
|
77
|
if ( $log->is_level('debug') ) { |
|
39
|
0
|
|
|
|
|
0
|
$log->debug( 'REQUEST: BODY> ' . $req->body ); |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
35
|
|
|
|
|
136
|
my $decode_error; |
|
43
|
35
|
100
|
|
|
|
39
|
eval{ $request = decode_json( $req->body ); 1; } or $decode_error = $@; |
|
|
35
|
|
|
|
|
73
|
|
|
|
33
|
|
|
|
|
20981
|
|
|
44
|
35
|
100
|
|
|
|
4674
|
if ( $decode_error ) { |
|
45
|
2
|
|
|
|
|
11
|
$log->debug( 'REQUEST: JSON error> ' . $decode_error ); |
|
46
|
2
|
|
|
|
|
69
|
return MojoX::JSON::RPC::Dispatcher::Method->new->parse_error( |
|
47
|
|
|
|
|
|
|
$decode_error ); |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
elsif ( $method eq 'GET' ) { |
|
51
|
2
|
|
|
|
|
13
|
my $params = $req->query_params->to_hash; |
|
52
|
2
|
|
|
|
|
128
|
my $decoded_params; |
|
53
|
|
|
|
|
|
|
|
|
54
|
2
|
50
|
|
|
|
7
|
if ( exists $params->{params} ) { |
|
55
|
0
|
|
|
|
|
0
|
my $decode_error; |
|
56
|
0
|
0
|
|
|
|
0
|
eval{ $decoded_params = decode_json( $params->{params} ); 1; } or $decode_error = $@; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
57
|
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
0
|
if ( $decode_error ) { |
|
59
|
0
|
|
|
|
|
0
|
return MojoX::JSON::RPC::Dispatcher::Method->new->parse_error( |
|
60
|
|
|
|
|
|
|
$decode_error ); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
$request = { |
|
64
|
|
|
|
|
|
|
method => $params->{method}, |
|
65
|
2
|
50
|
|
|
|
15
|
exists $params->{id} ? ( id => $params->{id} ) : (), |
|
|
|
50
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
defined $decoded_params ? ( params => $decoded_params ) : () |
|
67
|
|
|
|
|
|
|
}; |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
else { |
|
70
|
0
|
|
|
|
|
0
|
return MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request( |
|
71
|
|
|
|
|
|
|
qq{Invalid method type: $method}); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
35
|
50
|
66
|
|
|
117
|
if ( ref $request ne 'HASH' && ref $request ne 'ARRAY' ) { |
|
75
|
0
|
|
|
|
|
0
|
return MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request( |
|
76
|
|
|
|
|
|
|
$request); |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
35
|
|
|
|
|
60
|
my @methods = (); |
|
80
|
|
|
|
|
|
|
METHOD: |
|
81
|
35
|
100
|
|
|
|
93
|
foreach my $obj ( ref $request eq 'HASH' ? $request : @{$request} ) { |
|
|
8
|
|
|
|
|
18
|
|
|
82
|
42
|
100
|
|
|
|
90
|
if ( ref $obj ne 'HASH' ) { |
|
83
|
4
|
|
|
|
|
9
|
push @methods, |
|
84
|
|
|
|
|
|
|
MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request; |
|
85
|
4
|
|
|
|
|
6
|
next METHOD; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
38
|
100
|
|
|
|
88
|
my $method = $obj->{method} if exists $obj->{method}; |
|
88
|
38
|
100
|
100
|
|
|
192
|
if (!( defined $method |
|
89
|
|
|
|
|
|
|
&& $method =~ m/^[A-Za-z_\.][A-Za-z0-9_\.]*$/xms |
|
90
|
|
|
|
|
|
|
) |
|
91
|
|
|
|
|
|
|
) |
|
92
|
|
|
|
|
|
|
{ |
|
93
|
4
|
|
|
|
|
13
|
push @methods, |
|
94
|
|
|
|
|
|
|
MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request( |
|
95
|
|
|
|
|
|
|
$method); |
|
96
|
4
|
|
|
|
|
11
|
next METHOD; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
34
|
100
|
|
|
|
82
|
my $params = $obj->{params} if exists $obj->{params}; |
|
99
|
34
|
100
|
100
|
|
|
127
|
if ( defined $params |
|
|
|
|
100
|
|
|
|
|
|
100
|
|
|
|
|
|
|
&& ref $params ne 'ARRAY' |
|
101
|
|
|
|
|
|
|
&& ref $params ne 'HASH' ) |
|
102
|
|
|
|
|
|
|
{ |
|
103
|
1
|
|
|
|
|
4
|
push @methods, |
|
104
|
|
|
|
|
|
|
MojoX::JSON::RPC::Dispatcher::Method->new->invalid_params( |
|
105
|
|
|
|
|
|
|
'NOT array or hash: ' . $params ); |
|
106
|
1
|
|
|
|
|
3
|
next METHOD; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
## Hack to support JSON-RPC 1.1 |
|
110
|
33
|
50
|
66
|
|
|
186
|
if ( !( exists $obj->{id} && defined $obj->{id} ) |
|
|
|
|
66
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
111
|
|
|
|
|
|
|
&& exists $obj->{version} |
|
112
|
|
|
|
|
|
|
&& defined $obj->{version} |
|
113
|
|
|
|
|
|
|
&& $obj->{version} eq '1.1' ) |
|
114
|
|
|
|
|
|
|
{ |
|
115
|
0
|
|
|
|
|
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
|
100
|
|
|
|
163
|
); |
|
123
|
33
|
100
|
66
|
|
|
284
|
if ( !( exists $obj->{id} && defined $obj->{id} ) ) { |
|
124
|
5
|
|
|
|
|
14
|
$m->is_notification(1); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
33
|
100
|
|
|
|
73
|
if ( defined $params ) { # process parameters |
|
127
|
26
|
50
|
66
|
|
|
72
|
if ( ref $params ne 'ARRAY' && ref $params ne 'HASH' ) { |
|
128
|
0
|
|
|
|
|
0
|
$m->invalid_params($params); |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
else { |
|
131
|
26
|
|
|
|
|
53
|
$m->params($params); |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
} |
|
134
|
33
|
|
|
|
|
183
|
push @methods, $m; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
35
|
|
|
|
|
125
|
return \@methods; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _handle_methods { |
|
140
|
37
|
|
|
37
|
|
80
|
my ( $self, $methods ) = @_; |
|
141
|
|
|
|
|
|
|
|
|
142
|
37
|
|
|
|
|
83
|
my $log = $self->app->log; |
|
143
|
|
|
|
|
|
|
|
|
144
|
37
|
100
|
|
|
|
231
|
if ( ref $methods eq 'MojoX::JSON::RPC::Dispatcher::Method' ) { |
|
145
|
2
|
50
|
|
|
|
5
|
if ( $methods->has_error ) { |
|
146
|
2
|
|
|
|
|
10
|
return $methods->response; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
0
|
|
|
|
|
0
|
$methods = [$methods]; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
35
|
100
|
|
|
|
38
|
if ( scalar @{$methods} == 0 ) { # empty |
|
|
35
|
|
|
|
|
67
|
|
|
151
|
|
|
|
|
|
|
return |
|
152
|
1
|
|
|
|
|
4
|
MojoX::JSON::RPC::Dispatcher::Method->new->invalid_request |
|
153
|
|
|
|
|
|
|
->response; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
34
|
|
|
|
|
38
|
my @responses; |
|
157
|
34
|
|
|
|
|
76
|
my $service = $self->stash('service'); |
|
158
|
34
|
|
|
|
|
295
|
my $rpcs = $service->{_rpcs}; |
|
159
|
|
|
|
|
|
|
METHOD: |
|
160
|
34
|
50
|
|
|
|
62
|
foreach my $m ( ref $methods eq 'ARRAY' ? @{$methods} : $methods ) { |
|
|
34
|
|
|
|
|
60
|
|
|
161
|
42
|
100
|
|
|
|
127
|
if ( $m->has_error ) { |
|
162
|
9
|
|
|
|
|
40
|
push @responses, $m->response; |
|
163
|
9
|
|
|
|
|
99
|
next METHOD; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
my $rpc = $rpcs->{ $m->method } |
|
166
|
33
|
100
|
|
|
|
160
|
if exists $rpcs->{ $m->method }; |
|
167
|
|
|
|
|
|
|
my $code_ref = $rpc->{method} |
|
168
|
33
|
50
|
66
|
|
|
274
|
if defined $rpc && exists $rpc->{method}; |
|
169
|
33
|
100
|
|
|
|
46
|
if ( defined $code_ref ) { |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# deal with params and calling |
|
172
|
|
|
|
|
|
|
# pass in svc obj and mojo tx if necessary |
|
173
|
31
|
|
|
|
|
45
|
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
|
100
|
66
|
|
|
190
|
? @{ $m->params } |
|
|
22
|
100
|
66
|
|
|
168
|
|
|
|
|
100
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
: $m->params |
|
186
|
|
|
|
|
|
|
: () |
|
187
|
|
|
|
|
|
|
) |
|
188
|
|
|
|
|
|
|
); |
|
189
|
|
|
|
|
|
|
}; |
|
190
|
31
|
100
|
|
|
|
1349
|
if ($@) { |
|
191
|
1
|
|
|
|
|
5
|
my $err = $@; |
|
192
|
1
|
|
|
|
|
3
|
my $handler = $self->stash('exception_handler'); |
|
193
|
1
|
50
|
|
|
|
10
|
if (ref $handler eq 'CODE') { |
|
194
|
1
|
|
|
|
|
3
|
$handler->($self, $err, $m); |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
else { |
|
197
|
0
|
|
|
|
|
0
|
$m->internal_error($@); |
|
198
|
|
|
|
|
|
|
} |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
else { |
|
202
|
2
|
|
|
|
|
5
|
$m->method_not_found( $m->method ); |
|
203
|
|
|
|
|
|
|
} |
|
204
|
33
|
100
|
66
|
|
|
67
|
if ( !$m->is_notification || $m->has_error ) { |
|
205
|
28
|
|
|
|
|
170
|
push @responses, $m->response; |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
} |
|
208
|
34
|
100
|
|
|
|
298
|
return scalar @responses > 1 ? \@responses : $responses[0]; |
|
209
|
|
|
|
|
|
|
} |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# Translate JSON-RPC error code to HTTP status. |
|
212
|
|
|
|
|
|
|
sub _translate_error_code_to_status { |
|
213
|
34
|
|
|
34
|
|
73
|
my ( $self, $code ) = @_; |
|
214
|
34
|
|
|
|
|
141
|
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
|
|
100
|
|
|
97
|
$code ||= q{}; |
|
222
|
34
|
50
|
|
|
|
142
|
return exists $trans{$code} ? $trans{$code} : 500; |
|
223
|
|
|
|
|
|
|
} |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
1; |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
__END__ |