| 1 | package Mojolicious::Plugin::HeaderCondition; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 18 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use re qw(is_regexp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 83 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 83 | my ($self, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 83 | $app->routes->add_condition(headers => \&_headers); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 0 | ▶ | $app->routes->add_condition(agent => sub { _headers(@_[0 .. 2], {'User-Agent' => $_[3]}) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | $app->routes->add_condition(host => sub { _check($_[1]->req->url->to_abs->host, $_[3]) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub _check { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | my ($value, $pattern) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | return 1 if $value && $pattern && is_regexp($pattern) && $value =~ $pattern; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$value and $pattern and &is_regexp($pattern) and $value =~ /$pattern/u
$value and $pattern and &is_regexp($pattern)
$value and $pattern
MC/DC0%0/4
$value and $pattern and &is_regexp($pattern) and $value =~ /$pattern/u
$value
$pattern
&is_regexp($pattern)
$value =~ /$pattern/u
Truth table0%0/5
A and B and C and D
A= $valueB= $patternC= &is_regexp($pattern)D= $value =~ /$pattern/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | return $value && defined $pattern && $pattern eq $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$value && defined $pattern && $pattern eq $value
$value and defined $pattern
MC/DC0%0/3
$value && defined $pattern && $pattern eq $value
$value
defined $pattern
$pattern eq $value
Truth table0%0/4
A and B && C
A= $valueB= defined $patternC= $pattern eq $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub _headers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_headers: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my ($route, $c, $captures, $patterns) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | return undef unless $patterns && ref $patterns eq 'HASH' && keys %$patterns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$patterns and ref $patterns eq "HASH" and keys %$patterns
$patterns and ref $patterns eq "HASH"
MC/DC0%0/3
$patterns and ref $patterns eq "HASH" and keys %$patterns
$patterns
ref $patterns eq "HASH"
keys %$patterns
Truth table0%0/4
A and B and C
A= $patternsB= ref $patterns eq "HASH"C= keys %$patterns
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # All headers need to match | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $headers = $c->req->headers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | _check($headers->header($_), $patterns->{$_}) || return undef for keys %$patterns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | Mojolicious::Plugin::HeaderCondition - Header condition plugin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Mojolicious | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | $app->plugin('HeaderCondition'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | $app->get('/foo')->requires(headers => {Referer => qr/example\.com/}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # Mojolicious::Lite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | plugin 'HeaderCondition'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | get '/' => (headers => {Referer => qr/example\.com/}) => sub {...}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # All headers need to match | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | $app->get('/foo')->requires(headers => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 'X-Secret-Header' => 'Foo', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | Referer => qr/example\.com/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # The "agent" condition is a shortcut for the "User-Agent" header | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | get '/' => (agent => qr/Firefox/) => sub {...}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # The "host" condition is a shortcut for the detected host | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | get '/' => (host => qr/mojolicious\.org/) => sub {...}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | L<Mojolicious::Plugin::HeaderCondition> is a route condition for header-based routes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | This is a core plugin, that means it is always enabled and its code a good example for learning to build new plugins, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | you're welcome to fork it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available by default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | L<Mojolicious::Plugin::HeaderCondition> inherits all methods from L<Mojolicious::Plugin> and implements the following | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | new ones. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | =head2 register | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | $plugin->register(Mojolicious->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | Register conditions in L<Mojolicious> application. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||