line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################################## |
2
|
|
|
|
|
|
|
# JSONRPC version 1.1 |
3
|
|
|
|
|
|
|
# http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html |
4
|
|
|
|
|
|
|
############################################################################## |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
36429
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
7
|
1
|
|
|
1
|
|
527
|
use JSON (); |
|
1
|
|
|
|
|
9598
|
|
|
1
|
|
|
|
|
17
|
|
8
|
1
|
|
|
1
|
|
4
|
use Carp (); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
############################################################################## |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
package JSON::RPC::Legacy::Client; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$JSON::RPC::Legacy::Client::VERSION = '0.93'; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
508
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
36078
|
|
|
1
|
|
|
|
|
84
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
BEGIN { |
20
|
1
|
|
|
1
|
|
3
|
for my $method (qw/uri ua json content_type version id allow_call status_line/) { |
21
|
8
|
0
|
|
0
|
0
|
1570
|
eval qq| |
|
0
|
0
|
|
0
|
0
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
0
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub $method { |
23
|
|
|
|
|
|
|
\$_[0]->{$method} = \$_[1] if defined \$_[1]; |
24
|
|
|
|
|
|
|
\$_[0]->{$method}; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub AUTOLOAD { |
33
|
0
|
|
|
0
|
|
|
my $self = shift; |
34
|
0
|
|
|
|
|
|
my $method = $JSON::RPC::Legacy::Client::AUTOLOAD; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$method =~ s/.*:://; |
37
|
|
|
|
|
|
|
|
38
|
0
|
0
|
|
|
|
|
return if ($method eq 'DESTROY'); |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
$method =~ s/^__(\w+)__$/$1/; # avoid to call built-in methods (ex. __VERSION__ => VERSION) |
41
|
|
|
|
|
|
|
|
42
|
0
|
0
|
|
|
|
|
unless ( exists $self->allow_call->{ $method } ) { |
43
|
0
|
|
|
|
|
|
Carp::croak("Can't call the method not allowed by prepare()."); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my @params = @_; |
47
|
0
|
0
|
|
|
|
|
my $obj = { |
48
|
|
|
|
|
|
|
method => $method, |
49
|
|
|
|
|
|
|
params => (ref $_[0] ? $_[0] : [@_]), |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
my $ret = $self->call($self->uri, $obj); |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
0
|
|
|
|
if ( $ret and $ret->is_success ) { |
55
|
0
|
|
|
|
|
|
return $ret->result; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
0
|
0
|
|
|
|
|
Carp::croak ( $ret ? '(Procedure error) ' . $ret->error_message : $self->status_line ); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub create_json_coder { |
65
|
0
|
|
|
0
|
1
|
|
JSON->new->allow_nonref->utf8; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub new { |
70
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
71
|
0
|
0
|
|
|
|
|
my $self = bless {}, (ref $proto ? ref $proto : $proto); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( |
74
|
|
|
|
|
|
|
agent => 'JSON::RPC::Legacy::Client/' . $JSON::RPC::Legacy::Client::VERSION . ' beta ', |
75
|
|
|
|
|
|
|
timeout => 10, |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
$self->ua($ua); |
79
|
0
|
|
|
|
|
|
$self->json( $proto->create_json_coder ); |
80
|
0
|
|
|
|
|
|
$self->version('1.1'); |
81
|
0
|
|
|
|
|
|
$self->content_type('application/json'); |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
return $self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub prepare { |
88
|
0
|
|
|
0
|
1
|
|
my ($self, $uri, $procedures) = @_; |
89
|
0
|
|
|
|
|
|
$self->uri($uri); |
90
|
0
|
|
|
|
|
|
$self->allow_call({ map { ($_ => 1) } @$procedures }); |
|
0
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub call { |
95
|
0
|
|
|
0
|
1
|
|
my ($self, $uri, $obj) = @_; |
96
|
0
|
|
|
|
|
|
my $result; |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
if ($uri =~ /\?/) { |
99
|
0
|
|
|
|
|
|
$result = $self->_get($uri); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else { |
102
|
0
|
0
|
|
|
|
|
Carp::croak "not hashref." unless (ref $obj eq 'HASH'); |
103
|
0
|
|
|
|
|
|
$result = $self->_post($uri, $obj); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
|
my $service = $obj->{method} =~ /^system\./ if ( $obj ); |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$self->status_line($result->status_line); |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if ($result->is_success) { |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
return unless($result->content); # notification? |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
|
if ($service) { |
115
|
0
|
|
|
|
|
|
return JSON::RPC::Legacy::ServiceObject->new($result, $self->json); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
return JSON::RPC::Legacy::ReturnObject->new($result, $self->json); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else { |
121
|
0
|
|
|
|
|
|
return; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub _post { |
127
|
0
|
|
|
0
|
|
|
my ($self, $uri, $obj) = @_; |
128
|
0
|
|
|
|
|
|
my $json = $self->json; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
0
|
|
|
|
$obj->{version} ||= $self->{version} || '1.1'; |
|
|
|
0
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
0
|
0
|
|
|
|
|
if ($obj->{version} eq '1.0') { |
133
|
0
|
|
|
|
|
|
delete $obj->{version}; |
134
|
0
|
0
|
|
|
|
|
if (exists $obj->{id}) { |
135
|
0
|
0
|
|
|
|
|
$self->id($obj->{id}) if ($obj->{id}); # if undef, it is notification. |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
else { |
138
|
0
|
|
0
|
|
|
|
$obj->{id} = $self->id || ($self->id('JSON::RPC::Legacy::Client')); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
else { |
142
|
0
|
0
|
|
|
|
|
$obj->{id} = $self->id if (defined $self->id); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my $content = $json->encode($obj); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$self->ua->post( |
148
|
|
|
|
|
|
|
$uri, |
149
|
|
|
|
|
|
|
Content_Type => $self->{content_type}, |
150
|
0
|
|
|
|
|
|
Content => $content, |
151
|
|
|
|
|
|
|
Accept => 'application/json', |
152
|
|
|
|
|
|
|
); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _get { |
157
|
0
|
|
|
0
|
|
|
my ($self, $uri) = @_; |
158
|
0
|
|
|
|
|
|
$self->ua->get( |
159
|
|
|
|
|
|
|
$uri, |
160
|
|
|
|
|
|
|
Accept => 'application/json', |
161
|
|
|
|
|
|
|
); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
############################################################################## |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
package JSON::RPC::Legacy::ReturnObject; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
$JSON::RPC::Legacy::ReturnObject::VERSION = $JSON::RPC::Legacy::VERSION; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
BEGIN { |
173
|
1
|
|
|
1
|
|
3
|
for my $method (qw/is_success content jsontext version/) { |
174
|
4
|
0
|
|
0
|
|
594
|
eval qq| |
|
0
|
0
|
|
0
|
|
|
|
|
0
|
0
|
|
0
|
|
|
|
|
0
|
0
|
|
0
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub $method { |
176
|
|
|
|
|
|
|
\$_[0]->{$method} = \$_[1] if defined \$_[1]; |
177
|
|
|
|
|
|
|
\$_[0]->{$method}; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub new { |
185
|
0
|
|
|
0
|
|
|
my ($class, $obj, $json) = @_; |
186
|
0
|
|
0
|
|
|
|
my $content = ( $json || JSON->new->utf8 )->decode( $obj->content ); |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
my $self = bless { |
189
|
|
|
|
|
|
|
jsontext => $obj->content, |
190
|
|
|
|
|
|
|
content => $content, |
191
|
|
|
|
|
|
|
}, $class; |
192
|
|
|
|
|
|
|
|
193
|
0
|
0
|
|
|
|
|
$content->{error} ? $self->is_success(0) : $self->is_success(1); |
194
|
|
|
|
|
|
|
|
195
|
0
|
0
|
|
|
|
|
$content->{version} ? $self->version(1.1) : $self->version(0) ; |
196
|
|
|
|
|
|
|
|
197
|
0
|
|
|
|
|
|
$self; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
201
|
0
|
|
|
0
|
|
|
sub is_error { !$_[0]->is_success; } |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub error_message { |
204
|
0
|
0
|
|
0
|
|
|
$_[0]->version ? $_[0]->{content}->{error}->{message} : $_[0]->{content}->{error}; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub result { |
209
|
0
|
|
|
0
|
|
|
$_[0]->{content}->{result}; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
############################################################################## |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
package JSON::RPC::Legacy::ServiceObject; |
216
|
|
|
|
|
|
|
|
217
|
1
|
|
|
1
|
|
10
|
use base qw(JSON::RPC::Legacy::ReturnObject); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
592
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub sdversion { |
221
|
0
|
0
|
|
0
|
|
|
$_[0]->{content}->{sdversion} || ''; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub name { |
226
|
0
|
0
|
|
0
|
|
|
$_[0]->{content}->{name} || ''; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub result { |
231
|
0
|
0
|
|
0
|
|
|
$_[0]->{content}->{summary} || ''; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
1; |
237
|
|
|
|
|
|
|
__END__ |