File Coverage

blib/lib/Alien/Build/Plugin/Fetch/HostBlockList.pm
Criterion Covered Total %
statement 30 30 100.0
branch 5 8 62.5
condition 2 3 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 45 49 91.8


line stmt bran cond sub pod time code
1             package Alien::Build::Plugin::Fetch::HostBlockList;
2              
3 1     1   58838 use strict;
  1         3  
  1         24  
4 1     1   4 use warnings;
  1         4  
  1         18  
5 1     1   14 use 5.008004;
  1         3  
6 1     1   6 use Alien::Build::Plugin;
  1         2  
  1         6  
7 1     1   595 use URI;
  1         4433  
  1         240  
8              
9             # ABSTRACT: Reject any Alien::Build fetch requests going to hosts in the block list
10             our $VERSION = '0.01'; # VERSION
11              
12              
13             has '+block_hosts' => sub { [
14             defined $ENV{ALIEN_BUILD_HOST_BLOCK}
15             ? split /,/, $ENV{ALIEN_BUILD_HOST_BLOCK}
16             : ()
17             ] };
18              
19             sub init
20             {
21 1     1 1 107 my($self, $meta) = @_;
22              
23 1         2 my %blocked = map { $_ => 1 } @{ $self->block_hosts };
  2         14  
  1         3  
24              
25             $meta->around_hook( fetch => sub {
26              
27 3     3   10034 my $orig = shift;
28 3         6 my $build = shift;
29 3   66     12 my $url = $_[0] || $build->meta_prop->{start_url};
30              
31 3 50       23 if($url =~ /:/)
32             {
33 3         11 my $url = URI->new($url);
34 3 50       6970 if($url->scheme ne 'file')
35             {
36 3         124 my $host = eval { $url->host };
  3         10  
37 3 50       102 die "unable to determine host from $url: $@" if $@;
38 3 100       28 die "The host $host is in the block list" if $blocked{$host};
39             }
40             }
41              
42 1         13 $orig->($build, @_);
43 1         8 });
44             }
45              
46              
47             1;
48              
49             __END__