blib/lib/Dancer/Plugin/RPC/RESTISH.pm

filter: Statementstmt 100% 185 / 185 Branchbran 96.1% 50 / 52 Conditioncond 81.6% 40 / 49 MC/DCmcdc 70.0% 21 / 30 Subroutinesub 100% 20 / 20 Podpod 100% 2 / 2 totaltotal 94.0% 318 / 338 CC 47 SCAR 32.8
CC
47
Cov
96%
__ANON__
41.3
__ANON__
7.0
BEGIN
1.0
CRAP 26.7
↑ Summary
1 package Dancer::Plugin::RPC::RESTISH;
2 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '1.01_01';
8  
9 3 ▶use constant PLUGIN_NAME => 'restish';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Dancer::RPCPlugin::CallbackResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Dancer::RPCPlugin::DispatchFromConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Dancer::RPCPlugin::DispatchFromPod;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Dancer::RPCPlugin::DispatchItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Dancer::RPCPlugin::DispatchMethodList;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Dancer::RPCPlugin::ErrorResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Dancer::RPCPlugin::FlattenData;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Dancer::RPCPlugin::PluginNames;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 Dancer::RPCPlugin::PluginNames->new->add_names(PLUGIN_NAME);
21 Dancer::RPCPlugin::ErrorResponse->register_error_responses(
22 PLUGIN_NAME ,=> {
23 -32500 => 500,
24 -32601 => 403,
25 default => 400,
26 },
27 8 ▶ sprintf("as_%s_error", PLUGIN_NAME) => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 8 my $self = shift;
29  
30 return {
31 8 error_code => $self->error_code,
32 error_message => $self->error_message,
33 error_data => $self->error_data,
34 };
35 }
36 3 );
37  
38 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # A char between the HTTP-Method and the REST-route
41 3 our $_HM_POSTFIX = '@';
42  
43 3 my %dispatch_builder_map = (
44 pod => \&build_dispatcher_from_pod,
45 config => \&build_dispatcher_from_config,
46 );
47  
48 16 ▶register PLUGIN_NAME ,=> sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
49 16 my ($self, $endpoint, $arguments) = plugin_args(@_);
50 16 ▶ my $allow_origin = $arguments->{cors_allow_origin} || '';
Condition100%2/2
$arguments->{'cors_allow_origin'} || ''
l!l
214
MC/DC100%1/1
$arguments->{'cors_allow_origin'} || ''
$arguments->{'cors_allow_origin'}
Truth table100%2/2
A
A= $arguments->{'cors_allow_origin'}
Aresult
11
00
51 16 my @allowed_origins = split(' ', $allow_origin);
52  
53 16 my $publisher;
54 GIVEN: {
55 16 ▶ local $_ = $arguments->{publish} // 'config';
Condition100%2/2
$arguments->{"publish"} // "config"
l!l
151
MC/DC100%1/1
$arguments->{"publish"} // "config"
$arguments->{"publish"}
Truth table100%2/2
A
A= $arguments->{"publish"}
Aresult
11
00
56 16 ▶ exists($dispatch_builder_map{$_}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $dispatch_builder_map{$_} and do { $publisher = $dispatch_builder_map{$_}; $arguments->{"arguments"} = plugin_setting() if $_ eq "config"; last GIVEN } 5 11 16
57 5 $publisher = $dispatch_builder_map{$_};
58 5 ▶ $arguments->{arguments} = plugin_setting() if $_ eq 'config';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "config" 1 4 5
59 5 last GIVEN;
60 };
61 11 do {
62 11 $publisher = $_;
63 };
64 }
65 16 my $dispatcher = $publisher->($arguments->{arguments}, $endpoint);
66  
67 16 my $lister = Dancer::RPCPlugin::DispatchMethodList->new();
68 $lister->set_partial(
69 protocol => PLUGIN_NAME,
70 endpoint => $endpoint,
71 16 methods => [ sort keys %{ $dispatcher } ],
72 );
73  
74 my $code_wrapper = $arguments->{code_wrapper}
75 ? $arguments->{code_wrapper}
76 19 ▶ : sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
77 19 my $code = shift;
78 19 my $pkg = shift;
79 19 $code->(@_);
80 16 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$arguments->{'code_wrapper'} ? : 3 13 16
81 16 my $callback = $arguments->{callback};
82  
83 16 debug("Starting restish-handler build: ", $lister);
84 33 ▶ my $handle_call = sub {
Subroutine __ANON__: called CC=41 SCAR=37.2
85 # we'll only handle requests that have either a JSON body or no body
86 33 my ($ct) = (split /;\s*/, request->content_type, 2);
87 33 ▶ if (request->body && ($ct ne 'application/json')) {
Branch: 1 branch
BranchTrueFalseTotal
if (request()->body and $ct ne "application/json") 1 32 33
Condition100%3/3
request()->body and $ct ne "application/json"
!ll&&!rl&&r
2661
MC/DC100%2/2
request()->body and $ct ne "application/json"
request()->body $ct ne "application/json"
Truth table100%3/3
A and B
A= request()->bodyB= $ct ne "application/json"
ABresult
0X0
100
111
88 1 pass();
89 }
90  
91 32 my $http_method = uc(request->method);
92 32 my $request_path = request->path;
93  
94 32 ▶ my $has_origin = request->header('Origin') || '';
Condition100%2/2
request()->header('Origin') || ''
l!l
1121
MC/DC100%1/1
request()->header('Origin') || ''
request()->header('Origin')
Truth table100%2/2
A
A= request()->header('Origin')
Aresult
11
00
95 my $allowed_origin = ($allow_origin eq '*')
96 32 ▶ || grep { $_ eq $has_origin } @allowed_origins;
Condition66%2/3
$allow_origin eq '*' || grep({$_ eq $has_origin;} @allowed_origins)
l!l&&r!l&&!r
10022
MC/DC50%1/2
$allow_origin eq '*' || grep({$_ eq $has_origin;} @allowed_origins)
$allow_origin eq '*' grep({$_ eq $has_origin;} @allowed_origins)
Truth table66%2/3
A || B
A= $allow_origin eq '*'B= grep({$_ eq $has_origin;} @allowed_origins)
ABresult
1X1
011
000
97  
98 32 ▶ my $is_preflight = $has_origin && ($http_method eq 'OPTIONS');
Condition100%3/3
$has_origin && $http_method eq 'OPTIONS'
!ll&&!rl&&r
2156
MC/DC100%2/2
$has_origin && $http_method eq 'OPTIONS'
$has_origin $http_method eq 'OPTIONS'
Truth table100%3/3
A && B
A= $has_originB= $http_method eq 'OPTIONS'
ABresult
0X0
100
111
99  
100 # with CORS, we do not allow mismatches on Origin
101 32 ▶ if ($allow_origin && $has_origin && !$allowed_origin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_origin and $has_origin and not $allowed_origin) 1 31 32
Condition83%5/6
$allow_origin and $has_origin and not $allowed_origin
!ll&&!rl&&r
21101
$allow_origin and $has_origin
!ll&&!rl&&r
21011
MC/DC66%2/3
$allow_origin and $has_origin and not $allowed_origin
$allow_origin $has_origin not $allowed_origin
Truth table75%3/4
A and B and not(C)
A= $allow_originB= $has_originC= not $allowed_origin
ABCresult
0XX0
10X0
1100
1111
102 1 debug("[RESTISH-CORS] '$has_origin' not allowed ($allow_origin)");
103 1 status(403);
104 1 content_type('text/plain');
105 1 return "[CORS] $has_origin not allowed";
106 }
107  
108 # method_name should exist...
109 # we need to turn 'GET@some_resource/:id' into a regex that we can use
110 # to match this request so we know what thing to call...
111 31 (my $method_name = $request_path) =~ s{^$endpoint/}{};
112 31 my ($found_match, $found_method);
113 my @sorted_dispatch_keys = sort {
114 # reverse length of the regex we use to match
115 31 my ($am, $ar) = split(/\b$_HM_POSTFIX/, $a);
116 73 $ar =~ s{/:\w+}{/[^/]+};
117 73 my ($bm, $br) = split(/\b$_HM_POSTFIX/, $b);
118 73 $br =~ s{/:\w+}{/[^/]+};
119 73 length($br) <=> length($ar)
120 } keys %$dispatcher;
121  
122 31 ▶ my $preflight_method = $is_preflight
Branch: 1 branch
BranchTrueFalseTotal
$is_preflight ? : 5 26 31
Condition100%2/2
request()->header("Access-Control-Request-Method") // "GET"
l!l
32
MC/DC100%1/1
request()->header("Access-Control-Request-Method") // "GET"
request()->header("Access-Control-Request-Method")
Truth table100%2/2
A
A= request()->header("Access-Control-Request-Method")
Aresult
11
00
123 ? request->header('Access-Control-Request-Method') // 'GET'
124 : undef;
125  
126 31 my $check_for_method;
127 31 for my $plugin_route (@sorted_dispatch_keys) {
128 63 my ($hm, $route) = split(/\b$_HM_POSTFIX/, $plugin_route, 2);
129 63 $hm = uc($hm);
130  
131 63 ▶ if ($allow_origin && $is_preflight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_origin and $is_preflight) { } 14 49 63
Condition100%3/3
$allow_origin and $is_preflight
!ll&&!rl&&r
351414
MC/DC100%2/2
$allow_origin and $is_preflight
$allow_origin $is_preflight
Truth table100%3/3
A and B
A= $allow_originB= $is_preflight
ABresult
0X0
100
111
132 14 $check_for_method = $preflight_method;
133 }
134 else {
135 49 $check_for_method = $http_method;
136 }
137 63 ▶ next if $hm ne $check_for_method;
Branch: 1 branch
BranchTrueFalseTotal
if $hm ne $check_for_method 28 35 63
138  
139 35 (my $route_match = $route) =~ s{:\w+}{[^/]+}g;
140 35 debug("[restish_find_route($check_for_method => $method_name, $route ($route_match)");
141 35 ▶ if ($method_name =~ m{^$route_match$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^$route_match$/) 29 6 35
142 29 $found_match = $plugin_route;
143 29 $found_method = $hm;
144 29 last;
145 }
146 }
147  
148 31 ▶ if (! $found_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_match) 2 29 31
149 2 ▶ if ($allow_origin && $is_preflight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_origin and $is_preflight) 1 1 2
Condition66%2/3
$allow_origin and $is_preflight
!ll&&!rl&&r
101
MC/DC50%1/2
$allow_origin and $is_preflight
$allow_origin $is_preflight
Truth table66%2/3
A and B
A= $allow_originB= $is_preflight
ABresult
0X0
100
111
150 1 my $msg = "[CORS-preflight] failed for $preflight_method => $request_path";
151 1 debug($msg);
152 1 status(200); # maybe 403?
153 1 content_type 'text/plain';
154 1 return $msg;
155 }
156 1 warning("$http_method => $request_path ($method_name) not found, pass()");
157 1 pass();
158 }
159 29 debug("[restish_found_route($http_method => $request_path ($method_name) ($found_match)");
160  
161 # Send the CORS 'Access-Control-Allow-Origin' header
162 29 ▶ if ($allow_origin && $has_origin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_origin and $has_origin) 9 20 29
Condition66%2/3
$allow_origin and $has_origin
!ll&&!rl&&r
2009
MC/DC50%1/2
$allow_origin and $has_origin
$allow_origin $has_origin
Truth table66%2/3
A and B
A= $allow_originB= $has_origin
ABresult
0X0
100
111
163 9 ▶ my $allow_now = $allow_origin eq '*' ? '*' : $has_origin;
Branch: 1 branch
BranchTrueFalseTotal
$allow_origin eq '*' ? : 9 0 9
164 9 header('Access-Control-Allow-Origin' => $allow_now);
165 }
166  
167 29 ▶ if ($is_preflight) { # Send more CORS headers and return.
Branch: 1 branch
BranchTrueFalseTotal
if ($is_preflight) 4 25 29
168 4 debug("[CORS] preflight-request: $request_path ($method_name)");
169 4 status(200);
170 4 ▶ header(
Branch: 1 branch
BranchTrueFalseTotal
if request()->header("Access-Control-Request-Headers") 0 4 4
171 'Access-Control-Allow-Headers',
172 request->header('Access-Control-Request-Headers')
173 ) if request->header('Access-Control-Request-Headers');
174  
175 4 header('Access-Control-Allow-Methods' => $found_method);
176 4 content_type 'text/plain';
177 4 return "";
178 }
179  
180 25 content_type 'application/json';
181 25 ▶ my $method_args = request->body
Branch: 1 branch
BranchTrueFalseTotal
request()->body ? : 6 19 25
182 ? from_json(request->body)
183 : { };
184 25 ▶ my $route_args = request->params('route') // { };
Condition50%1/2
request()->params("route") // {}
l!l
250
MC/DC0%0/1
request()->params("route") // {}
request()->params("route")
Truth table50%1/2
A
A= request()->params("route")
Aresult
11
00
185 25 my $query_args = request->params('query');
186  
187 # We'll merge method_args and route_args, where route_args win:
188 25 $method_args = {
189 %$method_args,
190 %$route_args,
191 %$query_args,
192 };
193 25 debug("[handling_restish_request('$request_path' via '$found_match')] ", $method_args);
194  
195 25 my $start_request = time();
196 25 my Dancer::RPCPlugin::CallbackResult $continue = eval {
197 25 (my $match_re = $found_match) =~ s{:\w+}{[^/]+}g;
198 25 local $Dancer::RPCPlugin::ROUTE_INFO = {
199 plugin => PLUGIN_NAME,
200 route_matched => $found_match,
201 matched_re => $match_re,
202 endpoint => $endpoint,
203 rpc_method => $method_name,
204 full_path => request->path,
205 http_method => $http_method,
206 };
207 25 ▶ $callback
Branch: 1 branch
BranchTrueFalseTotal
$callback ? : 8 17 25
208 ? $callback->(request(), $method_name, $method_args)
209 : callback_success();
210 };
211 25 my $error = $@;
212 25 my $response;
213 25 ▶ if ($error) {
Branch: 3 branches
BranchTrueFalseTotal
if ($error) { } 1 24 25
elsif (not builtin::blessed($continue) && $continue->isa('Dancer::RPCPlugin::CallbackResult')) { } 1 23 24
elsif (builtin::blessed($continue) and not $continue->success) { } 2 21 23
214 1 my $error_response = error_response(
215 error_code => -32500,
216 error_message => $error,
217 error_data => $method_args,
218 );
219 1 status $error_response->return_status('restish');
220 1 $response = $error_response->as_restish_error;
221 }
222 ▶ elsif (!blessed($continue) || !$continue->isa('Dancer::RPCPlugin::CallbackResult')) {
Condition66%2/3
builtin::blessed($continue) && $continue->isa('Dancer::RPCPlugin::CallbackResult')
!ll&&!rl&&r
0123
MC/DC50%1/2
builtin::blessed($continue) && $continue->isa('Dancer::RPCPlugin::CallbackResult')
builtin::blessed($continue) $continue->isa('Dancer::RPCPlugin::CallbackResult')
Truth table66%2/3
A && B
A= builtin::blessed($continue)B= $continue->isa('Dancer::RPCPlugin::CallbackResult')
ABresult
0X0
100
111
223 1 my $error_response = error_response(
224 error_code => -32603,
225 error_message => "Internal error: 'callback_result' wrong class "
226 . blessed($continue),
227 error_data => $method_args,
228 );
229 1 status($error_response->return_status('restish'));
230 1 $response = $error_response->as_restish_error;
231 }
232 ▶ elsif (blessed($continue) && !$continue->success) {
Condition66%2/3
builtin::blessed($continue) and not $continue->success
!ll&&!rl&&r
0212
MC/DC50%1/2
builtin::blessed($continue) and not $continue->success
builtin::blessed($continue) not $continue->success
Truth table66%2/3
A and not(B)
A= builtin::blessed($continue)B= not $continue->success
ABresult
0X0
100
111
233 2 my $error_response = error_response(
234 error_code => $continue->error_code,
235 error_message => $continue->error_message,
236 error_data => $method_args,
237 );
238 2 status $error_response->return_status('restish');
239 2 $response = $error_response->as_restish_error;
240 }
241 else {
242 21 my Dancer::RPCPlugin::DispatchItem $di = $dispatcher->{$found_match};
243 21 my $handler = $di->code;
244 21 my $package = $di->package;
245  
246 21 $response = eval {
247 21 $code_wrapper->($handler, $package, $method_name, $method_args);
248 };
249  
250 21 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 3 18 21
251 3 ▶ my $error_response = blessed($error) && $error->can('as_restish_error')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($error) && $error->can('as_restish_error') ? : 1 2 3
Condition66%2/3
builtin::blessed($error) and $error->can("as_restish_error")
!ll&&!rl&&r
201
MC/DC50%1/2
builtin::blessed($error) and $error->can("as_restish_error")
builtin::blessed($error) $error->can("as_restish_error")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->can("as_restish_error")
ABresult
0X0
100
111
252 ? $error
253 : error_response(
254 error_code => 500,
255 error_message => $error,
256 error_data => $method_args,
257 );
258 3 status($error_response->return_status('restish'));
259 3 $response = $error_response->as_restish_error;
260 }
261 21 ▶ if (blessed($response) && $response->can('as_restish_error')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->can('as_restish_error')) { } 1 20 21
elsif (builtin::blessed($response)) { } 1 19 20
Condition100%3/3
builtin::blessed($response) and $response->can("as_restish_error")
!ll&&!rl&&r
1911
MC/DC100%2/2
builtin::blessed($response) and $response->can("as_restish_error")
builtin::blessed($response) $response->can("as_restish_error")
Truth table100%3/3
A and B
A= builtin::blessed($response)B= $response->can("as_restish_error")
ABresult
0X0
100
111
262 1 status($response->return_status('restish'));
263 1 $response = $response->as_restish_error;
264 }
265 elsif (blessed($response)) {
266 1 $response = flatten_data($response);
267 }
268 21 debug("[handled_restish_response($request_path)] ", $response);
269 }
270 25 my $jsonise_options = {canonical => 1};
271 25 ▶ if (config->{encoding} && config->{encoding} =~ m{^utf-?8$}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (config()->{"encoding"} and config()->{"encoding"} =~ /^utf-?8$/i) 13 12 25
Condition66%2/3
config()->{"encoding"} and config()->{"encoding"} =~ /^utf-?8$/i
!ll&&!rl&&r
12013
MC/DC50%1/2
config()->{"encoding"} and config()->{"encoding"} =~ /^utf-?8$/i
config()->{"encoding"} config()->{"encoding"} =~ /^utf-?8$/i
Truth table66%2/3
A and B
A= config()->{"encoding"}B= config()->{"encoding"} =~ /^utf-?8$/i
ABresult
0X0
100
111
272 13 $jsonise_options->{utf8} = 1;
273 }
274  
275 25 info( sprintf(
276 "[RPC::RESTISH] request for %s took %.4fs",
277 $request_path, time() - $start_request
278 ));
279 # non-refs will be send as-is
280 25 ▶ return ref($response)
Branch: 1 branch
BranchTrueFalseTotal
ref $response ? : 24 1 25
281 ? to_json($response, $jsonise_options)
282 : $response;
283 16 };
284  
285 16 debug("setting routes (restish): $endpoint ", $lister);
286 # split the keys in $dispatcher so we can register 'any' methods for all
287 # the handler will know what to do...
288 16 for my $dispatch_route (keys %$dispatcher) {
289 25 my ($hm, $route) = split(/$_HM_POSTFIX/, $dispatch_route, 2);
290 25 my $dancer_route = "$endpoint/$route";
291 25 debug("[restish] registering `any $dancer_route` ($hm)");
292 25 any $dancer_route, $handle_call;
293 }
294  
295 3 };
296  
297 4 ▶sub build_dispatcher_from_pod {
Subroutine build_dispatcher_from_pod: called CC=1 SCAR=0.0
Pod documented
298 4 my ($pkgs, $endpoint) = @_;
299 4 debug("[build_dispatcher_from_pod]");
300 4 return dispatch_table_from_pod(
301 plugin => 'restish',
302 packages => $pkgs,
303 endpoint => $endpoint,
304 );
305 }
306  
307 1 ▶sub build_dispatcher_from_config {
Subroutine build_dispatcher_from_config: called CC=1 SCAR=0.0
Pod documented
308 1 my ($config, $endpoint) = @_;
309 1 debug("[build_dispatcher_from_config]");
310  
311 1 return dispatch_table_from_config(
312 plugin => 'restish',
313 config => $config,
314 endpoint => $endpoint,
315 );
316 }
317  
318 3 register_plugin();
319 3 true;
320  
321 =head1 NAME
322  
323 Dancer::Plugin::RPC::RESTISH - Simple plugin to implement a restish interface.
324  
325 =head1 SYNOPSIS
326  
327 In the Controler-bit:
328  
329 use Dancer::Plugin::RPC::RESTISH;
330 restish '/endpoint' => {
331 publish => 'pod',
332 arguments => ['MyProject::Admin'],
333 cors_allow_origin => '*',
334 };
335  
336 and in the Model-bit (B<MyProject::Admin>):
337  
338 package MyProject::Admin;
339
340 =for restish GET@ability/:id rpc_get_ability_details
341
342 =cut
343
344 sub rpc_get_ability_details {
345 my %args = @_; # contains: {"id": 42}
346 return {
347 # datastructure
348 };
349 }
350 1;
351  
352 =head1 DESCRIPTION
353  
354 RESTISH is an implementation of REST that lets you bind routes to code in the
355 style the rest of L<Dancer::Plugin::RPC> modules do. One must realise that this
356 basically binds REST-paths to RPC-methods (that's not ideal, but saves a lot of
357 code).
358  
359 B<This version only supports JSON as data serialisation>.
360  
361 =head2 restish '/base_path' => \%publisher_arguments
362  
363 See L<Dancer::Plugin::RPC>, L<Dancer::Plugin::RPC::JSONRPC>,
364 L<Dancer::Plugin::RPC::RESTRPC>, L<Dancer::Plugin::RPC::XMLRPC> for more
365 information about the C<%publisher_arguments>.
366  
367 =head2 Implement the routes for RESTISH
368  
369 The plugin registers Dancer-C<any> route-handlers for the C<base_path> +
370 C<method_path> and the route-handler looks for a data-handler that matches the path
371 and HTTP-method.
372  
373 Method-paths can contain colon-prefixed parameters native to Dancer. These
374 parameters will be merged with the content-parameters and the query-parameters
375 into a single hash which will be passed to the code as the parameters.
376  
377 Method-paths are prefixed by a HTTP-method followed by B<@>:
378  
379 =over
380  
381 =item publisher => 'config'
382  
383 plugins:
384 'RPC::RESTISH':
385 '/rest':
386 'MyProject::Admin':
387 'GET@resources': 'get_all_resourses'
388 'POST@resource': 'create_resource'
389 'GET@resource/:id': 'get_resource'
390 'PATCH@resource/:id': 'update_resource'
391 'DELETE@resource/:id': 'delete_resource'
392  
393 =item publisher => 'pod'
394  
395 =for restish GET@resources get_all_resources /rest
396 =for restish POST@resource create_resource /rest
397 =for restish GET@resource/:id get_resource /rest
398 =for restish PATCH@resource/:id update_resource /rest
399 =for restish DELETE@resource/:id delete_resource /rest
400  
401 The third argument (the base_path) is optional.
402  
403 =back
404  
405 The plugin for RESTISH also adds 2 fields to C<$Dancer::RPCPlugin::ROUTE_INFO>:
406  
407 local $Dancer::RPCPlugin::ROUTE_INFO = {
408 plugin => PLUGIN_NAME,
409 endpoint => $endpoint,
410 rpc_method => $method_name,
411 full_path => request->path,
412 http_method => $http_method,
413 # These two are added
414 route_matched => $found_match, # PATCH@resource/:id
415 matched_re => $match_re, # PATCH@resource/[^/]+
416 };
417  
418 =head2 CORS (Cross-Origin Resource Sharing)
419  
420 If one wants the service to be directly called from javascript in a browser, one
421 has to consider CORS as browsers enforce that. This means that the actual
422 request is preceded by what's called a I<preflight request> that uses the
423 HTTP-method B<OPTIONS> with a number of header-fields.
424  
425 =over
426  
427 =item Origin
428  
429 =item Access-Control-Request-Method
430  
431 =back
432  
433 The plugin supports considering these CORS requests, by special casing these
434 B<OPTIONS> requests and always sending the C<Access-Control-Allow-Origin> header
435 as set in the config options.
436  
437 =head3 cors_allow_origin => $list_of_urls | '*'
438  
439 If left out, no attempt to honour a CORS B<OPTIONS> request will be done and the
440 request will be passed.
441  
442 When set to a value, the B<OPTIONS> request will be executed, for any http-method in
443 the C<Access-Control-Request-Method> header. The response to the B<OPTIONS>
444 request will also contain every C<Access-Control-Allow-*> header that was
445 requested as C<Access-Control-Request-*> header.
446  
447 When set, all responses will contain the C<Access-Control-Allow-Origin>-header
448 with either C<*> if that was set, or the value of the actual C<Origin>-header
449 that was passed and equals one the preset values.
450  
451 =head1 INTERNAL
452  
453 =head2 build_dispatcher_from_config
454  
455 Creates a (partial) dispatch table from data passed from the (YAML)-config file.
456  
457 =head2 build_dispatcher_from_pod
458  
459 Creates a (partial) dispatch table from data provided in POD.
460  
461 =head1 LICENSE
462  
463 This library is free software; you can redistribute it and/or modify
464 it under the same terms as Perl itself.
465  
466 See:
467  
468 =over 4
469  
470 =item * L<http://www.perl.com/perl/misc/Artistic.html>
471  
472 =item * L<http://www.gnu.org/copyleft/gpl.html>
473  
474 =back
475  
476 This program is distributed in the hope that it will be useful,
477 but WITHOUT ANY WARRANTY; without even the implied warranty of
478 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
479  
480 =head1 COPYRIGHT
481  
482 (c) MMXX - Abe Timmerman <abeltje@cpan.org>
483  
484 =cut
↑ Summary