lib/Plack/Middleware/TrailingSlash.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 100% 12 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 94.1% 64 / 68 CC 9 SCAR 17.1
CC
9
Cov
96%
call
9.0
__ANON__
1.1
BEGIN
1.0
CRAP 5.5
↑ Summary
1 package Plack::Middleware::TrailingSlash;
2  
3 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 $Plack::Middleware::TrailingSlash::AUTHORITY = 'cpan:okko';
6 }
7 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 $Plack::Middleware::TrailingSlash::VERSION = '0.001';
9 }
10 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 extends 'Plack::Middleware';
15  
16 # use Plack::Util::Accessor qw( ignore );
17 0 ▶has 'ignore' => (is => 'rw', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
18  
19 48 ▶sub call {
Subroutine call: called CC=9 SCAR=22.0
Pod documented
20 48 my ($self, $env) = @_;
21 48 my $req = Plack::Request->new($env);
22 48 my $p = $req->path_info();
23  
24 # Ignore if not GET
25 48 ▶ if ($req->method() ne 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->method ne "GET") 36 12 48
26 36 return $self->app->($env);
27 }
28  
29 # Ignore if we are happy with the URL
30 12 ▶ if ($p =~ /^.*\/$/ # Slash at the end OR
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ m[^.*/$] or $p =~ m[^.*/[^/]+\.[^/]+$]) 3 9 12
Condition66%2/3
$p =~ m[^.*/$] or $p =~ m[^.*/[^/]+\.[^/]+$]
l!l&&r!l&&!r
309
MC/DC50%1/2
$p =~ m[^.*/$] or $p =~ m[^.*/[^/]+\.[^/]+$]
$p =~ m[^.*/$] $p =~ m[^.*/[^/]+\.[^/]+$]
Truth table66%2/3
A or B
A= $p =~ m[^.*/$]B= $p =~ m[^.*/[^/]+\.[^/]+$]
ABresult
1X1
011
000
31 or $p =~ /^.*\/[^\/]+\.[^\/]+$/ # dot in the filename after the last /
32 ) {
33 3 return $self->app->($env);
34 }
35  
36 # Ignore if path is in the ignores list
37 9 ▶ if ( defined $self->ignore ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->ignore) 7 2 9
38 7 ▶ unless ( ref($self->ignore) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->ignore eq "ARRAY") 2 5 7
39 2 $self->ignore( [ $self->ignore ] );
40 }
41  
42 7 foreach my $ign ( @{$self->ignore} ) {
43 10 ▶ if ($p =~ $ign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /$ign/) 5 5 10
44 5 return $self->app->($env);
45 }
46 }
47 }
48  
49 # If we're here the pattern indicates it is a GET request to a directory path and should have a trailing slash.
50 4 my $uri = $req->uri();
51 4 ▶ if ($uri =~ /\?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /\?/) { } 3 1 4
52 # with a query string
53 3 $uri =~ s/\?/\/?/;
54 } else {
55 # without a query string
56 1 $uri .= '/';
57 }
58  
59 4 my $res = $req->new_response(301); # new Plack::Response
60 4 $res->headers([
61 'Location' => $uri,
62 'Content-Type' => 'text/html; charset=UTF-8',
63 'Cache-Control' => 'must-revalidate, max-age=3600'
64 ]);
65  
66 4 my $uhe = encode_entities($uri);
67 4 $res->body(
68 '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>301 Moved Permanently</title></head>'
69 .'<body><h1>Moved Permanently</h1><p>The document has moved <a href="'.$uhe.'">here</a>.</p></body></html>'
70 );
71  
72 4 return $res->finalize;
73 };
74  
75 4 __PACKAGE__->meta->make_immutable( inline_constructor => 0 );
76 4 1;
77 __END__
↑ Summary