File Coverage

blib/lib/Plack/Middleware/Antibot/TooSlow.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::TooSlow;
2              
3 1     1   24730 use strict;
  1         2  
  1         49  
4 1     1   6 use warnings;
  1         3  
  1         38  
5              
6 1     1   620 use parent 'Plack::Middleware::Antibot::FilterBase';
  1         375  
  1         7  
7              
8 1     1   679 use Plack::Session;
  1         1658  
  1         292  
9              
10             sub new {
11 5     5 1 7120 my $self = shift->SUPER::new(@_);
12 5         11 my (%params) = @_;
13              
14 5   50     41 $self->{session_name} = $params{session_name} || 'antibot_tooslow';
15 5   50     24 $self->{timeout} = $params{timeout} || 60 * 60;
16 5   50     26 $self->{score} = $params{score} || 0.8;
17              
18 5         17 return $self;
19             }
20              
21             sub execute {
22 5     5 1 51 my $self = shift;
23 5         6 my ($env) = @_;
24              
25 5 100       26 if ($env->{REQUEST_METHOD} eq 'GET') {
    50          
26 2         12 my $session = Plack::Session->new($env);
27              
28 2         38 $session->set($self->{session_name}, time);
29             }
30             elsif ($env->{REQUEST_METHOD} eq 'POST') {
31 3         11 my $session = Plack::Session->new($env);
32              
33 3         30 my $too_slow = $session->get($self->{session_name});
34 3 100 100     47 unless ($too_slow && time - $too_slow < $self->{timeout}) {
35 2         8 $env->{'plack.antibot.tooslow.detected'}++;
36             }
37             }
38              
39 5         60 return;
40             }
41              
42             1;
43             __END__