blib/lib/Plack/Middleware/ModuleInfo.pm

filter: Statementstmt 89.5% 60 / 67 Branchbran 71.4% 20 / 28 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 79.6% 98 / 123 CC 21 SCAR 20.8
CC
21
Cov
81%
prepare_app
15.6
_allowed
5.2
_handle_module_info
5.0
CRAP 8.0
↑ Summary
1 package Plack::Middleware::ModuleInfo;
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 Plack::Util::Accessor qw/ path allow dumper /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Net::CIDR::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Module::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.04';
10  
11 4 ▶sub prepare_app {
Subroutine prepare_app: called CC=12 SCAR=27.5
Pod documented
12 4 my $self = shift;
13  
14 # this code of block was copied from Plack::Middleware::ServerStatus::Lite ;-P
15 4 ▶ if ( $self->allow ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow) 4 0 4
16 4 ▶ my @ip = ref $self->allow ? @{$self->allow} : ($self->allow);
Branch: 1 branch
BranchTrueFalseTotal
ref $self->allow ? : 1 3 4
17 4 my @ipv4;
18 my @ipv6;
19 4 for (@ip) {
20 # hacky check, but actual checks are done in Net::CIDR::Lite.
21 3 ▶ if (/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/:/) { } 0 3 3
22 0 push @ipv6, $_;
23 } else {
24 3 push @ipv4, $_;
25 }
26 }
27 4 ▶ if ( @ipv4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ipv4) 3 1 4
28 3 my $cidr4 = Net::CIDR::Lite->new();
29 3 $cidr4->add_any($_) for @ipv4;
30 3 $self->{__cidr4} = $cidr4;
31 }
32 4 ▶ if ( @ipv6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ipv6) 0 4 4
33 0 my $cidr6 = Net::CIDR::Lite->new();
34 0 $cidr6->add_any($_) for @ipv6;
35 0 $self->{__cidr6} = $cidr6;
36 }
37 }
38  
39 4 ▶ if (!$self->allow || !$self->path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->allow and $self->path) 0 4 4
Condition33%1/3
$self->allow and $self->path
!ll&&!rl&&r
004
MC/DC0%0/2
$self->allow and $self->path
$self->allow $self->path
Truth table33%1/3
A and B
A= $self->allowB= $self->path
ABresult
0X0
100
111
40 0 warn "[Plack::Middleware::ModuleInfo] 'allow' is not provided."
41 . "Any host will not be able to access the information.\n";
42 }
43  
44 4 ▶ unless ($self->dumper) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dumper) 4 0 4
45 4 require YAML;
46 3 ▶ $self->dumper(sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
47 3 my ($info) = @_;
48 return [
49 3 200,
50 ['Content-Type' => 'text/plain'],
51 [YAML::Dump($info)]
52 ];
53 4 });
54 }
55 }
56  
57 5 ▶sub call {
Subroutine call: called CC=3 SCAR=11.0
Pod documented
58 5 my ($self, $env) = @_;
59  
60 5 my $path = $self->path;
61  
62 5 ▶ if( $path && $env->{PATH_INFO} =~ m!^$path! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and $env->{"PATH_INFO"} =~ /^$path/) 4 1 5
Condition66%2/3
$path and $env->{"PATH_INFO"} =~ /^$path/
!ll&&!rl&&r
014
MC/DC50%1/2
$path and $env->{"PATH_INFO"} =~ /^$path/
$path $env->{"PATH_INFO"} =~ /^$path/
Truth table66%2/3
A and B
A= $pathB= $env->{"PATH_INFO"} =~ /^$path/
ABresult
0X0
100
111
63 4 my $res = $self->_handle_module_info($env);
64 4 return $res;
65 }
66  
67 1 my $res = $self->app->($env);
68  
69 1 return $res;
70 }
71  
72 4 ▶sub _handle_module_info {
Subroutine _handle_module_info: called CC=5 SCAR=16.2
73 4 my ($self, $env) = @_;
74  
75 4 ▶ if ( ! $self->_allowed($env->{REMOTE_ADDR}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_allowed($env->{"REMOTE_ADDR"})) 1 3 4
76 1 return [403, ['Content-Type' => 'text/plain'], [ 'Forbidden' ]];
77 }
78  
79 3 my $info = { PID => $$, lib => \@INC, };
80  
81 3 ▶ if ( my $input_module = $env->{QUERY_STRING} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $input_module = $env->{"QUERY_STRING"}) 2 1 3
82 2 $input_module =~ s/-/::/g;
83 2 ▶ if ( my $mod = Module::Info->new_from_loaded($input_module) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mod = 'Module::Info'->new_from_loaded($input_module)) { } 1 1 2
84 $info->{module} = {
85 1 ▶ name => $mod->name,
Condition33%1/3
eval "\$${input_module}::VERSION" || $mod->version
l!l&&r!l&&!r
100
MC/DC0%0/2
eval "\$${input_module}::VERSION" || $mod->version
eval "\$${input_module}::VERSION" $mod->version
Truth table33%1/3
A || B
A= eval "\$${input_module}::VERSION"B= $mod->version
ABresult
1X1
011
000
86 version => eval "\$$input_module\::VERSION" || $mod->version, ## no critic
87 file => $mod->file,
88 };
89 }
90 else {
91 1 $info->{module} = "'$input_module' not found";
92 }
93 }
94  
95 3 return $self->dumper->($info, $env);
96 }
97  
98 4 ▶sub _allowed {
Subroutine _allowed: called CC=4 SCAR=16.4
99 4 my ( $self , $address ) = @_;
100  
101 4 ▶ if ( $address =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($address =~ /:/) 0 4 4
102 0 ▶ return unless $self->{__cidr6};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__cidr6"} 0 0 0
103 0 return $self->{__cidr6}->find( $address );
104 }
105 4 ▶ return unless $self->{__cidr4};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__cidr4"} 1 3 4
106 3 return $self->{__cidr4}->find( $address );
107 }
108  
109 2 1;
110  
111 __END__
↑ Summary