File Coverage

blib/lib/Amon2/Plugin/Web/Maintenance.pm
Criterion Covered Total %
statement 12 46 26.0
branch 0 28 0.0
condition 0 6 0.0
subroutine 4 9 44.4
pod 0 1 0.0
total 16 90 17.7


line stmt bran cond sub pod time code
1             package Amon2::Plugin::Web::Maintenance;
2 1     1   1000 use 5.008001;
  1         5  
  1         52  
3 1     1   6 use strict;
  1         10  
  1         35  
4 1     1   5 use warnings;
  1         11  
  1         34  
5 1     1   14071 use Net::CIDR::Lite;
  1         5531  
  1         555  
6              
7             our $VERSION = "0.01";
8              
9             sub init {
10 0     0 0   my ( $class, $c, $conf ) = @_;
11              
12 0 0         return unless $c->config->{MAINTENANCE}->{enable};
13              
14             $c->add_trigger(
15             BEFORE_DISPATCH => sub {
16 0     0     my ($self) = @_;
17              
18 0 0         return if &_is_exception($self);
19              
20 0 0         return $self->res_maintenance
21             if $self->can('res_maintenance');
22              
23 0           return $self->create_response(
24             503,
25             [ 'Content-Type' => 'text/plain',
26             'Content-Length' => 36,
27             ],
28             ['Service unavailable for maintenance.']
29             );
30             }
31 0           );
32             }
33              
34             sub _is_exception {
35 0     0     my ($self) = @_;
36              
37 0           my $config = $self->config->{MAINTENANCE};
38              
39 0 0         if ( exists $config->{except} ) {
40 0 0         if ( exists $config->{except}->{addr} ) {
41 0           my $remote_addr = $self->request->env->{REMOTE_ADDR};
42 0 0         return 1
43             if &_match_addr( $remote_addr,
44             $config->{except}->{addr} );
45             }
46 0 0         if ( exists $config->{except}->{path} ) {
47 0           my $path = $self->request->env->{PATH_INFO};
48 0 0         return 1
49             if &_match_path( $path, $config->{except}->{path} );
50             }
51 0           return 0;
52             }
53             }
54              
55             sub _match_addr {
56 0     0     my ( $addr, $conditions ) = @_;
57              
58 0           my $cidr4 = Net::CIDR::Lite->new();
59 0           my $cidr6 = Net::CIDR::Lite->new();
60 0           for my $condition (@$conditions) {
61 0 0 0       if ( $condition && $condition =~ m!:! ) {
62 0           $cidr6->add_any($condition);
63             }
64             else {
65 0           $cidr4->add_any($condition);
66             }
67             }
68              
69 0 0         if ( $addr =~ m!:! ) {
70 0           return $cidr6->find($addr);
71             }
72             else {
73 0           return $cidr4->find($addr);
74             }
75 0           return 0;
76             }
77              
78             sub _match_path {
79 0     0     my ( $path, $conditions ) = @_;
80              
81 0           for my $condition (@$conditions) {
82 0 0 0       if ( ref $condition && ref $condition eq 'Regexp' ) {
    0          
83 0 0         return 1 if $path =~ m!$condition!;
84             }
85             elsif ( defined $condition ) {
86 0 0         return 1 if $path eq $condition;
87             }
88             }
89 0           return 0;
90             }
91              
92             1;
93             __END__