File Coverage

blib/lib/AWS/S3/Roles/Bucket.pm
Criterion Covered Total %
statement 15 15 100.0
branch 9 10 90.0
condition 7 9 77.7
subroutine 3 3 100.0
pod 0 2 0.0
total 34 39 87.1


line stmt bran cond sub pod time code
1             package AWS::S3::Roles::Bucket;
2              
3 2     2   2113 use Moose::Role;
  2         4108  
  2         15  
4              
5             sub bucket_uri {
6 7     7 0 20 my ( $s,$path ) = @_;
7              
8 7   66     148 $path //= $s->bucket;
9 7 50       147 my $protocol = $s->s3->secure ? 'https' : 'http';
10 7         150 my $endpoint = $s->s3->endpoint;
11 7         144 my $uri = "$protocol://$endpoint/$path";
12 7 100 66     60 if ( $path =~ m{^([^/?]+)(.*)} && $s->is_dns_bucket( $1 ) ) {
13 5         24 $uri = "$protocol://$1.$endpoint$2";
14             } # end if()
15              
16 7         21 return $uri;
17             }
18              
19             sub is_dns_bucket {
20 14     14 0 835 my ( $s,$bucket ) = @_;
21              
22             # https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
23 14 100 100     77 return 0 if ( length( $bucket ) < 3 or length( $bucket ) > 63 );
24 12 100       41 return 0 if $bucket =~ /^(?:\d{1,3}\.){3}\d{1,3}$/;
25              
26             # DNS bucket names can contain lowercase letters, numbers, and hyphens
27             # so anything outside this range we say isn't a valid DNS bucket
28 11 100       77 return $bucket =~ /[^a-z0-9-\.]/ ? 0 : 1;
29             }
30              
31             1;