URI/_generic.pm

filter: Statementstmt 96.7% 178 / 184 Branchbran 85.4% 94 / 110 Conditioncond 79.5% 35 / 44 MC/DCmcdc 62.9% 17 / 27 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 89.3% 345 / 386 CC 75 SCAR 27.0
CC
75
Cov
91%
abs
26.2
rel
18.3
_check_path
14.1
CRAP 14.9
« URI/file.pm ↑ Summary URI/_query.pm »
1 package URI::_generic;
2  
3 52 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 52 ▶use parent qw(URI URI::_query);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 52 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 52 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 52 our $VERSION = '5.38';
12  
13 52 my $ACHAR = URI::HAS_RESERVED_SQUARE_BRACKETS ? $URI::uric : $URI::uric4host;
14 52 $ACHAR =~ s,\\[/?],,g;
15 52 my $PCHAR = $URI::uric;
16 52 $PCHAR =~ s,\\[?],,g;
17  
18 359 ▶sub _no_scheme_ok {1}
Subroutine _no_scheme_ok: called CC=1 SCAR=0.0
19  
20 52 our $IPv6_re;
21  
22 361 ▶sub _looks_like_raw_ip6_address {
Subroutine _looks_like_raw_ip6_address: called CC=6 SCAR=18.3
23 361 my $addr = shift;
24  
25 361 ▶ if (!$IPv6_re) { #-- lazy / runs once / use Regexp::IPv6 if installed
Branch: 1 branch
BranchTrueFalseTotal
unless ($URI::_generic::IPv6_re) 26 335 361
26 eval {
27 26 require Regexp::IPv6;
28 0 Regexp::IPv6->import(qw($IPv6_re));
29 0 1;
30 }
31 26 ▶ || do { $IPv6_re = qr/[:0-9a-f]{3,}/; }; #-- fallback: unambitious guess
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Regexp::IPv6; "Regexp::IPv6"->import("\$IPv6_re"); 1 } } or do { $URI::_generic::IPv6_re = qr/[:0-9a-f]{3,}/ } 0 26 26
32 }
33  
34 361 ▶ return 0 unless $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr 0 361 361
35 361 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ tr/:// < 2 333 28 361
36 if $addr =~ tr/:/:/
37 < 2; #-- fallback must not create false positive for IPv4:Port = 0:0
38 28 ▶ return 1 if $addr =~ /^$IPv6_re$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /^$URI::_generic::IPv6_re$/i 2 26 28
39 26 return 0;
40 }
41  
42  
43 2625 ▶sub authority {
Subroutine authority: called CC=7 SCAR=19.5
Pod documented
44 2625 my $self = shift;
45 2625 ▶ $$self =~ m,^((?:$URI::scheme_re:)?)(?://([^/?\#]*))?(.*)$,os or die;
Branch: 1 branch
BranchTrueFalseTotal
$$self =~ m[^((?:$URI::scheme_re:)?)(?://([^/?\#]*))?(.*)$]so or die 2625 0 2625
46  
47 2625 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 466 2159 2625
48 466 my $auth = shift;
49 466 $$self = $1;
50 466 my $rest = $3;
51 466 ▶ if (defined $auth) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $auth) 391 75 466
52 391 $auth =~ s/([^$ACHAR])/ URI::Escape::escape_char($1)/ego;
53 391 ▶ if (my ($user, $host) = $auth =~ /^(.*@)?([^@]+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $auth =~ /^(.*@)?([^\@]+)$/) 361 30 391
54 { #-- special escape userinfo part
55 361 ▶ $user ||= '';
Condition100%2/2
$user ||= ""
l!l
69292
MC/DC100%1/1
$user ||= ""
$user
Truth table100%2/2
A
A= $user
Aresult
11
00
56 361 $user
57 76 =~ s/([^$URI::uric4user])/ URI::Escape::escape_char($1)/ego;
58 361 $user =~ s/%40$/\@/; # recover final '@'
59 361 ▶ $host = "[$host]" if _looks_like_raw_ip6_address($host);
Branch: 1 branch
BranchTrueFalseTotal
if _looks_like_raw_ip6_address($host) 2 359 361
60 361 $auth = $user . $host;
61 }
62 391 utf8::downgrade($auth);
63 391 $$self .= "//$auth";
64 }
65 466 _check_path($rest, $$self);
66 466 $$self .= $rest;
67 }
68 2625 $2;
69 }
70  
71 1391 ▶sub path {
Subroutine path: called CC=4 SCAR=13.9
Pod documented
72 1391 my $self = shift;
73 1391 ▶ $$self =~ m,^((?:[^:/?\#]+:)?(?://[^/?\#]*)?)([^?\#]*)(.*)$,s or die;
Branch: 1 branch
BranchTrueFalseTotal
$$self =~ m[^((?:[^:/?\#]+:)?(?://[^/?\#]*)?)([^?\#]*)(.*)$]s or die 1391 0 1391
74  
75 1391 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 323 1068 1391
76 323 $$self = $1;
77 323 my $rest = $3;
78 323 my $new_path = shift;
79 323 ▶ $new_path = "" unless defined $new_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_path 2 321 323
80 323 $new_path =~ s/([^$PCHAR])/ URI::Escape::escape_char($1)/ego;
81 323 utf8::downgrade($new_path);
82 323 _check_path($new_path, $$self);
83 323 $$self .= $new_path . $rest;
84 }
85 1391 $2;
86 }
87  
88 58 ▶sub path_query {
Subroutine path_query: called CC=4 SCAR=13.9
Pod documented
89 58 my $self = shift;
90 58 ▶ $$self =~ m,^((?:[^:/?\#]+:)?(?://[^/?\#]*)?)([^\#]*)(.*)$,s or die;
Branch: 1 branch
BranchTrueFalseTotal
$$self =~ m[^((?:[^:/?\#]+:)?(?://[^/?\#]*)?)([^\#]*)(.*)$]s or die 58 0 58
91  
92 58 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 55 58
93 3 $$self = $1;
94 3 my $rest = $3;
95 3 my $new_path = shift;
96 3 ▶ $new_path = "" unless defined $new_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_path 0 3 3
97 3 $new_path =~ s/([^$URI::uric])/ URI::Escape::escape_char($1)/ego;
98 3 utf8::downgrade($new_path);
99 3 _check_path($new_path, $$self);
100 3 $$self .= $new_path . $rest;
101 }
102 58 $2;
103 }
104  
105 792 ▶sub _check_path {
Subroutine _check_path: called CC=10 SCAR=26.5
106 792 my ($path, $pre) = @_;
107 792 my $prefix;
108 792 ▶ if ($pre =~ m,/,) { # authority present
Branch: 1 branch
BranchTrueFalseTotal
if ($pre =~ m[/]) { } 640 152 792
109 640 ▶ $prefix = "/" if length($path) && $path !~ m,^[/?\#],;
Branch: 1 branch
BranchTrueFalseTotal
if length $path and not $path =~ m[^[/?\#]] 217 423 640
Condition100%3/3
length $path and not $path =~ m[^[/?\#]]
!ll&&!rl&&r
63360217
MC/DC100%2/2
length $path and not $path =~ m[^[/?\#]]
length $path not $path =~ m[^[/?\#]]
Truth table100%3/3
A and not(B)
A= length $pathB= not $path =~ m[^[/?\#]]
ABresult
0X0
100
111
110 }
111 else {
112 152 ▶ if ($path =~ m,^//,) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ m[^//]) { } 0 152 152
elsif (not length $pre and $path =~ m[^[^:/?\#]+:]) { } 0 175 175
113 0 ▶ Carp::carp("Path starting with double slash is confusing") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
114 }
115 ▶ elsif (!length($pre) && $path =~ m,^[^:/?\#]+:,) {
Condition66%2/3
not length $pre and $path =~ m[^[^:/?\#]+:]
!ll&&!rl&&r
231290
MC/DC0%0/2
not length $pre and $path =~ m[^[^:/?\#]+:]
not length $pre $path =~ m[^[^:/?\#]+:]
Truth table66%2/3
not(A) and B
A= not length $preB= $path =~ m[^[^:/?\#]+:]
ABresult
0X0
100
111
116 0 ▶ Carp::carp("Path might look like scheme, './' prepended") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
117 0 $prefix = "./";
118 }
119 }
120 792 ▶ substr($_[0], 0, 0) = $prefix if defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix 217 575 792
121 }
122  
123 97 ▶sub path_segments {
Subroutine path_segments: called CC=7 SCAR=19.5
Pod documented
124 97 my $self = shift;
125 97 my $path = $self->path;
126 97 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 91 97
127 6 my @arg = @_; # make a copy
128 6 for (@arg) {
129 21 ▶ if (ref($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 1 20 21
130 1 my @seg = @$_;
131 1 $seg[0] =~ s/%/%25/g;
132 1 for (@seg) { s/;/%3B/g; }
133 1 $_ = join(";", @seg);
134 }
135 else {
136 20 s/%/%25/g;
137 20 s/;/%3B/g;
138 }
139 21 s,/,%2F,g;
140 }
141 6 $self->path(join("/", @arg));
142 }
143 97 ▶ return $path unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 9 88 97
144 88 ▶ map { /;/ ? $self->_split_segment($_) : uri_unescape($_) }
Branch: 1 branch
BranchTrueFalseTotal
/;/ ? : 4 236 240
145 split('/', $path, -1);
146 }
147  
148  
149 4 ▶sub _split_segment {
Subroutine _split_segment: called CC=1 SCAR=0.0
150 4 my $self = shift;
151 4 require URI::_segment;
152 4 URI::_segment->new(@_);
153 }
154  
155  
156 280 ▶sub abs {
Subroutine abs: called CC=26 SCAR=32.6
Pod documented
157 280 my $self = shift;
158 280 ▶ my $base = shift || Carp::croak("Missing base argument");
Condition33%1/3
shift() || Carp::croak('Missing base argument')
l!l&&r!l&&!r
28000
MC/DC0%0/2
shift() || Carp::croak('Missing base argument')
shift() Carp::croak('Missing base argument')
Truth table33%1/3
A || B
A= shift()B= Carp::croak('Missing base argument')
ABresult
1X1
011
000
159  
160 280 ▶ if (my $scheme = $self->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scheme = $self->scheme) 29 251 280
161 29 ▶ return $self unless $URI::ABS_ALLOW_RELATIVE_SCHEME;
Branch: 1 branch
BranchTrueFalseTotal
unless $URI::ABS_ALLOW_RELATIVE_SCHEME 19 10 29
162 10 ▶ $base = URI->new($base) unless ref $base;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 10 0 10
163 10 ▶ return $self unless $scheme eq $base->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme eq $base->scheme 3 7 10
164 }
165  
166 258 ▶ $base = URI->new($base) unless ref $base;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 111 147 258
167 258 my $abs = $self->clone;
168 258 $abs->scheme($base->scheme);
169 258 ▶ return $abs if $$self =~ m,^(?:$URI::scheme_re:)?//,o;
Branch: 1 branch
BranchTrueFalseTotal
if $$self =~ m[^(?:$URI::scheme_re:)?//]o 11 247 258
170 247 $abs->authority($base->authority);
171  
172 247 my $path = $self->path;
173 247 ▶ return $abs if $path =~ m,^/,;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 19 228 247
174  
175 228 ▶ if (!length($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $path) 26 202 228
176 26 my $abs = $base->clone;
177 26 my $query = $self->query;
178 26 ▶ $abs->query($query) if defined $query;
Branch: 1 branch
BranchTrueFalseTotal
if defined $query 9 17 26
179 26 my $fragment = $self->fragment;
180 26 ▶ $abs->fragment($fragment) if defined $fragment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fragment 14 12 26
181 26 return $abs;
182 }
183  
184 202 my $p = $base->path;
185 202 $p =~ s,[^/]+$,,;
186 202 $p .= $path;
187 202 my @p = split('/', $p, -1);
188 202 ▶ shift(@p) if @p && !length($p[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @p and not length $p[0] 199 3 202
Condition66%2/3
@p and not length $p[0]
!ll&&!rl&&r
03199
MC/DC50%1/2
@p and not length $p[0]
@p not length $p[0]
Truth table66%2/3
A and not(B)
A= @pB= not length $p[0]
ABresult
0X0
100
111
189 202 my $i = 1;
190 202 ▶ while ($i < @p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @p) 646 202 848
191  
192 #print "$i ", join("/", @p), " ($p[$i])\n";
193 646 ▶ if ($p[$i - 1] eq ".") {
Branch: 2 branches
BranchTrueFalseTotal
if ($p[$i - 1] eq '.') { } 32 614 646
elsif ($p[$i] eq '..' and $p[$i - 1] ne '..') { } 107 507 614
194 32 splice(@p, $i - 1, 1);
195 32 ▶ $i-- if $i > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 1 32 0 32
196 }
197 ▶ elsif ($p[$i] eq ".." && $p[$i - 1] ne "..") {
Condition100%3/3
$p[$i] eq ".." and $p[$i - 1] ne ".."
!ll&&!rl&&r
49611107
MC/DC100%2/2
$p[$i] eq ".." and $p[$i - 1] ne ".."
$p[$i] eq ".." $p[$i - 1] ne ".."
Truth table100%3/3
A and B
A= $p[$i] eq ".."B= $p[$i - 1] ne ".."
ABresult
0X0
100
111
198 107 splice(@p, $i - 1, 2);
199 107 ▶ if ($i > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 77 30 107
200 77 $i--;
201 77 ▶ push(@p, "") if $i == @p;
Branch: 1 branch
BranchTrueFalseTotal
if $i == @p 4 73 77
202 }
203 }
204 else {
205 507 $i++;
206 }
207 }
208 202 ▶ $p[-1] = "" if @p && $p[-1] eq "."; # trailing "/."
Branch: 1 branch
BranchTrueFalseTotal
if @p and $p[-1] eq "." 8 194 202
Condition100%3/3
@p and $p[-1] eq "."
!ll&&!rl&&r
41908
MC/DC100%2/2
@p and $p[-1] eq "."
@p $p[-1] eq "."
Truth table100%3/3
A and B
A= @pB= $p[-1] eq "."
ABresult
0X0
100
111
209 202 ▶ if ($URI::ABS_REMOTE_LEADING_DOTS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($URI::ABS_REMOTE_LEADING_DOTS) 3 199 202
210 3 ▶ shift @p while @p && $p[0] =~ /^\.\.?$/;
Branch: 1 branch
BranchTrueFalseTotal
@p and $p[0] =~ /^\.\.?$/ and do { shift @p } 9 3 12
Condition66%2/3
@p and $p[0] =~ /^\.\.?$/
!ll&&!rl&&r
039
MC/DC50%1/2
@p and $p[0] =~ /^\.\.?$/
@p $p[0] =~ /^\.\.?$/
Truth table66%2/3
A and B
A= @pB= $p[0] =~ /^\.\.?$/
ABresult
0X0
100
111
211 }
212 202 $abs->path("/" . join("/", @p));
213 202 $abs;
214 }
215  
216 # The opposite of $url->abs. Return a URI which is as relative as possible
217 67 ▶sub rel {
Subroutine rel: called CC=18 SCAR=29.1
Pod documented
218 67 my $self = shift;
219 67 ▶ my $base = shift || Carp::croak("Missing base argument");
Condition33%1/3
shift() || Carp::croak('Missing base argument')
l!l&&r!l&&!r
6700
MC/DC0%0/2
shift() || Carp::croak('Missing base argument')
shift() Carp::croak('Missing base argument')
Truth table33%1/3
A || B
A= shift()B= Carp::croak('Missing base argument')
ABresult
1X1
011
000
220 67 my $rel = $self->clone;
221 67 ▶ $base = URI->new($base) unless ref $base;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 67 0 67
222  
223 #my($scheme, $auth, $path) = @{$rel}{qw(scheme authority path)};
224 67 my $scheme = $rel->scheme;
225 67 my $auth = $rel->canonical->authority;
226 67 my $path = $rel->path;
227  
228 67 ▶ if (!defined($scheme) && !defined($auth)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scheme or defined $auth) 0 67 67
Condition33%1/3
defined $scheme or defined $auth
l!l&&r!l&&!r
6700
MC/DC0%0/2
defined $scheme or defined $auth
defined $scheme defined $auth
Truth table33%1/3
A or B
A= defined $schemeB= defined $auth
ABresult
1X1
011
000
229  
230 # it is already relative
231 0 return $rel;
232 }
233  
234 #my($bscheme, $bauth, $bpath) = @{$base}{qw(scheme authority path)};
235 67 my $bscheme = $base->scheme;
236 67 my $bauth = $base->canonical->authority;
237 67 my $bpath = $base->path;
238  
239 67 for ($bscheme, $bauth, $auth) {
240 201 ▶ $_ = '' unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 17 184 201
241 }
242  
243 67 ▶ unless ($scheme eq $bscheme && $auth eq $bauth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme eq $bscheme and $auth eq $bauth) 5 62 67
Condition100%3/3
$scheme eq $bscheme and $auth eq $bauth
!ll&&!rl&&r
2362
MC/DC100%2/2
$scheme eq $bscheme and $auth eq $bauth
$scheme eq $bscheme $auth eq $bauth
Truth table100%3/3
A and B
A= $scheme eq $bschemeB= $auth eq $bauth
ABresult
0X0
100
111
244  
245 # different location, can't make it relative
246 5 return $rel;
247 }
248  
249 62 ▶ for ($path, $bpath) { $_ = "/$_" unless m,^/,; }
Branch: 1 branch
BranchTrueFalseTotal
unless m[^/] 2 122 124
250  
251 # Make it relative by eliminating scheme and authority
252 62 $rel->scheme(undef);
253 62 $rel->authority(undef);
254  
255 # This loop is based on code from Nicolai Langfeldt <janl@ifi.uio.no>.
256 # First we calculate common initial path components length ($li).
257 62 my $li = 1;
258 62 while (1) {
259 137 my $i = index($path, '/', $li);
260 last
261 137 ▶ if $i < 0
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 or $i != index($bpath, "/", $li) or substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li) 62 75 137
Condition100%6/6
$i < 0 or $i != index($bpath, "/", $li) or substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li)
l!l&&r!l&&!r
59375
$i < 0 or $i != index($bpath, "/", $li)
l!l&&r!l&&!r
481178
MC/DC100%3/3
$i < 0 or $i != index($bpath, "/", $li) or substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li)
$i < 0 $i != index($bpath, "/", $li) substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li)
Truth table100%4/4
A or B or C
A= $i < 0B= $i != index($bpath, "/", $li)C= substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li)
ABCresult
1XX1
01X1
0011
0000
262 || $i != index($bpath, '/', $li)
263 || substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li);
264 75 $li = $i + 1;
265 }
266  
267 # then we nuke it from both paths
268 62 substr($path, 0, $li) = '';
269 62 substr($bpath, 0, $li) = '';
270  
271 62 ▶ if ($path eq $bpath && defined($rel->fragment) && !defined($rel->query)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $bpath and defined $rel->fragment and not defined $rel->query) { } 1 61 62
Condition100%6/6
$path eq $bpath and defined $rel->fragment and not defined $rel->query
!ll&&!rl&&r
5831
$path eq $bpath and defined $rel->fragment
!ll&&!rl&&r
5174
MC/DC100%3/3
$path eq $bpath and defined $rel->fragment and not defined $rel->query
$path eq $bpath defined $rel->fragment not defined $rel->query
Truth table100%4/4
A and B and not(C)
A= $path eq $bpathB= defined $rel->fragmentC= not defined $rel->query
ABCresult
0XX0
10X0
1100
1111
272 1 $rel->path("");
273 }
274 else {
275 # Add one "../" for each path component left in the base path
276 61 $path = ('../' x $bpath =~ tr|/|/|) . $path;
277 61 ▶ $path = "./" if $path eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 6 55 61
278 61 $rel->path($path);
279 }
280  
281 62 $rel;
282 }
283  
284 52 1;
« URI/file.pm ↑ Summary URI/_query.pm »