| 1 | package Mojolicious::Plugin::Qooxdoo::JsonRpcController; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Mojo::JSON qw(encode_json decode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Mojo::Base 'Mojolicious::Controller'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mojo::Promise; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 15 | ▶ | has toUTF8 => sub { find_encoding('utf8') }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = '1.1.0'; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | has 'service'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | has 'crossDomain'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | has 'requestId'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | has 'methodName'; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | has 'rpcParams'; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 24 | ▶ | sub dispatch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dispatch: called CC=27 SCAR=33.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # We have to differentiate between POST and GET requests, because | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # the data is not sent in the same place.. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 24 | my $log = $self->log; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # send warnings to log file preserving the origin | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | local $SIG{__WARN__} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my $message = shift; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | $message =~ s/\n$//; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | @_ = ($log, $message); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | goto &Mojo::Log::warn; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 24 | }; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 24 | my $data; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 24 | for ( $self->req->method ){ | |||||||||||||||||||||||||||||||||||||||||
| 44 | 24 | ▶ | /^POST$/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Data comes as JSON object, so fetch a reference to it | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 20 | ▶ | my $type = $self->req->headers->content_type//'*missing header*'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->req->headers->content_type // "*missing header*"
MC/DC100%1/1
$self->req->headers->content_type // "*missing header*"
$self->req->headers->content_type
Truth table100%2/2
A
A= $self->req->headers->content_type
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 20 | ▶ | if ($type !~ m{^application/json\b}i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | $log->error("unexpected Content-Type header: $type (should be application/json)"); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | $self->render(text => "invalid payload format announcement", status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 19 | $data = eval { decode_json($self->req->body) }; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 19 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | my $error = "Invalid json string: " . $@; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | $self->render(text => "invalid payload format", status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 18 | $self->requestId($data->{id}); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 18 | $self->crossDomain(0); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 18 | last; | |||||||||||||||||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | /^GET$/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | # not checking the content header here since we are trying to | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # to a cross domain request ... all sorts of things may have | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # happened to the data since this | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | $data= eval { decode_json($self->param('_ScriptTransport_data')) }; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | my $error = "Invalid json string: " . $@; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | $self->requestId($self->param('_ScriptTransport_id')) ; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | $self->crossDomain(1); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | my $error = "request must be POST or GET. Can't handle '".$self->req->method."'"; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # Detect protocol: a 'jsonrpc' member selects strict JSON-RPC 2.0; | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # its absence keeps the legacy qooxdoo "qx1" behaviour intact. Stored as a | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # private instance field (no 'has' accessor) so it is not part of the | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # public interface, yet the public render methods can still read it when a | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # subclass calls them directly (e.g. from a render_later callback). | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 20 | ▶ | $self->{_jsonRpc20} = defined $data->{jsonrpc} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 20 | ▶ | if (not defined $self->requestId){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | my $error = "Missing 'id' property in JsonRPC request."; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 19 | my $method; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 19 | ▶ | if ($self->{_jsonRpc20}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | # --- JSON-RPC 2.0 --- | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | ▶ | if ($data->{jsonrpc} ne '2.0') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | my $error = "Invalid 'jsonrpc' version (must be \"2.0\")."; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | ▶ | if ($self->req->method ne 'POST') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | my $error = "JSON-RPC 2.0 requests must be POST."; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 6 | $method = $data->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 6 | ▶ | if (not defined $method) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $error = "Missing 'method' property in JsonRPC request."; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # --- legacy qx1 --- | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # Check if service property is available | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 11 | ▶ | $data->{service} or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | my $error = "Missing service property in JsonRPC request."; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # Check if method is specified in the request | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 10 | ▶ | $method = $data->{method} or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | my $error = "Missing method property in JsonRPC request."; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 138 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 15 | $self->methodName($method); | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 15 | ▶ | $self->rpcParams($data->{params} // []); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$data->{"params"} // []
MC/DC100%1/1
$data->{"params"} // []
$data->{"params"}
Truth table100%2/2
A
A= $data->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | # invocation of method in class according to request | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 15 | my $reply = eval { | |||||||||||||||||||||||||||||||||||||||||
| 146 | # make sure there are not foreign signal handlers | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # messing with our problems | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 15 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||||||||||||||||
| 149 | # Getting available services from stash | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | die { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | origin => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | message => "service ".$data->{service}." not available", | ||||||||||||||||||||||||||||||||||||||||||
| 155 | code=> 2 | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 15 | ▶ | } if not $self->{_jsonRpc20} and not $self->service eq $data->{service}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"_jsonRpc20"} or $self->service eq $data->{"service"}
MC/DC100%2/2
$self->{"_jsonRpc20"} or $self->service eq $data->{"service"}
$self->{"_jsonRpc20"}
$self->service eq $data->{"service"}
Truth table100%3/3
A or B
A= $self->{"_jsonRpc20"}B= $self->service eq $data->{"service"}
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 14 | ▶ | die { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | origin => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 160 | message => "your rpc service controller (".ref($self).") must provide an allow_rpc_access method", | ||||||||||||||||||||||||||||||||||||||||||
| 161 | code=> 2 | ||||||||||||||||||||||||||||||||||||||||||
| 162 | } unless $self->can('allow_rpc_access'); | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 14 | ▶ | die { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | origin => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | message => "rpc access to method $method denied", | ||||||||||||||||||||||||||||||||||||||||||
| 168 | code=> 6 | ||||||||||||||||||||||||||||||||||||||||||
| 169 | } unless $self->allow_rpc_access($method); | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 12 | ▶ | die { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | origin => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | message => "method $method does not exist.", | ||||||||||||||||||||||||||||||||||||||||||
| 174 | code=> 4 | ||||||||||||||||||||||||||||||||||||||||||
| 175 | } if not $self->can($method); | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 12 | $self->logRpcCall($method,dclone($self->rpcParams)); | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | # reply | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 12 | my $params = $self->rpcParams; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 12 | ▶ | if (ref $params eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 11 | return $self->$method(@$params); # positional params | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | elsif (ref $params eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | return $self->$method($params); # named params -> single hashref | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | die { | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | origin => 1, | |||||||||||||||||||||||||||||||||||||||||
| 190 | message => "'params' must be an array or an object", | ||||||||||||||||||||||||||||||||||||||||||
| 191 | code => 4 | ||||||||||||||||||||||||||||||||||||||||||
| 192 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 193 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 15 | ▶ | if ($@){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 5 | $self->renderJsonRpcError($@); | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 10 | ▶ | if (eval { $reply->isa('Mojo::Promise') }){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | $reply->then( | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | my $ret = shift; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 1 | $self->renderJsonRpcResult($ret); | |||||||||||||||||||||||||||||||||||||||||
| 203 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | my $err = shift; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | $self->renderJsonRpcError($err); | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 2 | $self->render_later; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # do NOT render if | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 8 | ▶ | if (not $self->stash->{'mojo.rendered'}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 4 | $self->renderJsonRpcResult($reply); | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 12 | ▶ | sub logRpcCall { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
logRpcCall: called CC=4 SCAR=24.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 12 | ▶ | if ($self->log->level eq 'debug'){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my $method = shift; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $request = encode_json(shift); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | if (not $ENV{MOJO_QX_FULL_RPC_DETAILS}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | if (length($request) > 60){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | $request = substr($request,0,60) . ' [...]'; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | $self->log->debug("call $method(".$request.")"); | |||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 7 | ▶ | sub renderJsonRpcResult { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
renderJsonRpcResult: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 7 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||
| 237 | my $reply = $self->{_jsonRpc20} | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 7 | ▶ | ? { jsonrpc => '2.0', id => $self->requestId, result => $data } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | : { id => $self->requestId, result => $data }; | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 7 | $self->logRpcReturn(dclone($reply)); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 7 | $self->finalizeJsonRpcReply(encode_json($reply)); | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 7 | ▶ | sub logRpcReturn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
logRpcReturn: called CC=4 SCAR=24.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 7 | ▶ | if ($self->log->level eq 'debug'){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | my $debug = encode_json(shift); | |||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | if (not $ENV{MOJO_QX_FULL_RPC_DETAILS}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | if (length($debug) > 60){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | $debug = substr($debug,0,60) . ' [...]'; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $self->log->debug("return ".$debug); | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | 8 | ▶ | sub renderJsonRpcError { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
renderJsonRpcError: called CC=13 SCAR=29.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 8 | my $exception = shift; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 8 | my ($origin, $message, $code); | |||||||||||||||||||||||||||||||||||||||||
| 261 | 8 | for (ref $exception){ | |||||||||||||||||||||||||||||||||||||||||
| 262 | 8 | ▶ | /HASH/ && $exception->{message} && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/HASH/u and $exception->{"message"}
MC/DC50%1/2
/HASH/u and $exception->{"message"}
/HASH/u
$exception->{"message"}
Truth table66%2/3
A and B
A= /HASH/uB= $exception->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 3 | ▶ | $origin = $exception->{origin} // 2; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$exception->{"origin"} // 2
MC/DC0%0/1
$exception->{"origin"} // 2
$exception->{"origin"}
Truth table50%1/2
A
A= $exception->{"origin"}
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | $message = $exception->{message}; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | $code = $exception->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 3 | last; | |||||||||||||||||||||||||||||||||||||||||
| 267 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 5 | ▶ | /.+/ && $exception->can('message') && $exception->can('code') && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
/.+/u and $exception->can("message") and $exception->can("code")
/.+/u and $exception->can("message")
MC/DC0%0/3
/.+/u and $exception->can("message") and $exception->can("code")
/.+/u
$exception->can("message")
$exception->can("code")
Truth table25%1/4
A and B and C
A= /.+/uB= $exception->can("message")C= $exception->can("code")
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 5 | $origin = 2; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 5 | $message = $exception->message(); | |||||||||||||||||||||||||||||||||||||||||
| 271 | 5 | $code = $exception->code(); | |||||||||||||||||||||||||||||||||||||||||
| 272 | 5 | last; | |||||||||||||||||||||||||||||||||||||||||
| 273 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | $self->log->error("Error while processing " . ($self->service // '') . "::" . $self->methodName . ": $exception"); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->service // ""
MC/DC0%0/1
$self->service // ""
$self->service
Truth table0%0/2
A
A= $self->service
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $origin = 2; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | $message = "Couldn't process request"; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | $code = 9999; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 8 | ▶ | $self->log->error("JsonRPC error sent to client: '" . ($code // 'undef') . ": $message'"); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$code // "undef"
MC/DC0%0/1
$code // "undef"
$code
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 8 | my $reply; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 8 | ▶ | if ($self->{_jsonRpc20}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | # JSON-RPC 2.0: error.code must be an integer; non-standard 'origin' | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # is carried inside the permitted 'data' member. | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 3 | ▶ | $reply = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | jsonrpc => '2.0', | ||||||||||||||||||||||||||||||||||||||||||
| 287 | id => $self->requestId, | ||||||||||||||||||||||||||||||||||||||||||
| 288 | error => { | ||||||||||||||||||||||||||||||||||||||||||
| 289 | code => defined $code ? int($code) : 9999, | ||||||||||||||||||||||||||||||||||||||||||
| 290 | message => $message, | ||||||||||||||||||||||||||||||||||||||||||
| 291 | data => { origin => $origin }, | ||||||||||||||||||||||||||||||||||||||||||
| 292 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 293 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 5 | ▶ | $reply = { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$origin || 2
MC/DC0%0/1
$origin || 2
$origin
Truth table50%1/2
A
A= $origin
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | id => $self->requestId, | ||||||||||||||||||||||||||||||||||||||||||
| 298 | error => { origin => $origin || 2, message => $message, code => $code }, | ||||||||||||||||||||||||||||||||||||||||||
| 299 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 8 | $self->finalizeJsonRpcReply(encode_json($reply)); | |||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 15 | ▶ | sub finalizeJsonRpcReply { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
finalizeJsonRpcReply: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 15 | my $reply = shift; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 15 | ▶ | if ($self->crossDomain){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | # for GET requests, qooxdoo expects us to send a javascript method | ||||||||||||||||||||||||||||||||||||||||||
| 309 | # and to wrap our json a litte bit more | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 1 | $self->res->headers->content_type('application/javascript; charset=utf-8'); | |||||||||||||||||||||||||||||||||||||||||
| 311 | 1 | $reply = "qx.io.remote.transport.Script._requestFinished( ".$self->requestId.", " . $reply . ");"; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 14 | $self->res->headers->content_type('application/json; charset=utf-8'); | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | # the render takes care of encoding the output, so make sure we re-decode | ||||||||||||||||||||||||||||||||||||||||||
| 316 | # the json stuf | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 15 | $self->render(text => $self->toUTF8->decode($reply)); | |||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 24 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 24 | local($., $@, $!, $^E, $?); | |||||||||||||||||||||||||||||||||||||||||
| 322 | 24 | ▶ | return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 24 | $self->log->debug("Destroying ".__PACKAGE__); | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | Mojolicious::Plugin::Qooxdoo::JsonRpcController - A controller base class for Qooxdoo JSON-RPC Calls | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | # lib/MyApp.pm | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | use base 'Mojolicious'; | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | sub startup { | ||||||||||||||||||||||||||||||||||||||||||
| 342 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | # add a route to the Qooxdoo dispatcher and route to it | ||||||||||||||||||||||||||||||||||||||||||
| 345 | my $r = $self->routes; | ||||||||||||||||||||||||||||||||||||||||||
| 346 | $r->route('/RpcService') -> to( | ||||||||||||||||||||||||||||||||||||||||||
| 347 | controller => 'MyJsonRpcController', | ||||||||||||||||||||||||||||||||||||||||||
| 348 | action => 'dispatch', | ||||||||||||||||||||||||||||||||||||||||||
| 349 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | package MyApp::MyJsonRpcController; | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | use Mojo::Base qw(Mojolicious::Plugin::Qooxdoo::JsonRpcController); | ||||||||||||||||||||||||||||||||||||||||||
| 355 | use Mojo::Promise; | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | has service => sub { 'Test' }; | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | out %allow = ( echo => 1, bad => 1, async => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | sub allow_rpc_access { | ||||||||||||||||||||||||||||||||||||||||||
| 362 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 363 | my $method = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 364 | return $allow{$method};; | ||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | sub echo { | ||||||||||||||||||||||||||||||||||||||||||
| 368 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 369 | my $text = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 370 | return $text; | ||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | sub bad { | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | die MyException->new(code=>1323,message=>'I died'); | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | die { code => 1234, message => 'another way to die' }; | ||||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | sub async { | ||||||||||||||||||||||||||||||||||||||||||
| 381 | my $self=shift; | ||||||||||||||||||||||||||||||||||||||||||
| 382 | $self->render_later; | ||||||||||||||||||||||||||||||||||||||||||
| 383 | xyzWithCallback(callback=>sub{ | ||||||||||||||||||||||||||||||||||||||||||
| 384 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 385 | local $SIG{__DIE__}; | ||||||||||||||||||||||||||||||||||||||||||
| 386 | $self->renderJsonRpcResult('Late Reply'); | ||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||
| 388 | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||
| 389 | $self->renderJsonRpcError($@); | ||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | sub async_p { | ||||||||||||||||||||||||||||||||||||||||||
| 395 | my $self=shift; | ||||||||||||||||||||||||||||||||||||||||||
| 396 | my $p = Mojo::Promise->new; | ||||||||||||||||||||||||||||||||||||||||||
| 397 | xyzWithCallback(callback => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 398 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 399 | local $SIG{__DIE__}; | ||||||||||||||||||||||||||||||||||||||||||
| 400 | $p->resolve('Late Reply'); | ||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||
| 402 | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||
| 403 | $p->reject($@); | ||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 406 | return $p; | ||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | package MyException; | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||||
| 412 | has 'code'; | ||||||||||||||||||||||||||||||||||||||||||
| 413 | has 'message'; | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | All you have todo to process incoming JSON-RPC requests from a qooxdoo | ||||||||||||||||||||||||||||||||||||||||||
| 419 | application, is to make your controller a child of | ||||||||||||||||||||||||||||||||||||||||||
| 420 | L<Mojolicious::Plugin::Qooxdoo::JsonRpcController>. And then route all | ||||||||||||||||||||||||||||||||||||||||||
| 421 | incoming requests to the inherited dispatch method in the new controller. | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | If you want your Mojolicious app to also serve the qooxdoo application | ||||||||||||||||||||||||||||||||||||||||||
| 424 | files, you can use L<Mojolicous::Plugin::Qooxdoo> to have everything setup for you. | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | =head2 Exception processing | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | Errors within the methods of your controller are handled by an eval call, | ||||||||||||||||||||||||||||||||||||||||||
| 429 | encapsulating the method call. So if you run into trouble, just C<die>. If | ||||||||||||||||||||||||||||||||||||||||||
| 430 | if you die with a object providing a C<code> and C<message> property or with | ||||||||||||||||||||||||||||||||||||||||||
| 431 | a hash containing a C<code> and C<message> key, this information will be | ||||||||||||||||||||||||||||||||||||||||||
| 432 | used to populate the JSON-RPC error object returned to the caller. | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | =head2 Security | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | The C<dispatcher> method provided by | ||||||||||||||||||||||||||||||||||||||||||
| 437 | L<Mojolicious::Plugin::Qooxoo::JsonRpcController> calls the C<allow_rpc_access> | ||||||||||||||||||||||||||||||||||||||||||
| 438 | method to check if rpc access should be allowed. The result of this request | ||||||||||||||||||||||||||||||||||||||||||
| 439 | is NOT cached, so you can use this method to provide dynamic access control | ||||||||||||||||||||||||||||||||||||||||||
| 440 | or even do initialization tasks that are required before handling each | ||||||||||||||||||||||||||||||||||||||||||
| 441 | request. | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | =head2 Async Processing | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | If you want to do async data processing, call the C<render_later> method | ||||||||||||||||||||||||||||||||||||||||||
| 446 | to let the dispatcher know that it should not bother with trying to render anyting. | ||||||||||||||||||||||||||||||||||||||||||
| 447 | In the callback, call the C<renderJsonRpcResult> method to render your result. Note | ||||||||||||||||||||||||||||||||||||||||||
| 448 | that you have to take care of any exceptions in the callback yourself and use | ||||||||||||||||||||||||||||||||||||||||||
| 449 | the C<renderJsonRpcError> method to send the exception to the client. | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | =head2 Mojo::Promise Support | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | If your method returns a promise, all will workout as expected. See the example above. | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | =head2 Debugging | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | To see full details of your rpc request and the answers sent back to the | ||||||||||||||||||||||||||||||||||||||||||
| 458 | browser in your debug log, set the MOJO_QX_FULL_RPC_DETAILS environment | ||||||||||||||||||||||||||||||||||||||||||
| 459 | variable to 1. Otherwise you will only see the first 60 characters even | ||||||||||||||||||||||||||||||||||||||||||
| 460 | when logging at debug level. | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | NOTE: At debug log level will log the payload of RPC calls will be logged. This might | ||||||||||||||||||||||||||||||||||||||||||
| 463 | lead to sensitive data like passwords being logged. | ||||||||||||||||||||||||||||||||||||||||||
| 464 | You might want to override the logRpc* methods appropriately in your application. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | S<Matthias Bloch, E<lt>matthias@puffin.chE<gt>>, | ||||||||||||||||||||||||||||||||||||||||||
| 469 | S<Tobias Oetiker, E<lt>tobi@oetiker.chE<gt>>. | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | This Module is sponsored by OETIKER+PARTNER AG. | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | Copyright (C) 2010,2013,2024 | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 480 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||||||||||||||||
| 481 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | =cut | ||||||||||||||||||||||||||||||||||||||||||