| 1 | package Dancer::Plugin::CORS; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Dancer::Plugin::CORS::Sharing; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Dancer::Plugin::CORS - A plugin for using cross origin resource sharing | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Version 0.10 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Cross origin resource sharing is a feature used by modern web browser to bypass cross site scripting restrictions. A webservice can provide those rules from which origin a client is allowed to make cross-site requests. This module helps you to setup such rules. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | use Dancer::Plugin::CORS; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | get '/foo' => sub { ... }; | ||||||||||||||||||||||||||||
| 27 | share '/foo' => | ||||||||||||||||||||||||||||
| 28 | origin => 'http://localhost/', | ||||||||||||||||||||||||||||
| 29 | credentials => 1, | ||||||||||||||||||||||||||||
| 30 | expose => [qw[ Content-Type ]], | ||||||||||||||||||||||||||||
| 31 | method => 'GET', | ||||||||||||||||||||||||||||
| 32 | headers => [qw[ X-Requested-With ]], | ||||||||||||||||||||||||||||
| 33 | maxage => 7200, | ||||||||||||||||||||||||||||
| 34 | ; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 8 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 8 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 8 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 8 | ▶ | use Sub::Name; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 8 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 8 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 8 | ▶ | use constant DEBUG => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 8 | my $routes = {}; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub _isin($@) { | ||||||||||||||||||||||||||||
| 50 | 6 | ▶ | shift ~~ \@_; | ||||||||||||||||||||||||||
|
Subroutine
_isin: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub _isuri(_) { | ||||||||||||||||||||||||||||
| 54 | 23 | ▶ | shift =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?| | ||||||||||||||||||||||||||
|
Subroutine
_isuri: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub _handle; | ||||||||||||||||||||||||||||
| 58 | 8 | my $current_route; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub _prefl_handle { | ||||||||||||||||||||||||||||
| 61 | 8 | ▶ | debug "[CORS] entered preflight request main subroutine" if DEBUG; | ||||||||||||||||||||||||||
|
Subroutine
_prefl_handle: called CC=5 SCAR=17.4
| |||||||||||||||||||||||||||||
| 62 | 8 | ▶ | unless (defined $current_route) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | warning "[CORS] current route not defined!"; | |||||||||||||||||||||||||||
| 64 | 0 | return; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 8 | ▶ | unless(_handle($current_route)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 5 | my $request = Dancer::SharedData->request; | |||||||||||||||||||||||||||
| 68 | 5 | ▶ | while ($current_route = $current_route->next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | if ($current_route->match($request)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | debug "[CORS] going to next handler" if DEBUG; | |||||||||||||||||||||||||||
| 71 | 0 | pass; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 5 | debug "[CORS] no more rules." if DEBUG; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 8 | $current_route = undef; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub _add_rule($%) { | ||||||||||||||||||||||||||||
| 80 | 18 | ▶ | my ($route, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_add_rule: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 18 | ▶ | if (blessed $route and $route->isa('Dancer::Route')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($route) and $route->isa("Dancer::Route")
MC/DC50%1/2
builtin::blessed($route) and $route->isa("Dancer::Route")
builtin::blessed($route)
$route->isa("Dancer::Route")
Truth table66%2/3
A and B
A= builtin::blessed($route)B= $route->isa("Dancer::Route")
| |||||||||||||||||||||||||||||
| 83 | 13 | my $prefl = Dancer::App->current->registry->add_route(Dancer::Route->new( | |||||||||||||||||||||||||||
| 84 | method => 'options', | ||||||||||||||||||||||||||||
| 85 | code => \&_prefl_handle, | ||||||||||||||||||||||||||||
| 86 | options => $route->options, | ||||||||||||||||||||||||||||
| 87 | pattern => $route->pattern | ||||||||||||||||||||||||||||
| 88 | )); | ||||||||||||||||||||||||||||
| 89 | 13 | $options{method} = uc($route->method); | |||||||||||||||||||||||||||
| 90 | 13 | $routes->{$prefl} = [{ %options }]; | |||||||||||||||||||||||||||
| 91 | 13 | debug "registered preflight route handler for ".$route->method." pattern: ".$route->pattern."\n" if DEBUG; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 18 | ▶ | unless (exists $routes->{$route}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 16 | $routes->{$route} = []; | |||||||||||||||||||||||||||
| 96 | 16 | ▶ | unless (ref $route) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 5 | debug "registered preflight route handler for any pattern: $route\n" if DEBUG; | |||||||||||||||||||||||||||
| 98 | 5 | options $route => \&_prefl_handle; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 18 | push @{ $routes->{$route} } => \%options; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub _handle { | ||||||||||||||||||||||||||||
| 105 | 27 | ▶ | my $route = shift; | ||||||||||||||||||||||||||
|
Subroutine
_handle: called CC=31 SCAR=35.2
| |||||||||||||||||||||||||||||
| 106 | 27 | my $request = Dancer::SharedData->request; | |||||||||||||||||||||||||||
| 107 | 27 | my $path = $request->path_info; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 27 | ▶ | unless (exists $routes->{$path} or exists $routes->{$route}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $routes->{$path} or exists $routes->{$route}
MC/DC0%0/2
exists $routes->{$path} or exists $routes->{$route}
exists $routes->{$path}
exists $routes->{$route}
Truth table66%2/3
A or B
A= exists $routes->{$path}B= exists $routes->{$route}
| |||||||||||||||||||||||||||||
| 110 | 0 | debug "[CORS] path $path or route $route did not no matched any rule" if DEBUG; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 27 | my $preflight = uc $request->method eq 'OPTIONS'; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 27 | debug "[CORS] preflight request" if DEBUG and $preflight; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 27 | my $origin = scalar($request->header('Origin')); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 27 | ▶ | unless (defined $origin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 4 | debug "[CORS] no origin header present in request" if DEBUG; | |||||||||||||||||||||||||||
| 121 | 4 | return; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 23 | ▶ | unless (_isuri($origin)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | debug "[CORS] origin '$origin' is not a URI" if DEBUG; | |||||||||||||||||||||||||||
| 126 | 0 | return; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 23 | ▶ | my $requested_method = $preflight | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | ? scalar($request->header('Access-Control-Request-Method')) | ||||||||||||||||||||||||||||
| 131 | : $request->method | ||||||||||||||||||||||||||||
| 132 | ; | ||||||||||||||||||||||||||||
| 133 | 23 | ▶ | unless (defined $requested_method) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | debug "[CORS] no request method defined" if DEBUG; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 23 | ▶ | my @requested_headers = map { s{\s+}{}g; $_ } split /,+/, (scalar($request->header('Access-Control-Request-Headers')) || ''); | ||||||||||||||||||||||||||
|
Condition100%2/2
scalar $request->header('Access-Control-Request-Headers') || ''
MC/DC100%1/1
scalar $request->header('Access-Control-Request-Headers') || ''
scalar $request->header('Access-Control-Request-Headers')
Truth table100%2/2
A
A= scalar $request->header('Access-Control-Request-Headers')
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 23 | my ($ok, $i) = (0, 0); | |||||||||||||||||||||||||||
| 140 | 23 | my ($headers, $xoptions); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 23 | ▶ | if (exists $routes->{$route}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 15 | $path = "$route"; | |||||||||||||||||||||||||||
| 144 | 15 | debug "[CORS] dynamic route" if DEBUG; | |||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||
| 146 | 8 | debug "[CORS] static route" if DEBUG; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 23 | my $n = scalar @{$routes->{$path}}; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 23 | RULE: foreach my $options (@{$routes->{$path}}) { | |||||||||||||||||||||||||||
| 152 | 24 | debug "[CORS] testing rule ".++$i." of $n" if DEBUG; | |||||||||||||||||||||||||||
| 153 | 24 | if (DEBUG) { | |||||||||||||||||||||||||||
| 154 | 8 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | debug Dumper($options); | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 24 | $headers = {}; | |||||||||||||||||||||||||||
| 158 | 24 | ▶ | if (exists $options->{origin}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 23 | given (ref $options->{origin}) { | |||||||||||||||||||||||||||
| 160 | 23 | when ('CODE') { | |||||||||||||||||||||||||||
| 161 | 5 | ▶ | if (!$options->{origin}->(URI->new($origin))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 3 | debug "[CORS] origin $origin did not matched against coderef" if DEBUG; | |||||||||||||||||||||||||||
| 163 | 3 | next RULE; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 18 | when ('ARRAY') { | |||||||||||||||||||||||||||
| 167 | 2 | ▶ | unless (_isin($origin => @{ $options->{origin} })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 1 | debug "[CORS] origin $origin is not in array" if DEBUG; | |||||||||||||||||||||||||||
| 169 | 1 | next RULE; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 16 | when ('Regexp') { | |||||||||||||||||||||||||||
| 173 | 2 | ▶ | unless ($origin =~ $options->{origin}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 1 | debug "[CORS] origin $origin did not matched against regexp" if DEBUG; | |||||||||||||||||||||||||||
| 175 | 1 | next RULE; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 14 | when ('') { | |||||||||||||||||||||||||||
| 179 | 14 | ▶ | unless ($options->{origin} eq $origin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 5 | debug "[CORS] origin $origin did not matched against static string" if DEBUG; | |||||||||||||||||||||||||||
| 181 | 5 | next RULE; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 0 | default { | |||||||||||||||||||||||||||
| 185 | 0 | confess("unknown origin type: $_"); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } else { | ||||||||||||||||||||||||||||
| 189 | 1 | $origin = '*'; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 14 | $headers->{'Access-Control-Allow-Origin'} = $origin; | |||||||||||||||||||||||||||
| 192 | 14 | ▶ | $headers->{'Vary'} = 'Origin' if $origin ne '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 14 | ▶ | if (exists $options->{credentials}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 2 | ▶ | if (!!$options->{credentials}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 2 | ▶ | if ($origin eq '*') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 1 | warning('For a resource that supports credentials a origin matcher must be specified.'); | |||||||||||||||||||||||||||
| 198 | 1 | next RULE; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 1 | $headers->{'Access-Control-Allow-Credentials'} = 'true' ; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 13 | ▶ | if (exists $options->{expose}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 2 | $headers->{'Access-Control-Expose-Headers'} = $options->{expose}; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 13 | ▶ | if (exists $options->{methods}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 209 | 2 | ▶ | unless (_isin(lc $requested_method => map lc, @{ $options->{methods} })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | debug "[CORS] request method not allowed" if DEBUG; | |||||||||||||||||||||||||||
| 211 | 0 | next RULE; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 2 | $headers->{'Access-Control-Allow-Methods'} = join ', ' => map uc, @{ $options->{methods} }; | |||||||||||||||||||||||||||
| 214 | } elsif (exists $options->{method}) { | ||||||||||||||||||||||||||||
| 215 | 11 | ▶ | unless ($options->{method} eq $requested_method) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | debug "[CORS] request method '$requested_method' not allowed: ".$options->{method} if DEBUG; | |||||||||||||||||||||||||||
| 217 | 0 | next RULE; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 11 | $headers->{'Access-Control-Allow-Methods'} = $options->{method}; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 13 | ▶ | if (exists $options->{headers}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 223 | 2 | foreach my $requested_header (@requested_headers) { | |||||||||||||||||||||||||||
| 224 | 2 | ▶ | unless (_isin(lc $requested_header => map lc, @{ $options->{headers} })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 0 | debug "[CORS] requested headers did not match allowed in rule" if DEBUG; | |||||||||||||||||||||||||||
| 226 | 0 | next RULE; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | 2 | $headers->{'Access-Control-Allow-Headers'} = join ', ' => @{ $options->{headers} }; | |||||||||||||||||||||||||||
| 230 | } elsif (@requested_headers) { | ||||||||||||||||||||||||||||
| 231 | 0 | $headers->{'Access-Control-Allow-Headers'} = join ', ' => @requested_headers; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 13 | ▶ | if ($preflight and exists $options->{maxage}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$preflight and exists $options->{"maxage"}
MC/DC100%2/2
$preflight and exists $options->{"maxage"}
$preflight
exists $options->{"maxage"}
Truth table100%3/3
A and B
A= $preflightB= exists $options->{"maxage"}
| |||||||||||||||||||||||||||||
| 235 | 2 | $headers->{'Access-Control-Max-Age'} = $options->{maxage}; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 13 | $ok = 1; | |||||||||||||||||||||||||||
| 239 | 13 | var CORS => {%$options}; | |||||||||||||||||||||||||||
| 240 | 13 | Dancer::SharedData->response->headers(%$headers); | |||||||||||||||||||||||||||
| 241 | 13 | if (DEBUG) { | |||||||||||||||||||||||||||
| 242 | 8 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 243 | debug Dumper({headers => $headers}); | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | 13 | last RULE; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 23 | ▶ | if ($ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 13 | debug "[CORS] matched!" if DEBUG; | |||||||||||||||||||||||||||
| 250 | } else { | ||||||||||||||||||||||||||||
| 251 | 10 | debug "[CORS] no rule matched" if DEBUG; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 23 | return $ok; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head1 KEYWORDS | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head2 share(C<$route>, C<%options>) | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | The parameter C<$route> may be any valid path like used I<get>, I<post>, I<put>, I<delete> or I<patch> but not I<option>. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Alternatively a L<Dancer::Route> object may be used instead: | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | $route = get '/' => sub { ... }; | ||||||||||||||||||||||||||||
| 266 | share $route => ... ; | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | For any route more than one rule may be defined. The order is relevant: the first matching rule wins. | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Following keywords recognized by C<%options>: | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =over 4 | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =item I<origin> | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | This key defines a static origin (scalar), a list (arrayref), a regex or a subroutine. | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | If not specified, any origin is allowed. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | If a subroutine is used, the first passed parameter is a L<URI> object. It should return a true value if this origin is allowed to access the route in question; otherwise false. | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | origin => sub { shift->host ~~ [ 'localhost', '127.0.0.1', '::1' ] } # allow only from localhost | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | Hint: a origin consists of protocol, hostname and maybe a port. Examples: C<http://www.example.com>, C<https://securesite.com>, C<http://localhost:3000>, C<http://127.0.0.1>, C<http://[::1]> | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =item I<credentials> | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | This indicates whether cookies, HTTP authentication and/or client-side SSL certificates may sent by a client. Allowed values are C<0> or C<1>. | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | This option must be used together with I<origin>. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =item I<expose> | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | A comma-seperated list of headers, that a client may extract from response for use in a client application. | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =item I<methods> | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | A arrayref of allowed methods. If no methods are specified, any methods are allowed. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =item I<method> | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | A string containing a single supported method. This parameter is autofilled when I<share()> is used together with a L<Dancer::Route> object. If no method is specified, any method is allowed. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =item I<headers> | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | A arrayref of allowed request headers. In most cases that should be C<[ 'X-Requested-With' ]> when ajax requests are made. If not headers are specified, all requested headers are allowed. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =item I<maxage> | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | A maximum time (in seconds) a client may cache a preflight request. This can decrease the amount of requests made to the webservice. | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =back | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =cut | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 8 | register(share => \&_add_rule); | |||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | hook(before => sub { | ||||||||||||||||||||||||||||
| 319 | 27 | ▶ | $current_route = shift || return; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 320 | 27 | my $preflight = uc Dancer::SharedData->request->method eq 'OPTIONS'; | |||||||||||||||||||||||||||
| 321 | 27 | ▶ | if ($preflight) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 8 | debug "[CORS] pre-check: preflight request, handle within main subroutine" if DEBUG; | |||||||||||||||||||||||||||
| 323 | } else { | ||||||||||||||||||||||||||||
| 324 | 19 | debug "[CORS] pre-check: no preflight, handle actual request now" if DEBUG; | |||||||||||||||||||||||||||
| 325 | 19 | _handle($current_route); | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | 8 | }); | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 8 | my $current_sharing; | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =head2 sharing | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | This keyword is a helper for re-using rules for many routes. | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | See L<Dancer::Plugin::CORS::Sharing> for more information about this feature. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | register sharing => sub { | ||||||||||||||||||||||||||||
| 340 | 0 | ▶ | my $class = __PACKAGE__.'::Sharing'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | $current_sharing ||= $class->new(@_,_add_rule=>\&_add_rule); | ||||||||||||||||||||||||||
|
Condition0%0/3
$current_sharing ||= $class->new(@_, "_add_rule", \&Dancer::Plugin::CORS::_add_rule)
MC/DC0%0/2
$current_sharing ||= $class->new(@_, "_add_rule", \&Dancer::Plugin::CORS::_add_rule)
$current_sharing
$class->new(@_, "_add_rule", \&Dancer::Plugin::CORS::_add_rule)
Truth table0%0/3
A ||= B
A= $current_sharingB= $class->new(@_, "_add_rule", \&Dancer::Plugin::CORS::_add_rule)
| |||||||||||||||||||||||||||||
| 342 | 0 | return $current_sharing; | |||||||||||||||||||||||||||
| 343 | 8 | }; | |||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | David Zurborg, C<< <zurborg@cpan.org> >> | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head1 BUGS | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Please report any bugs or feature requests trough my project management tool | ||||||||||||||||||||||||||||
| 352 | at L<http://development.david-zurb.org/projects/libdancer-plugin-cors-perl/issues/new>. I | ||||||||||||||||||||||||||||
| 353 | will be notified, and then you'll automatically be notified of progress on | ||||||||||||||||||||||||||||
| 354 | your bug as I make changes. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | perldoc Dancer::Plugin::CORS | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | You can also look for information at: | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =over 4 | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =item * Redmine: Homepage of this module | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | L<http://development.david-zurb.org/projects/libdancer-plugin-cors-perl> | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-CORS> | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | L<http://annocpan.org/dist/Dancer-Plugin-CORS> | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | L<http://cpanratings.perl.org/d/Dancer-Plugin-CORS> | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | L<http://search.cpan.org/dist/Dancer-Plugin-CORS/> | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =back | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | Copyright 2014 David Zurborg, all rights reserved. | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | This program is released under the following license: open-source | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =cut | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 8 | register_plugin; | |||||||||||||||||||||||||||
| 397 | 8 | 1; | |||||||||||||||||||||||||||