PackageURL.pm

filter: Statementstmt 95.8% 160 / 167 Branchbran 81.0% 47 / 58 Conditioncond 62.9% 17 / 27 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 91.1% 31 / 34 Podpod 100% 19 / 19 totaltotal 87.1% 279 / 320 CC 42 SCAR 23.4
CC
42
Cov
89%
from_string
24.9
to_string
11.2
new
6.0
CRAP 10.4
« VersionRange/Util.pm ↑ Summary VersionRange.pm »
1 package URI::PackageURL;
2  
3 9 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use URI::PackageURL::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use URI::PackageURL::Util qw(purl_to_urls);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶BEGIN { *PURL:: = *URI::PackageURL:: }
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use constant DEBUG => $ENV{PURL_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use overload '""' => 'to_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 our $VERSION = '2.25';
21 9 our @EXPORT = qw(encode_purl decode_purl);
22  
23 9 my $PURL_REGEXP = qr{^pkg:(([/]{1,})?)([A-Za-z][A-Za-z0-9\.\-]*)([/]{1,}).+};
24  
25 1645 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
26  
27 1645 my ($class, %params) = @_;
28  
29 1645 ▶ my $type = delete $params{type} or Carp::croak "Invalid PURL: 'type' component is required";
Branch: 1 branch
BranchTrueFalseTotal
my $type = delete $params{"type"} or Carp::croak("Invalid PURL: 'type' component is required") 1636 9 1645
30 1636 my $namespace = delete $params{namespace};
31 1636 ▶ my $name = delete $params{name} or Carp::croak "Invalid PURL: 'name' component is required";
Branch: 1 branch
BranchTrueFalseTotal
my $name = delete $params{"name"} or Carp::croak("Invalid PURL: 'name' component is required") 1626 10 1636
32 1626 my $version = delete $params{version};
33 1626 ▶ my $qualifiers = delete $params{qualifiers} // {};
Condition100%2/2
delete $params{"qualifiers"} // {}
l!l
1232394
MC/DC100%1/1
delete $params{"qualifiers"} // {}
delete $params{"qualifiers"}
Truth table100%2/2
A
A= delete $params{"qualifiers"}
Aresult
11
00
34 1626 my $subpath = delete $params{subpath};
35  
36 1626 ▶ my $validate = delete $params{validate} // 1;
Condition50%1/2
delete $params{"validate"} // 1
l!l
01626
MC/DC0%0/1
delete $params{"validate"} // 1
delete $params{"validate"}
Truth table50%1/2
A
A= delete $params{"validate"}
Aresult
11
00
37  
38 1626 my $purl_definition = URI::PackageURL::Type->new($type);
39  
40 1626 my %components = $purl_definition->normalize(
41 scheme => 'pkg', # The scheme is a constant with the value "pkg".
42 type => $type,
43 namespace => $namespace,
44 name => $name,
45 version => $version,
46 qualifiers => $qualifiers,
47 subpath => $subpath,
48 );
49  
50 1626 ▶ $purl_definition->validate(%components) if $validate;
Branch: 1 branch
BranchTrueFalseTotal
if $validate 1626 0 1626
51  
52 1567 my $self = {components => \%components, definition => $purl_definition};
53  
54 1567 return bless $self, $class;
55  
56 }
57  
58 1859 ▶sub definition { shift->{definition} }
Subroutine definition: called CC=1 SCAR=0.0
Pod documented
59  
60 0 ▶sub scheme {'pkg'} # The scheme is a constant with the value "pkg".
Subroutine scheme: not called CC=1 SCAR=6.9
Pod documented
61 6329 ▶sub type { shift->_component('type', @_) }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
62 6667 ▶sub namespace { shift->_component('namespace', @_) }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
63 4487 ▶sub name { shift->_component('name', @_) }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
64 6805 ▶sub version { shift->_component('version', @_) }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
65 7110 ▶sub qualifiers { shift->_component('qualifiers', @_) }
Subroutine qualifiers: called CC=1 SCAR=0.0
Pod documented
66 2628 ▶sub subpath { shift->_component('subpath', @_) }
Subroutine subpath: called CC=1 SCAR=0.0
Pod documented
67  
68 5 ▶sub encode_purl { __PACKAGE__->new(@_)->to_string }
Subroutine encode_purl: called CC=1 SCAR=0.0
Pod documented
69 6 ▶sub decode_purl { __PACKAGE__->from_string(shift) }
Subroutine decode_purl: called CC=1 SCAR=0.0
Pod documented
70  
71 927 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
72 927 my $self = shift;
73 927 bless {%$self}, ref $self;
74 }
75  
76 1 ▶sub to_urls { purl_to_urls(shift) }
Subroutine to_urls: called CC=1 SCAR=0.0
Pod documented
77 0 ▶sub download_url { shift->to_urls->{download} }
Subroutine download_url: not called CC=1 SCAR=6.9
Pod documented
78 0 ▶sub repository_url { shift->to_urls->{repository} }
Subroutine repository_url: not called CC=1 SCAR=6.9
Pod documented
79  
80 499 ▶sub from_string {
Subroutine from_string: called CC=23 SCAR=32.1
Pod documented
81  
82 499 my ($class, $string) = @_;
83  
84 499 DEBUG and say STDERR "-- INPUT: $string";
85 499 DEBUG and say STDERR "-- REGEXP: $PURL_REGEXP";
86  
87 # Strip slash / after scheme
88 499 ▶ while ($string =~ m{^pkg:/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ m[^pkg:/]) 20 499 519
89 20 $string =~ s{^pkg:/}{pkg:};
90 }
91  
92 499 ▶ if ($string !~ /$PURL_REGEXP/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /$PURL_REGEXP/) 26 473 499
93 26 Carp::croak 'Malformed PURL string';
94 }
95  
96 473 my %components = ();
97  
98  
99 # Split the purl string once from right on '#'
100 # The left side is the 'remainder'
101 # Strip the right side from leading and trailing '/'
102 # Split this on '/'
103 # Discard any empty string segment from that split
104 # Discard any '.' or '..' segment from that split
105 # Percent-decode each segment
106 # UTF-8-decode each segment if needed in your programming language
107 # Join segments back with a '/'
108 # This is the 'subpath'
109  
110 473 my @s1 = split(/#([^#]+)$/, $string);
111  
112 473 ▶ if ($s1[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s1[1]) 43 430 473
113 43 $s1[1] =~ s/(^\/|\/$)//;
114 43 ▶ my @subpath = map { _url_decode($_) } grep { $_ ne '' && $_ ne '.' && $_ ne '..' } split /\//, $s1[1];
Condition33%2/6
$_ ne '' && $_ ne '.' && $_ ne '..'
!ll&&!rl&&r
00103
$_ ne "" and $_ ne "."
!ll&&!rl&&r
00103
MC/DC0%0/3
$_ ne '' && $_ ne '.' && $_ ne '..'
$_ ne "" $_ ne "." $_ ne '..'
Truth table25%1/4
A and B && C
A= $_ ne ""B= $_ ne "."C= $_ ne '..'
ABCresult
0XX0
10X0
1100
1111
115 43 $components{subpath} = join '/', @subpath;
116 }
117  
118  
119 # Split the 'remainder' once from right on '?'
120 # The left side is the 'remainder'
121 # The right side is the 'qualifiers' string
122 # Split the 'qualifiers' on '&'. Each part is a 'key=value' pair
123 # For each pair, split the 'key=value' once from left on '=':
124 # The 'key' is the lowercase left side
125 # The 'value' is the percent-decoded right side
126 # UTF-8-decode the value if needed in your programming language
127 # Discard any key/value pairs where the value is empty
128 # If the 'key' is 'checksum', split the 'value' on ',' to create a list of checksum
129 # This list of key/value is the 'qualifiers' object
130  
131 473 my @s2 = split(/\?([^\?]+)$/, $s1[0]);
132  
133 473 ▶ if ($s2[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s2[1]) 192 281 473
134  
135 192 my @qualifiers = split('&', $s2[1]);
136  
137 192 foreach my $qualifier (@qualifiers) {
138  
139 310 my ($key, $value) = ($qualifier =~ /^([^=]+)(?:=(.*))?$/);
140 310 $value = _url_decode($value);
141  
142 310 ▶ if ($key eq 'checksums' || $key eq 'checksum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "checksums" or $key eq "checksum") 5 305 310
Condition66%2/3
$key eq "checksums" or $key eq "checksum"
l!l&&r!l&&!r
05305
MC/DC50%1/2
$key eq "checksums" or $key eq "checksum"
$key eq "checksums" $key eq "checksum"
Truth table66%2/3
A or B
A= $key eq "checksums"B= $key eq "checksum"
ABresult
1X1
011
000
143 5 ▶ Carp::carp "Detected 'checksums' qualifier. Use 'checksum' qualifier instead." if ($key eq 'checksums');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "checksums" 0 5 5
144 5 $value = [split(',', $value)];
145 }
146  
147 310 $components{qualifiers}->{lc $key} = $value;
148  
149 }
150  
151 }
152  
153  
154 # Split the 'remainder' once from left on ':'
155 # The left side lowercased is the 'scheme'
156 # The right side is the 'remainder'
157  
158 473 my @s3 = split(':', $s2[0], 2);
159  
160 473 ▶ Carp::croak 'Invalid PURL: Missing "scheme"' unless $s3[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $s3[0] 0 473 473
161 473 ▶ Carp::croak 'Invalid PURL' unless $s3[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $s3[1] 0 473 473
162  
163 473 $components{scheme} = lc $s3[0];
164  
165 # Strip all leading '/' characters (e.g., '/', '//', '///' and so on) from the 'remainder'
166 # Split this once from left on '/'
167 # The left side lowercased is the 'type'
168 # The right side is the 'remainder'
169  
170 473 ▶ while ($s3[1] =~ m|^//|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s3[1] =~ m[^//]) 0 473 473
171 0 $s3[1] =~ s|^//|/|;
172 }
173  
174 473 $s3[1] =~ s|^/||; # Strip leading '/' character
175  
176 473 my @s4 = split('/', $s3[1], 2);
177 473 $components{type} = lc $s4[0];
178  
179 473 ▶ Carp::croak 'Invalid PURL: Invalid "type"' if $components{type} !~ /^[a-z][a-z0-9.-]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $components{"type"} =~ /^[a-z][a-z0-9.-]+$/ 0 473 473
180 473 ▶ Carp::croak 'Invalid PURL' unless $s4[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $s4[1] 0 473 473
181  
182  
183 # Split the 'remainder' once from right on '@'
184 # The left side is the 'remainder'
185 # Percent-decode the right side. This is the 'version'.
186 # UTF-8-decode the 'version' if needed in your programming language
187 # This is the 'version'
188  
189 473 my @s5 = split(/@([^@]+)$/, $s4[1]);
190  
191 # NPM purl MAY have a namespace starting with "@"
192 # so we need to handle this case separately
193  
194 473 ▶ if ($components{type} eq 'npm' and $s4[1] =~ /^@/ and $s4[1] !~ /@.*@/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($components{"type"} eq "npm" and $s4[1] =~ /^@/ and not $s4[1] =~ /\@.*@/) 2 471 473
Condition83%5/6
$components{"type"} eq "npm" and $s4[1] =~ /^@/ and not $s4[1] =~ /\@.*@/
!ll&&!rl&&r
47102
$components{"type"} eq "npm" and $s4[1] =~ /^@/
!ll&&!rl&&r
454172
MC/DC66%2/3
$components{"type"} eq "npm" and $s4[1] =~ /^@/ and not $s4[1] =~ /\@.*@/
$components{"type"} eq "npm" $s4[1] =~ /^@/ not $s4[1] =~ /\@.*@/
Truth table75%3/4
A and B and not(C)
A= $components{"type"} eq "npm"B= $s4[1] =~ /^@/C= not $s4[1] =~ /\@.*@/
ABCresult
0XX0
10X0
1100
1111
195 2 @s5 = ($s4[1]);
196 }
197  
198 473 ▶ $components{version} = _url_decode($s5[1]) if ($s5[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $s5[1] 420 53 473
199  
200  
201 # Strip all trailing '/' characters (e.g., '/', '//', '///' and so on) from the 'remainder'
202 # The left side is the 'remainder'
203 # Percent-decode the right side. This is the 'name'
204 # UTF-8-decode this 'name' if needed in your programming language
205 # Apply type-specific normalization to the 'name' if needed
206 # This is the 'name'
207  
208 473 ▶ while ($s5[0] =~ m|//$|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s5[0] =~ m[//$]) 0 473 473
209 0 $s5[0] =~ s|//$|/|;
210 }
211  
212 473 $s5[0] =~ s|/$||; # Strip trailing '/' character
213  
214 473 my @s6 = split('/', $s5[0], -1);
215 473 $components{name} = _url_decode(pop @s6);
216  
217 473 ▶ Carp::croak 'Invalid PURL: Missing "name"' unless $components{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $components{"name"} 9 464 473
218  
219  
220 # Split the 'remainder' on '/'
221 # Discard any empty segment from that split
222 # Percent-decode each segment
223 # UTF-8-decode the each segment if needed in your programming language
224 # Apply type-specific normalization to each segment if needed
225 # Join segments back with a '/'
226 # This is the 'namespace'
227  
228 464 ▶ if (@s6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@s6) 261 203 464
229 261 $components{namespace} = join '/', map { _url_decode($_) } @s6;
230 }
231  
232  
233 464 if (DEBUG) {
234 say STDERR "-- S1: @s1";
235 say STDERR "-- S2: @s2";
236 say STDERR "-- S3: @s3";
237 say STDERR "-- S4: @s4";
238 say STDERR "-- S5: @s5";
239 say STDERR "-- S6: @s6";
240 }
241  
242 464 return $class->new(%components);
243  
244 }
245  
246 2366 ▶sub to_string {
Subroutine to_string: called CC=11 SCAR=24.2
Pod documented
247  
248 2366 my $self = shift;
249  
250 2366 my @purl = ('pkg', ':', $self->type, '/');
251  
252 # Namespace
253 2366 ▶ if ($self->namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->namespace) 2185 181 2366
254  
255 2185 my @ns = map { _url_encode($_) } split(/\//, $self->namespace);
256 2185 push @purl, (join('/', @ns), '/');
257  
258 }
259  
260 # Name
261 2366 push @purl, _encode($self->name);
262  
263 # Version
264 2366 ▶ push @purl, ('@', _encode($self->version)) if ($self->version);
Branch: 1 branch
BranchTrueFalseTotal
if $self->version 2320 46 2366
265  
266 # Qualifiers
267 2366 ▶ if (my $qualifiers = $self->qualifiers) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $qualifiers = $self->qualifiers) 2366 0 2366
268  
269 # TODO: Legacy 'checksums' qualifier will be dropped in the future
270 2366 foreach (qw[checksum checksums]) {
271 4732 ▶ if (defined $qualifiers->{$_} && ref $qualifiers->{$_} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $qualifiers->{$_} and ref $qualifiers->{$_} eq "ARRAY") 5 4727 4732
Condition66%2/3
defined $qualifiers->{$_} and ref $qualifiers->{$_} eq "ARRAY"
!ll&&!rl&&r
472705
MC/DC50%1/2
defined $qualifiers->{$_} and ref $qualifiers->{$_} eq "ARRAY"
defined $qualifiers->{$_} ref $qualifiers->{$_} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $qualifiers->{$_}B= ref $qualifiers->{$_} eq "ARRAY"
ABresult
0X0
100
111
272 5 $qualifiers->{$_} = join ',', @{$qualifiers->{$_}};
273 }
274 }
275  
276 # TODO: Use URI::VersionRange during qualifiers decode ?
277 2366 ▶ if (defined $qualifiers->{vers} && ref $qualifiers->{vers} eq 'URI::VersionRange') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $qualifiers->{"vers"} and ref $qualifiers->{"vers"} eq "URI::VersionRange") 0 2366 2366
Condition66%2/3
defined $qualifiers->{"vers"} and ref $qualifiers->{"vers"} eq "URI::VersionRange"
!ll&&!rl&&r
236510
MC/DC0%0/2
defined $qualifiers->{"vers"} and ref $qualifiers->{"vers"} eq "URI::VersionRange"
defined $qualifiers->{"vers"} ref $qualifiers->{"vers"} eq "URI::VersionRange"
Truth table66%2/3
A and B
A= defined $qualifiers->{"vers"}B= ref $qualifiers->{"vers"} eq "URI::VersionRange"
ABresult
0X0
100
111
278 0 $qualifiers->{vers} = $qualifiers->{vers}->to_string;
279 0 say STDERR $qualifiers->{vers};
280 }
281  
282 2188 my @qualifiers = map { sprintf('%s=%s', lc $_, _encode($qualifiers->{$_})) }
283 2366 grep { $qualifiers->{$_} } sort keys %{$qualifiers};
284  
285 2366 ▶ push @purl, ('?', join('&', @qualifiers)) if (@qualifiers);
Branch: 1 branch
BranchTrueFalseTotal
if @qualifiers 2060 306 2366
286  
287 }
288  
289 # Subpath
290 2366 ▶ if ($self->subpath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subpath) 47 2319 2366
291  
292 47 my $subpath = $self->subpath;
293  
294 47 $subpath =~ s{\.\./}{};
295 47 $subpath =~ s{\./}{};
296  
297 47 my @subpath = map { _encode($_) } split '/', $subpath;
298 47 push @purl, ('#', join('/', @subpath));
299  
300 }
301  
302 2366 return join '', @purl;
303  
304 }
305  
306 1 ▶sub to_hash {
Subroutine to_hash: called CC=1 SCAR=0.0
Pod documented
307  
308 1 my $self = shift;
309  
310 1 my %hash = map { $_ => $self->{components}->{$_} } qw[scheme type name version namespace qualifiers subpath];
311 1 return \%hash;
312  
313 }
314  
315 1 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
316  
317 34026 ▶sub _component {
Subroutine _component: called CC=2 SCAR=6.9
318  
319 34026 my ($self, $component, $value) = @_;
320  
321 34026 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) 927 33099 34026
322 927 $self->{components}->{$component} = $value;
323 }
324  
325 34026 return $self->{components}->{$component};
326  
327 }
328  
329 9187 ▶sub _url_encode {
Subroutine _url_encode: called CC=3 SCAR=11.3
330  
331 9187 my ($string, $pattern) = @_;
332  
333 # RFC-3986
334 9187 ▶ $pattern //= '^A-Za-z0-9\-._~/' unless $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern 9187 0 9187
Condition50%1/2
$pattern //= "^A-Za-z0-9\\-._~/"
l!l
09187
MC/DC0%0/1
$pattern //= "^A-Za-z0-9\\-._~/"
$pattern
Truth table50%1/2
A
A= $pattern
Aresult
11
00
335 9187 $string =~ s/([$pattern])/sprintf '%%%02X', ord $1/ge;
336 9187 return $string;
337  
338 }
339  
340 6983 ▶sub _encode {
Subroutine _encode: called CC=1 SCAR=0.0
341  
342 6983 my $string = shift;
343  
344 6983 $string = _url_encode($string);
345  
346 6983 $string =~ s{%3A}{:}g;
347 6983 $string =~ s{/}{%2F}g;
348  
349 6983 return $string;
350  
351 }
352  
353 1588 ▶sub _url_decode {
Subroutine _url_decode: called CC=2 SCAR=6.9
354  
355 1588 my $string = shift;
356 1588 ▶ return unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 9 1579 1588
357  
358 1579 $string =~ s/%([0-9a-fA-F]{2})/chr hex $1/ge;
359 1579 return $string;
360  
361 }
362  
363 9 1;
364  
365 __END__
« VersionRange/Util.pm ↑ Summary VersionRange.pm »