File Coverage

blib/lib/Plack/Middleware/TrailingSlashKiller.pm
Criterion Covered Total %
statement 20 20 100.0
branch 7 8 87.5
condition 3 3 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 36 37 97.3


line stmt bran cond sub pod time code
1             package Plack::Middleware::TrailingSlashKiller;
2              
3 1     1   661 use strict;
  1         2  
  1         36  
4 1     1   5 use warnings;
  1         1  
  1         30  
5 1     1   13 use parent qw(Plack::Middleware);
  1         1  
  1         5  
6              
7 1     1   59 use Plack::Util::Accessor qw( redirect );
  1         1  
  1         7  
8              
9             our $VERSION = '0.01';
10             our %STATUSES = (
11             301 => 'Moved Permanently',
12             307 => 'Temporary Redirect',
13             );
14              
15             sub call {
16 17     17 1 37300 my( $self, $env ) = @_;
17              
18             # check for/replace trailing slash
19 17 100       80 if( $env->{PATH_INFO} =~ s{^(.+)/$}{$1} ) {
20             # redirect if waned
21 10 100       29 if( $self->redirect ) {
22             my $code = ( $env->{REQUEST_METHOD } eq 'HEAD' ||
23 4 100 100     41 $env->{REQUEST_METHOD } eq 'GET' )
24             ? 301
25             : 307;
26 4         6 my $location = $env->{PATH_INFO};
27 4 50       12 $location .= "?$env->{QUERY_STRING}" if $env->{QUERY_STRING};
28              
29 4         34 return [ $code, [ 'Location' => $location ], [ $STATUSES{$code} ] ];
30             }
31             }
32              
33             # return the app's content (with possibly updated PATH_INFO)
34 13         68 return $self->app->($env);
35             }
36              
37             1;
38             __END__