File Coverage

blib/lib/Plack/Middleware/Antibot/TooSlow.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Plack::Middleware::Antibot::TooSlow;
2              
3 1     1   16585 use strict;
  1         3  
  1         53  
4 1     1   7 use warnings;
  1         2  
  1         41  
5              
6 1     1   490 use parent 'Plack::Middleware::Antibot::FilterBase';
  1         258  
  1         5  
7              
8 1     1   966 use Plack::Session;
  0            
  0            
9              
10             sub new {
11             my $self = shift->SUPER::new(@_);
12             my (%params) = @_;
13              
14             $self->{session_name} = $params{session_name} || 'antibot_tooslow';
15             $self->{timeout} = $params{timeout} || 60 * 60;
16             $self->{score} = $params{score} || 0.8;
17              
18             return $self;
19             }
20              
21             sub execute {
22             my $self = shift;
23             my ($env) = @_;
24              
25             if ($env->{REQUEST_METHOD} eq 'GET') {
26             my $session = Plack::Session->new($env);
27              
28             $session->set($self->{session_name}, time);
29             }
30             elsif ($env->{REQUEST_METHOD} eq 'POST') {
31             my $session = Plack::Session->new($env);
32              
33             my $too_slow = $session->get($self->{session_name});
34             unless ($too_slow && time - $too_slow < $self->{timeout}) {
35             $env->{'plack.antibot.tooslow.detected'}++;
36             }
37             }
38              
39             return;
40             }
41              
42             1;
43             __END__