File Coverage

blib/lib/Plack/Middleware/Antibot/TooFast.pm
Criterion Covered Total %
statement 28 28 100.0
branch 5 6 83.3
condition 6 9 66.6
subroutine 6 6 100.0
pod 2 2 100.0
total 47 51 92.1


line stmt bran cond sub pod time code
1             package Plack::Middleware::Antibot::TooFast;
2              
3 1     1   14992 use strict;
  1         2  
  1         33  
4 1     1   4 use warnings;
  1         1  
  1         25  
5              
6 1     1   404 use parent 'Plack::Middleware::Antibot::FilterBase';
  1         274  
  1         5  
7              
8 1     1   442 use Plack::Session;
  1         955  
  1         173  
9              
10             sub new {
11 5     5 1 4584 my $self = shift->SUPER::new(@_);
12 5         8 my (%params) = @_;
13              
14 5   50     35 $self->{session_name} = $params{session_name} || 'antibot_toofast';
15 5   50     23 $self->{timeout} = $params{timeout} || 1;
16 5   50     23 $self->{score} = $params{score} || 0.9;
17              
18 5         12 return $self;
19             }
20              
21             sub execute {
22 5     5 1 40 my $self = shift;
23 5         8 my ($env) = @_;
24              
25 5 100       19 if ($env->{REQUEST_METHOD} eq 'GET') {
    50          
26 2         8 my $session = Plack::Session->new($env);
27              
28 2         25 $session->set($self->{session_name}, time);
29             }
30             elsif ($env->{REQUEST_METHOD} eq 'POST') {
31 3         8 my $session = Plack::Session->new($env);
32              
33 3         20 my $too_fast = $session->get($self->{session_name});
34 3 100 100     33 unless ($too_fast && time - $too_fast > $self->{timeout}) {
35 2         5 $env->{'plack.antibot.toofast.detected'}++;
36             }
37             }
38              
39 5         41 return;
40             }
41              
42             1;
43             __END__