/usr/share/perl5/JSON/RPC/Parser.pm is in libjson-rpc-perl 1.06-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package JSON::RPC::Parser;
use strict;
use JSON::RPC::Procedure;
use Carp ();
use Plack::Request;
use Class::Accessor::Lite
new => 1,
rw => [ qw(
coder
) ]
;
sub construct_procedure {
my $self = shift;
JSON::RPC::Procedure->new( @_ );
}
sub construct_from_req {
my ($self, $req) = @_;
my $method = $req->method;
my $proc;
if ($method eq 'POST') {
$proc = $self->construct_from_post_req( $req );
} elsif ($method eq 'GET') {
$proc = $self->construct_from_get_req( $req );
} else {
Carp::croak( "Invalid method: $method" );
}
return $proc;
}
sub construct_from_post_req {
my ($self, $req) = @_;
my $request = eval { $self->coder->decode( $req->content ) };
if ($@) {
Carp::croak( "JSON parse error: $@" );
}
my $ref = ref $request;
if ($ref ne 'ARRAY') {
# is not a batch request
return $self->construct_procedure(
method => $request->{method},
id => $request->{id},
params => $request->{params},
jsonrpc => $request->{jsonrpc},
has_id => exists $request->{id},
);
}
my @procs;
foreach my $req ( @$request ) {
Carp::croak( "Invalid parameter") unless ref $req eq 'HASH';
push @procs, $self->construct_procedure(
method => $req->{method},
id => $req->{id},
params => $req->{params},
jsonrpc => $req->{jsonrpc},
has_id => exists $req->{id}, # when not true it's a notification in JSON-RPC 2.0
);
}
return \@procs;
}
sub construct_from_get_req {
my ($self, $req) = @_;
my $params = $req->query_parameters;
my $decoded_params;
if ($params->{params}) {
$decoded_params = eval { $self->coder->decode( $params->{params} ) };
}
return $self->construct_procedure(
method => $params->{method},
id => $params->{id},
params => $decoded_params,
jsonrpc => $params->{jsonrpc},
has_id => exists $params->{id},
);
}
1;
__END__
=head1 NAME
JSON::RPC::Parser - Parse JSON RPC Requests from Plack::Request
=head1 SYNOPSIS
use JSON::RPC::Parser;
my $parser = JSON::RPC::Parser->new(
coder => JSON->new
);
my $procedure = $parser->construct_from_req( $request );
=head1 DESCRIPTION
Constructs a L<JSON::RPC::Procedure> object from a Plack::Request object
=cut
|