Mojolicious/Plugin/HeaderCondition.pm

filter: Statementstmt 52.3% 11 / 21 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 42.8% 3 / 7 Podpod 100% 1 / 1 totaltotal 22.7% 15 / 66 CC 12 SCAR 35.9
CC
12
Cov
23%
_check
56.0
_headers
42.0
__ANON__
1.1
CRAP 36.1
« Mojolicious/Sessions.pm ↑ Summary Mojo/DOM/HTML.pm »
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
BranchTrueFalseTotal
if $value and $pattern and &is_regexp($pattern) and $value =~ /$pattern/u 0 0 0
Condition0%0/9
$value and $pattern and &is_regexp($pattern) and $value =~ /$pattern/u
!ll&&!rl&&r
000
$value and $pattern and &is_regexp($pattern)
!ll&&!rl&&r
000
$value and $pattern
!ll&&!rl&&r
000
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
ABCDresult
0XXX0
10XX0
110X0
11100
11111
17 0 ▶ return $value && defined $pattern && $pattern eq $value;
Condition0%0/6
$value && defined $pattern && $pattern eq $value
!ll&&!rl&&r
000
$value and defined $pattern
!ll&&!rl&&r
000
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
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
unless $patterns and ref $patterns eq "HASH" and keys %$patterns 0 0 0
Condition0%0/6
$patterns and ref $patterns eq "HASH" and keys %$patterns
!ll&&!rl&&r
000
$patterns and ref $patterns eq "HASH"
!ll&&!rl&&r
000
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
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
_check($headers->header($_), $patterns->{$_}) or return undef 0 0 0
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
« Mojolicious/Sessions.pm ↑ Summary Mojo/DOM/HTML.pm »