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