| 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
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | my @ip = ref $self->allow ? @{$self->allow} : ($self->allow); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 22 | 0 | push @ipv6, $_; | |||||||||||||||||||||||||||
| 23 | } else { | ||||||||||||||||||||||||||||
| 24 | 3 | push @ipv4, $_; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | 4 | ▶ | if ( @ipv4 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->allow and $self->path
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$path and $env->{"PATH_INFO"} =~ /^$path/
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/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 82 | 2 | $input_module =~ s/-/::/g; | |||||||||||||||||||||||||||
| 83 | 2 | ▶ | if ( my $mod = Module::Info->new_from_loaded($input_module) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | $info->{module} = { | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | name => $mod->name, | ||||||||||||||||||||||||||
|
Condition33%1/3
eval "\$${input_module}::VERSION" || $mod->version
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return unless $self->{__cidr6}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | return $self->{__cidr6}->find( $address ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 4 | ▶ | return unless $self->{__cidr4}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 3 | return $self->{__cidr4}->find( $address ); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | 1; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||