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

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