File Coverage

blib/lib/Plack/Middleware/RefererCheck.pm
Criterion Covered Total %
statement 20 20 100.0
branch 9 10 90.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 40 42 95.2


line stmt bran cond sub pod time code
1             package Plack::Middleware::RefererCheck;
2              
3 2     2   1353 use strict;
  2         4  
  2         74  
4 2     2   50 use 5.008_001;
  2         7  
  2         74  
5 2     2   1136 use parent qw(Plack::Middleware);
  2         331  
  2         230  
6            
7             __PACKAGE__->mk_accessors(qw(host same_scheme error_app no_warn));
8            
9             our $VERSION = '0.03';
10              
11             sub prepare_app {
12 4     4 1 927 my $self = shift;
13              
14 4 50       11 warn('Plack::Middleware::RefererCheck WAS DEPRECATED!') unless $self->no_warn;
15             }
16              
17             sub call {
18 18     18 1 63589 my($self, $env) = @_;
19              
20 18 100       62 $self->_check($env) ? $self->app->($env) : $self->error_app ? $self->error_app->($env) : _default_error_app();
    100          
21             }
22              
23             sub _check {
24 18     18   24 my ( $self, $env ) = @_;
25              
26 18 100       95 return 1 if $env->{REQUEST_METHOD} ne 'POST';
27              
28 14 100       61 my $scheme = $self->same_scheme ? qr{\Q$env->{'psgi.url_scheme'}\E} : qr{https?};
29 14   66     230 my $host = $self->host || $env->{HTTP_HOST};
30 14         166 $host = qr{\Q$host\E};
31              
32 14         276 return $env->{HTTP_REFERER} =~ m{\A$scheme://$host(?:/|\Z)};
33             }
34              
35             sub _default_error_app {
36 5     5   85 return ['403', ['Content-Type' => 'text/plain', 'Content-Length' => 9], ['Forbidden']];
37             }
38            
39             1;
40            
41             __END__