summaryrefslogtreecommitdiffstats
path: root/lib/Jaos/WebApp/Route/HeaderConstraint.pm
blob: b14d8c32d8f52049357d3e85434c331ad0fb4863 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package Jaos::WebApp::Route::HeaderConstraint;
use strict;
use warnings;

sub register
{
  my ($class, $app) = @_;

  $app->add_route_hook(
    header => sub {
      my ($route, $ctx, $arguments) = @_;

      return unless ref($arguments) eq 'HASH';

      for my $key ( keys %$arguments ) {
        my $pattern = $arguments->{$key};
        if (my $value = $ctx->req->headers->header($key)) {
          $app->log->debug("Checking $key($value) against $pattern");
          if (ref $pattern eq 'Regexp') {
            return $value =~ $pattern;
          } else {
            return $value eq $pattern;
          }
        }
      }
      return;
    }
  );
}

=head1 NAME

Jaos::WebApp::Route::HeaderConstraint - Adds a header route constraint

=head1 SYNOPSIS

 package MyApp;
 use base qw/Jaos::WebApp/;
 sub startup
 {
   my $app = shift;
   $app->load_plugin('Jaos::WebApp::Route::HeaderConstraint');
   $app->connect('/user/:id', header => { host => 'subdomain.foo.org' });
 }
 1;

=head1 DESCRIPTION

This adds a route option called "header" that allows matching headers against specific values or regular expressions.

=head1 METHODS

=head2 register

Called by the L<Jaos::WebApp> plugin loader.

=head1 AUTHOR

Jason Woodward <woodwardj@jaos.org>

=head1 LICENSE

Copyright (C) 2010 Jason Woodward
All rights reserved

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library.  If not, see <http://www.gnu.org/licenses/>.

=cut

1;