line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::RPC::Parser; |
2
|
3
|
|
|
3
|
|
454
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
666
|
|
3
|
3
|
|
|
3
|
|
879
|
use JSON::RPC::Procedure; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
63
|
|
4
|
3
|
|
|
3
|
|
13
|
use Carp (); |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
36
|
|
5
|
3
|
|
|
3
|
|
1141
|
use Plack::Request; |
|
3
|
|
|
|
|
79697
|
|
|
3
|
|
|
|
|
89
|
|
6
|
|
|
|
|
|
|
use Class::Accessor::Lite |
7
|
3
|
|
|
|
|
21
|
new => 1, |
8
|
|
|
|
|
|
|
rw => [ qw( |
9
|
|
|
|
|
|
|
coder |
10
|
|
|
|
|
|
|
) ] |
11
|
3
|
|
|
3
|
|
22
|
; |
|
3
|
|
|
|
|
4
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub construct_procedure { |
14
|
20
|
|
|
20
|
0
|
21
|
my $self = shift; |
15
|
20
|
|
|
|
|
84
|
JSON::RPC::Procedure->new( @_ ); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub construct_from_req { |
19
|
21
|
|
|
21
|
0
|
6419
|
my ($self, $req) = @_; |
20
|
|
|
|
|
|
|
|
21
|
21
|
|
|
|
|
50
|
my $method = $req->method; |
22
|
21
|
|
|
|
|
100
|
my $proc; |
23
|
21
|
100
|
|
|
|
49
|
if ($method eq 'POST') { |
|
|
100
|
|
|
|
|
|
24
|
15
|
|
|
|
|
28
|
$proc = $self->construct_from_post_req( $req ); |
25
|
|
|
|
|
|
|
} elsif ($method eq 'GET') { |
26
|
5
|
|
|
|
|
12
|
$proc = $self->construct_from_get_req( $req ); |
27
|
|
|
|
|
|
|
} else { |
28
|
1
|
|
|
|
|
83
|
Carp::croak( "Invalid method: $method" ); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
18
|
|
|
|
|
142
|
return $proc; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub construct_from_post_req { |
35
|
15
|
|
|
15
|
0
|
13
|
my ($self, $req) = @_; |
36
|
|
|
|
|
|
|
|
37
|
15
|
|
|
|
|
15
|
my $request = eval { $self->coder->decode( $req->content ) }; |
|
15
|
|
|
|
|
33
|
|
38
|
15
|
100
|
|
|
|
9877
|
if ($@) { |
39
|
1
|
|
|
|
|
80
|
Carp::croak( "JSON parse error: $@" ); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
14
|
|
|
|
|
22
|
my $ref = ref $request; |
43
|
14
|
100
|
|
|
|
31
|
if ($ref ne 'ARRAY') { |
44
|
|
|
|
|
|
|
# is not a batch request |
45
|
8
|
|
|
|
|
36
|
return $self->construct_procedure( |
46
|
|
|
|
|
|
|
method => $request->{method}, |
47
|
|
|
|
|
|
|
id => $request->{id}, |
48
|
|
|
|
|
|
|
params => $request->{params}, |
49
|
|
|
|
|
|
|
jsonrpc => $request->{jsonrpc}, |
50
|
|
|
|
|
|
|
has_id => exists $request->{id}, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
6
|
|
|
|
|
6
|
my @procs; |
55
|
6
|
|
|
|
|
15
|
foreach my $req ( @$request ) { |
56
|
8
|
100
|
|
|
|
190
|
Carp::croak( "Invalid parameter") unless ref $req eq 'HASH'; |
57
|
7
|
|
|
|
|
56
|
push @procs, $self->construct_procedure( |
58
|
|
|
|
|
|
|
method => $req->{method}, |
59
|
|
|
|
|
|
|
id => $req->{id}, |
60
|
|
|
|
|
|
|
params => $req->{params}, |
61
|
|
|
|
|
|
|
jsonrpc => $req->{jsonrpc}, |
62
|
|
|
|
|
|
|
has_id => exists $req->{id}, # when not true it's a notification in JSON-RPC 2.0 |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
} |
65
|
5
|
|
|
|
|
43
|
return \@procs; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub construct_from_get_req { |
69
|
5
|
|
|
5
|
0
|
5
|
my ($self, $req) = @_; |
70
|
|
|
|
|
|
|
|
71
|
5
|
|
|
|
|
12
|
my $params = $req->query_parameters; |
72
|
5
|
|
|
|
|
441
|
my $decoded_params; |
73
|
5
|
100
|
|
|
|
12
|
if ($params->{params}) { |
74
|
4
|
|
|
|
|
4
|
$decoded_params = eval { $self->coder->decode( $params->{params} ) }; |
|
4
|
|
|
|
|
11
|
|
75
|
|
|
|
|
|
|
} |
76
|
5
|
|
|
|
|
60
|
return $self->construct_procedure( |
77
|
|
|
|
|
|
|
method => $params->{method}, |
78
|
|
|
|
|
|
|
id => $params->{id}, |
79
|
|
|
|
|
|
|
params => $decoded_params, |
80
|
|
|
|
|
|
|
jsonrpc => $params->{jsonrpc}, |
81
|
|
|
|
|
|
|
has_id => exists $params->{id}, |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
__END__ |