blib/lib/Plack/App/Hostname.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 78.5% 11 / 14 Conditioncond 71.4% 10 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 87.0% 94 / 108 CC 17 SCAR 17.1
CC
17
Cov
90%
matching
9.5
unmap_app
5.2
call
5.0
CRAP 5.5
↑ Summary
1 1 ▶use 5.006; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Plack::App::Hostname;
4  
5 1 our $VERSION = '1.002';
6  
7 1 ▶BEGIN { require Plack::Component; our @ISA = 'Plack::Component' }
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Plack::Util::Accessor qw( custom_matcher missing_header_app default_app );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶sub map_hosts_to {
Subroutine map_hosts_to: called CC=1 SCAR=0.0
Pod documented
11 5 my $self = shift;
12 5 my $app = shift;
13 5 @{ $self->{'_app_for_host'} }{ map { lc } @_ } = ( $app ) x @_;
14 5 $self->{'_num_wildcards'} += grep { /\A\*\*\./ } @_;
15 5 return $self;
16 }
17  
18 2 ▶sub unmap_host {
Subroutine unmap_host: called CC=1 SCAR=0.0
Pod documented
19 2 my $self = shift;
20 2 delete @{ $self->{'_app_for_host'} }{ map { lc } @_ };
21 2 $self->{'_num_wildcards'} -= grep { /\A\*\*\./ } @_;
22 2 return $self;
23 }
24  
25 1 ▶sub unmap_app {
Subroutine unmap_app: called CC=5 SCAR=16.4
Pod documented
26 1 my $self = shift;
27 1 ▶ my $map = $self->{'_app_for_host'} ||= {};
Condition50%1/2
$self->{"_app_for_host"} ||= {}
l!l
10
MC/DC0%0/1
$self->{"_app_for_host"} ||= {}
$self->{"_app_for_host"}
Truth table50%1/2
A
A= $self->{"_app_for_host"}
Aresult
11
00
28 1 ▶ while ( my ( $host, $host_app ) = each %$map ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$map) 6 1 7
29 6 ▶ next if not grep { $host_app == $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$host_app == $_;} @_ 0 6 6
30 6 delete $map->{ $host };
31 6 ▶ --$self->{'_num_wildcards'} if $host =~ /\A\*\*\./;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /\A\*\*\./ 0 6 6
32 }
33 1 return $self;
34 }
35  
36 21 ▶sub matching {
Subroutine matching: called CC=9 SCAR=22.5
Pod documented
37 21 my $self = shift;
38 21 my $host = lc $_[0];
39  
40 21 my $map = $self->{'_app_for_host'};
41  
42 21 ▶ return $_ for $map->{ $host } || ();
Condition66%2/3
$map->{$host} || ()
l!l&&r!l&&!r
10011
MC/DC50%1/2
$map->{$host} || ()
$map->{$host} ()
Truth table66%2/3
A || B
A= $map->{$host}B= ()
ABresult
1X1
011
000
43  
44 11 ▶ if ( $self->{'_num_wildcards'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_num_wildcards"}) 4 7 11
45 4 my @part = split /\./, $host, 16;
46 4 for my $pattern ( map { shift @part; join '.', '**', @part } 1 .. $#part ) {
47 19 ▶ return $_ for $map->{ $pattern } || ();
Condition66%2/3
$map->{$pattern} || ()
l!l&&r!l&&!r
2170
MC/DC0%0/2
$map->{$pattern} || ()
$map->{$pattern} ()
Truth table66%2/3
A || B
A= $map->{$pattern}B= ()
ABresult
1X1
011
000
48 }
49 }
50  
51 9 ▶ return undef unless my $cb = $self->custom_matcher;
Branch: 1 branch
BranchTrueFalseTotal
unless my $cb = $self->custom_matcher 9 0 9
52  
53 0 return scalar $cb->() for $host;
54 }
55  
56 1 our $sadtrombone = [
57 400,
58 [qw( Content-Type text/html )],
59 ['<!DOCTYPE html><title>Bad Request</title><center style="font-family: sans-serif"><h1>Bad Request</h1><p>Unknown host or domain'],
60 ];
61  
62 24 ▶sub call {
Subroutine call: called CC=5 SCAR=16.1
Pod documented
63 24 my $self = shift;
64 24 my ( $env ) = @_;
65  
66 24 my $host = $env->{'HTTP_HOST'};
67  
68 24 ▶ my $app = defined $host
Branch: 1 branch
BranchTrueFalseTotal
defined $host ? : 21 3 24
Condition100%3/3
$self->matching($host) || $self->default_app
l!l&&r!l&&!r
1227
MC/DC100%2/2
$self->matching($host) || $self->default_app
$self->matching($host) $self->default_app
Truth table100%3/3
A || B
A= $self->matching($host)B= $self->default_app
ABresult
1X1
011
000
69 ? ( $host =~ s/:$env->{'SERVER_PORT'}\z//, $self->matching( $host ) || $self->default_app )
70 : $self->missing_header_app;
71  
72 24 ▶ return 'CODE' eq ref $app ? &$app : $app || $sadtrombone;
Branch: 1 branch
BranchTrueFalseTotal
'CODE' eq ref $app ? : 14 10 24
Condition66%2/3
$app or $Plack::App::Hostname::sadtrombone
l!l&&r!l&&!r
190
MC/DC0%0/2
$app or $Plack::App::Hostname::sadtrombone
$app $Plack::App::Hostname::sadtrombone
Truth table66%2/3
A or B
A= $appB= $Plack::App::Hostname::sadtrombone
ABresult
1X1
011
000
73 }
74  
75 1 1;
↑ Summary