| 1 | package URI::pack; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008003; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | ############################################################################### | ||||||||||||||||||||||||||||
| 8 | # METADATA | ||||||||||||||||||||||||||||
| 9 | 3 | our $AUTHORITY = 'cpan:DOUGDUDE'; | |||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '0.002001'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | ############################################################################### | ||||||||||||||||||||||||||||
| 13 | # MODULES | ||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Const::Fast qw(const); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use URI::Escape qw(uri_escape uri_unescape); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ############################################################################### | ||||||||||||||||||||||||||||
| 20 | # INHERIT FROM PARENT CLASS | ||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use parent qw(URI::_generic); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ############################################################################### | ||||||||||||||||||||||||||||
| 24 | # CONSTANTS | ||||||||||||||||||||||||||||
| 25 | 3 | const my $UNRESERVED => qr{[0-9A-Za-z\-\._~]}msx; | |||||||||||||||||||||||||||
| 26 | 3 | const my $PCT_ENCODED => qr{\%[0-9A-Fa-f]{2}}msx; | |||||||||||||||||||||||||||
| 27 | 3 | const my $SUB_DELIMS => qr{[!\$\&'\(\)\*\+,;=]}msx; | |||||||||||||||||||||||||||
| 28 | 3 | const my $PCHAR => qr{(?:$UNRESERVED|$PCT_ENCODED|$SUB_DELIMS|[:\@])}msx; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | ############################################################################### | ||||||||||||||||||||||||||||
| 31 | # ALL IMPORTS BEFORE THIS WILL BE ERASED | ||||||||||||||||||||||||||||
| 32 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | ############################################################################### | ||||||||||||||||||||||||||||
| 35 | # METHODS | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub clear_package_uri { | ||||||||||||||||||||||||||
|
Subroutine
clear_package_uri: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # This will remove the package by changing the authority to q{} | ||||||||||||||||||||||||||||
| 40 | 0 | $self->authority(q{}); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | return; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub clear_part_name { | ||||||||||||||||||||||||||
|
Subroutine
clear_part_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # This will remove the part name by changing the path to / | ||||||||||||||||||||||||||||
| 48 | 0 | $self->path(q{/}); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | return; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 5 | ▶ | sub has_package_uri { | ||||||||||||||||||||||||||
|
Subroutine
has_package_uri: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Does this URI have a package? | ||||||||||||||||||||||||||||
| 56 | 5 | ▶ | return defined $self->authority && $self->authority ne q{}; | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $self->authority && $self->authority ne ''
MC/DC50%1/2
defined $self->authority && $self->authority ne ''
defined $self->authority
$self->authority ne ''
Truth table66%2/3
A && B
A= defined $self->authorityB= $self->authority ne ''
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 15 | ▶ | sub has_part_name { | ||||||||||||||||||||||||||
|
Subroutine
has_part_name: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 15 | my ($self) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Does this URI have a part name? | ||||||||||||||||||||||||||||
| 62 | 15 | ▶ | return $self->path ne q{} && $self->path ne q{/}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->path ne '' && $self->path ne '/'
MC/DC0%0/2
$self->path ne '' && $self->path ne '/'
$self->path ne ''
$self->path ne '/'
Truth table33%1/3
A && B
A= $self->path ne ''B= $self->path ne '/'
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 3 | ▶ | sub package_uri { | ||||||||||||||||||||||||||
|
Subroutine
package_uri: called CC=2 SCAR=8.7
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 3 | my ($self, $new_package) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Get the package according to ECMA-376, Part 2, section B.2 | ||||||||||||||||||||||||||||
| 68 | # Call the normal authority and get the result | ||||||||||||||||||||||||||||
| 69 | 3 | my $authority = $self->authority; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Replace all commas with forward slashes | ||||||||||||||||||||||||||||
| 72 | 3 | $authority =~ s{,}{/}gmsx; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Unescape the authority | ||||||||||||||||||||||||||||
| 75 | 3 | $authority = uri_unescape($authority); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | ▶ | if (defined $new_package) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | # Set a new authority according to ECMA-376, Part 2, section B.3 | ||||||||||||||||||||||||||||
| 79 | # Make sure the new package is a URI | ||||||||||||||||||||||||||||
| 80 | 0 | $new_package = URI->new($new_package); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Remove the fragment | ||||||||||||||||||||||||||||
| 83 | 0 | $new_package->fragment(q{}); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Escape all %, ?, @, :, and , characters | ||||||||||||||||||||||||||||
| 86 | ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) | ||||||||||||||||||||||||||||
| 87 | 0 | $new_package = uri_escape($new_package, '%?@:,'); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Replace all forward slashes with commas | ||||||||||||||||||||||||||||
| 90 | 0 | $new_package =~ s{/}{,}gmsx; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Set the resulting string as the authority | ||||||||||||||||||||||||||||
| 93 | 0 | $self->authority($new_package); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Return the authority as an URI object | ||||||||||||||||||||||||||||
| 97 | 3 | return URI->new($authority); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 20 | ▶ | sub part_name { | ||||||||||||||||||||||||||
|
Subroutine
part_name: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 20 | my ($self, $new_part_name) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # The part name is simply the path | ||||||||||||||||||||||||||||
| 103 | 20 | my $part_name = $self->path; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 20 | ▶ | if (defined $new_part_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | # Set the new part name | ||||||||||||||||||||||||||||
| 107 | 14 | ▶ | if ($self->_is_valid_part_uri($new_part_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | # Set the new part name since it is valid | ||||||||||||||||||||||||||||
| 109 | 3 | $self->path($new_part_name); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||
| 112 | 0 | croak 'The part name given was not a valid part name was thus was not set'; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 9 | ▶ | if (!$self->has_part_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | return; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 9 | return $part_name; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 4 | ▶ | sub part_name_segments { | ||||||||||||||||||||||||||
|
Subroutine
part_name_segments: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 4 | my ($self, @new_part_name_segments) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # Get the path segments | ||||||||||||||||||||||||||||
| 126 | 4 | my @path_segments = $self->path_segments; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Remove the first path segment, as it is q{} | ||||||||||||||||||||||||||||
| 129 | 4 | ▶ | if (@path_segments && $path_segments[0] eq q{}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@path_segments and $path_segments[0] eq ""
MC/DC0%0/2
@path_segments and $path_segments[0] eq ""
@path_segments
$path_segments[0] eq ""
Truth table33%1/3
A and B
A= @path_segmentsB= $path_segments[0] eq ""
| |||||||||||||||||||||||||||||
| 130 | 4 | shift @path_segments; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | ▶ | if (@new_part_name_segments) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | # Set the new part name | ||||||||||||||||||||||||||||
| 135 | 1 | $self->part_name(q{/} . join q{/}, @new_part_name_segments); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 4 | return @path_segments; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | ############################################################################### | ||||||||||||||||||||||||||||
| 142 | # PRIVATE METHODS | ||||||||||||||||||||||||||||
| 143 | 3 | ▶ | sub _check_uri { | ||||||||||||||||||||||||||
|
Subroutine
_check_uri: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||
| 144 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # If the URI has a part name, check it | ||||||||||||||||||||||||||||
| 147 | 3 | ▶ | if ($self->has_part_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | # Check the part | ||||||||||||||||||||||||||||
| 149 | 3 | $self->_is_valid_part_uri($self->path); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # Must have either package or part name | ||||||||||||||||||||||||||||
| 153 | 3 | ▶ | if (!$self->has_package_uri && !$self->has_part_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->has_package_uri or $self->has_part_name
MC/DC0%0/2
$self->has_package_uri or $self->has_part_name
$self->has_package_uri
$self->has_part_name
Truth table66%2/3
A or B
A= $self->has_package_uriB= $self->has_part_name
| |||||||||||||||||||||||||||||
| 154 | 0 | croak 'Not a valid URI'; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 3 | return $self; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 3 | ▶ | sub _init { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 3 | my ($class, $uri, $scheme) = @_; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Create and bless into class using default _init | ||||||||||||||||||||||||||||
| 163 | 3 | my $self = $class->SUPER::_init($uri, $scheme); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Check the URI | ||||||||||||||||||||||||||||
| 166 | 3 | $self->_check_uri(); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 3 | return $self; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 17 | ▶ | sub _is_valid_part_uri { | ||||||||||||||||||||||||||
|
Subroutine
_is_valid_part_uri: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 171 | 17 | my ($self, $part_uri) = @_; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # Validate a part URI according to ECMA-376 Part 2, section 9.1.1.1.2 | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 17 | ▶ | if ($part_uri eq q{}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 1 | croak 'A part URI shall not be empty [M1.1]'; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 16 | ▶ | if ($part_uri !~ m{\A /}msx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 1 | croak 'A part URI shall start with a forward slash ("/") character [M1.4]'; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 15 | ▶ | if ($part_uri =~ m{/ \z}msx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 1 | croak 'A part URI shall not have a forward slash as the last character [M1.5]'; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Split the part URI into segments | ||||||||||||||||||||||||||||
| 188 | 14 | my @segments = split m{/}msx, $part_uri; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Remove the first empty segment | ||||||||||||||||||||||||||||
| 191 | 14 | ▶ | if ($segments[0] eq q{}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 14 | shift @segments; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 14 | foreach my $segment (@segments) { | |||||||||||||||||||||||||||
| 196 | 24 | ▶ | if ($segment eq q{}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 2 | croak 'A part URI shall not have empty segments [M1.3]'; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 22 | ▶ | if ($segment !~ m{\A (?:$PCHAR)+ \z}msx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 1 | croak 'A segments shall not hold any characters other than pchar characters [M1.6]'; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 21 | ▶ | if ($segment =~ m{\%(?:2f|5c)}imsx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 2 | croak 'A segments shall not contain percent-encoded forward slash ("/"), or backward slash ("\") characters [M1.7]'; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 19 | ▶ | while ($segment =~ m{%([0-9a-f]{2})}gimsx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | # Convert the byte into the original character | ||||||||||||||||||||||||||||
| 210 | 3 | my $character = chr hex $1; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 3 | ▶ | if ($character =~ m{\A [0-9A-Z\-\._~] \z}imsx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 1 | croak 'A segment shall not contain percent-encoded unreserved characters [M1.8]'; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 18 | ▶ | if ($segment =~ m{\. \z}msx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 2 | croak 'A segment shall not end with a dot (".") character [M1.9]'; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 16 | ▶ | if ($segment !~ m{[^\.]+}msx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | croak 'A segment shall include at least one non-dot character [M1.10]'; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 6 | return 1; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | 1 | ▶ | sub _no_scheme_ok { return 0; } ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||
|
Subroutine
_no_scheme_ok: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 3 | 1; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | __END__ | ||||||||||||||||||||||||||||