| 1 | package Mojolicious::Plugin::JSONRPC2; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = 'v2.0.4'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | # to ensure callback runs on notification | ||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use JSON::RPC2::Server 0.004000; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use constant TIMEOUT => 5*60; # sec | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use constant HTTP_200 => 200; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use constant HTTP_204 => 204; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use constant HTTP_415 => 415; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 3 | my $Type = 'application/json'; | |||||||||||||||||||||||||||||||
| 18 | 3 | my %HEADERS = ( | |||||||||||||||||||||||||||||||
| 19 | 'Content-Type' => qr{\A\s*\Q$Type\E\s*(?:;|\z)}msi, | ||||||||||||||||||||||||||||||||
| 20 | 'Accept' => qr{(?:\A|,)\s*\Q$Type\E\s*(?:[;,]|\z)}msi, | ||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 25 | 1 | my ($self, $app, $conf) = @_; | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | $app->helper(jsonrpc2_headers => sub { return %HEADERS }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | $app->routes->add_shortcut(jsonrpc2 => sub { _shortcut('POST', @_) }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | $app->routes->add_shortcut(jsonrpc2_get => sub { _shortcut('GET', @_) }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 1 | return; | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 9 | ▶ | sub _shortcut { | ||||||||||||||||||||||||||||||
|
Subroutine
_shortcut: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 36 | 9 | my ($method, $r, $path, $server) = @_; | |||||||||||||||||||||||||||||||
| 37 | 9 | ▶ | croak 'usage: $r->jsonrpc2'.($method eq 'GET' ? '_get' : q{}).'("/rpc/path", JSON::RPC2::Server->new)' | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $server and $server->isa("JSON::RPC2::Server")
MC/DC100%2/2
ref $server and $server->isa("JSON::RPC2::Server")
ref $server
$server->isa("JSON::RPC2::Server")
Truth table100%3/3
A and B
A= ref $serverB= $server->isa("JSON::RPC2::Server")
| |||||||||||||||||||||||||||||||||
| 38 | if !(ref $server && $server->isa('JSON::RPC2::Server')); | ||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | return $r->any([$method] => $path, [format => 0], sub { _srv($server, @_) }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 15 | ▶ | sub _srv { | ||||||||||||||||||||||||||||||
|
Subroutine
_srv: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||
| 43 | 15 | my ($server, $c) = @_; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 15 | ▶ | if (($c->req->headers->content_type // q{}) !~ /$HEADERS{'Content-Type'}/ms) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$c->req->headers->content_type // ""
MC/DC100%1/1
$c->req->headers->content_type // ""
$c->req->headers->content_type
Truth table100%2/2
A
A= $c->req->headers->content_type
| |||||||||||||||||||||||||||||||||
| 46 | 5 | return $c->render(status => HTTP_415, data => q{}); | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | if (($c->req->headers->accept // q{}) !~ /$HEADERS{'Accept'}/ms) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$c->req->headers->accept // ""
MC/DC0%0/1
$c->req->headers->accept // ""
$c->req->headers->accept
Truth table50%1/2
A
A= $c->req->headers->accept
| |||||||||||||||||||||||||||||||||
| 49 | 2 | return $c->render(status => HTTP_415, data => q{}); | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 8 | $c->res->headers->content_type($Type); | |||||||||||||||||||||||||||||||
| 53 | 8 | $c->render_later; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 8 | ▶ | my $timeout = $c->stash('jsonrpc2.timeout') || TIMEOUT; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$c->stash('jsonrpc2.timeout') || 300
MC/DC100%1/1
$c->stash('jsonrpc2.timeout') || 300
$c->stash('jsonrpc2.timeout')
Truth table100%2/2
A
A= $c->stash('jsonrpc2.timeout')
| |||||||||||||||||||||||||||||||||
| 56 | 8 | $c->inactivity_timeout($timeout); | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 8 | my $request; | |||||||||||||||||||||||||||||||
| 59 | 8 | ▶ | if ($c->req->method eq 'GET') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 2 | $request = $c->req->query_params->to_hash; | |||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if (exists $request->{params}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | 1 | $request->{params} = eval { decode_json($request->{params}) }; | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||||||
| 65 | 6 | $request = eval { decode_json($c->req->body) }; | |||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 7 | ▶ | $server->execute($request, sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 69 | 7 | my ($json_response) = @_; | |||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | my $status = $json_response ? HTTP_200 : HTTP_204; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 7 | $c->render(status => $status, data => $json_response); | |||||||||||||||||||||||||||||||
| 72 | 8 | }); | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 8 | return; | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 3 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||||||