blib/lib/Plack/Middleware/IPMatch.pm

filter: Statementstmt 90.3% 47 / 52 Branchbran 58.3% 7 / 12 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 79.0% 68 / 86 CC 12 SCAR 14.3
CC
12
Cov
80%
prepare_app
6.2
_build_real_ip
6.0
call
4.1
CRAP 4.2
↑ Summary
1 package Plack::Middleware::IPMatch;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent qw/Plack::Middleware/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Net::IP::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Net::IP::Match::Trie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $VERSION = 0.04;
8  
9 2 ▶use Plack::Util::Accessor qw( IPFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶sub _build_real_ip {
Subroutine _build_real_ip: called CC=4 SCAR=17.9
12 8 my ($env) = @_;
13  
14 my @possible_forwarded_ips
15 = grep {
16 0 $_->iptype
17 !~ /^(?:LOOPBACK|LINK\-LOCAL|PRIVATE|UNIQUE\-LOCAL\-UNICAST|LINK\-LOCAL\-UNICAST|RESERVED)$/xo
18 }
19 0 grep {defined}
20 0 map { Net::IP::XS->new($_) }
21 48 ▶ grep {defined} (
Condition50%1/2
$env->{"HTTP_X_FORWARDED_FOR"} // ""
l!l
08
MC/DC0%0/1
$env->{"HTTP_X_FORWARDED_FOR"} // ""
$env->{"HTTP_X_FORWARDED_FOR"}
Truth table50%1/2
A
A= $env->{"HTTP_X_FORWARDED_FOR"}
Aresult
11
00
22 $env->{'HTTP_X_REAL_IP'},
23 $env->{'HTTP_CLIENT_IP'},
24 split( /,\s*/xo, $env->{'HTTP_X_FORWARDED_FOR'} // '' ),
25 $env->{'HTTP_X_FORWARDED'},
26 $env->{'HTTP_X_CLUSTER_CLIENT_IP'},
27 $env->{'HTTP_FORWARDED_FOR'},
28 8 $env->{'HTTP_FORWARDED'},
29 );
30  
31 return $possible_forwarded_ips[0]
32 8 ▶ // Net::IP::XS->new( $env->{'REMOTE_ADDR'} // '' );
Condition40%2/5
$possible_forwarded_ips[0] // "Net::IP::XS"->new($env->{"REMOTE_ADDR"} // "")
l!l&&r!l&&!r
080
$env->{"REMOTE_ADDR"} // ""
l!l
80
MC/DC0%0/2
$possible_forwarded_ips[0] // "Net::IP::XS"->new($env->{"REMOTE_ADDR"} // "")
$possible_forwarded_ips[0] "Net::IP::XS"->new($env->{"REMOTE_ADDR"} // "")
MC/DC0%0/1
$env->{"REMOTE_ADDR"} // ""
$env->{"REMOTE_ADDR"}
Truth table33%1/3
A // B
A= $possible_forwarded_ips[0]B= "Net::IP::XS"->new($env->{"REMOTE_ADDR"} // "")
ABresult
1X1
011
000
Truth table50%1/2
A
A= $env->{"REMOTE_ADDR"}
Aresult
11
00
33 }
34  
35  
36 8 ▶sub prepare_app {
Subroutine prepare_app: called CC=6 SCAR=18.3
Pod documented
37 8 my $self = shift;
38  
39 8 ▶ if (my $ipfiles = $self->IPFile) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ipfiles = $self->IPFile) 8 0 8
40 0 ▶ my @ipfiles = ref $ipfiles ? @{ $ipfiles } : ($ipfiles);
Branch: 1 branch
BranchTrueFalseTotal
ref $ipfiles ? : 0 8 8
41 8 for my $ipfile (@ipfiles) {
42 8 my $match = Net::IP::Match::Trie->new();
43  
44 8 ▶ open my $fh, "<", $ipfile or die "$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $ipfile or die "$!" 8 0 8
45 8 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 88 8 96
46 88 chomp;
47 88 my ($CIDRS, $lable) = split(/[,|\s]/, $_);
48 88 $match->add( $lable , [$CIDRS] );
49 }
50 8 push @{ $self->{IPMatcher} }, $match;
51 }
52 }
53 }
54  
55 8 ▶sub call {
Subroutine call: called CC=4 SCAR=14.1
Pod documented
56 8 my $self = shift;
57 8 my $env = shift;
58  
59 8 my $ip;
60 8 ▶ if ($env->{QUERY_STRING} =~ /(?:^|&)ip=([^&]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{'QUERY_STRING'} =~ /(?:^|&)ip=([^&]+)/) { } 0 8 8
61 0 $ip = $1;
62 }
63 else {
64 8 $ip = _build_real_ip($env)->ip;
65 }
66 8 $env->{MATCH_IP} = $ip;
67  
68 8 foreach my $matcher (@{ $self->{IPMatcher} }) {
69 8 my $label = $matcher->match_ip($ip);
70 8 ▶ $env->{IPMATCH_LABEL} = $label and last;
Branch: 1 branch
BranchTrueFalseTotal
$env->{"IPMATCH_LABEL"} = $label and last 8 0 8
71 }
72  
73 8 return $self->app->($env);
74 }
75  
76 2 1;
77 __END__
↑ Summary