File Coverage

blib/lib/Mojolicious/Plugin/HeaderCondition.pm
Criterion Covered Total %
statement 20 20 100.0
branch 3 4 75.0
condition 15 20 75.0
subroutine 7 7 100.0
pod 1 1 100.0
total 46 52 88.4


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::HeaderCondition;
2 48     48   383 use Mojo::Base 'Mojolicious::Plugin';
  48         138  
  48         390  
3              
4 48     48   407 use re qw(is_regexp);
  48         131  
  48         26982  
5              
6             sub register {
7 104     104 1 360 my ($self, $app) = @_;
8              
9 104         394 $app->routes->add_condition(headers => \&_headers);
10 104     2   403 $app->routes->add_condition(agent => sub { _headers(@_[0 .. 2], {'User-Agent' => $_[3]}) });
  2         17  
11 104     121   446 $app->routes->add_condition(host => sub { _check($_[1]->req->url->to_abs->host, $_[3]) });
  121         430  
12             }
13              
14             sub _check {
15 130     130   315 my ($value, $pattern) = @_;
16 130 100 66     1614 return 1 if $value && $pattern && is_regexp($pattern) && $value =~ $pattern;
      100        
      100        
17 108   100     1422 return $value && defined $pattern && $pattern eq $value;
18             }
19              
20             sub _headers {
21 9     9   21 my ($route, $c, $captures, $patterns) = @_;
22 9 50 33     67 return undef unless $patterns && ref $patterns eq 'HASH' && keys %$patterns;
      33        
23              
24             # All headers need to match
25 9         36 my $headers = $c->req->headers;
26 9   100     56 _check($headers->header($_), $patterns->{$_}) || return undef for keys %$patterns;
27 5         31 return 1;
28             }
29              
30             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->routes->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->routes->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 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 for a list of plugins that are available by default.
68              
69             =head1 METHODS
70              
71             L inherits all methods from L and implements the following
72             new ones.
73              
74             =head2 register
75              
76             $plugin->register(Mojolicious->new);
77              
78             Register conditions in L application.
79              
80             =head1 SEE ALSO
81              
82             L, L, L.
83              
84             =cut