URI/_generic.pm

filter: Statementstmt 96.7% 180 / 186 Branchbran 86.6% 97 / 112 Conditioncond 83.3% 35 / 42 MC/DCmcdc 68.0% 17 / 25 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 90.6% 350 / 386 CC 76 SCAR 27.1
CC
76
Cov
92%
abs
26.1
rel
19.2
_check_path
14.1
CRAP 15.1
« 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.40';
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 365 ▶sub _no_scheme_ok {1}
Subroutine _no_scheme_ok: called CC=1 SCAR=0.0
19  
20 52 our $IPv6_re;
21  
22 367 ▶sub _looks_like_raw_ip6_address {
Subroutine _looks_like_raw_ip6_address: called CC=6 SCAR=18.3
23 367 my $addr = shift;
24  
25 367 ▶ if (!$IPv6_re) { #-- lazy / runs once / use Regexp::IPv6 if installed
Branch: 1 branch
BranchTrueFalseTotal
unless ($URI::_generic::IPv6_re) 26 341 367
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 367 ▶ return 0 unless $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr 0 367 367
35 367 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ tr/:// < 2 339 28 367
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 2691 ▶sub authority {
Subroutine authority: called CC=7 SCAR=19.5
Pod documented
44 2691 my $self = shift;
45 2691 ▶ $$self =~ m,^((?:$URI::scheme_re:)?)(?://([^/?\#]*))?(.*)$,os or die;
Branch: 1 branch
BranchTrueFalseTotal
$$self =~ m[^((?:$URI::scheme_re:)?)(?://([^/?\#]*))?(.*)$]so or die 2691 0 2691
46  
47 2691 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 478 2213 2691
48 478 my $auth = shift;
49 478 $$self = $1;
50 478 my $rest = $3;
51 478 ▶ if (defined $auth) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $auth) 397 81 478
52 397 $auth =~ s/([^$ACHAR])/ URI::Escape::escape_char($1)/ego;
53 397 ▶ if (my ($user, $host) = $auth =~ /^(.*@)?([^@]+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $auth =~ /^(.*@)?([^\@]+)$/) 367 30 397
54 { #-- special escape userinfo part
55 367 ▶ $user ||= '';
Condition100%2/2
$user ||= ""
l!l
69298
MC/DC100%1/1
$user ||= ""
$user
Truth table100%2/2
A
A= $user
Aresult
11
00
56 367 $user
57 76 =~ s/([^$URI::uric4user])/ URI::Escape::escape_char($1)/ego;
58 367 $user =~ s/%40$/\@/; # recover final '@'
59 367 ▶ $host = "[$host]" if _looks_like_raw_ip6_address($host);
Branch: 1 branch
BranchTrueFalseTotal
if _looks_like_raw_ip6_address($host) 2 365 367
60 367 $auth = $user . $host;
61 }
62 397 utf8::downgrade($auth);
63 397 $$self .= "//$auth";
64 }
65 478 _check_path($rest, $$self);
66 478 $$self .= $rest;
67 }
68 2691 $2;
69 }
70  
71 1505 ▶sub path {
Subroutine path: called CC=4 SCAR=13.9
Pod documented
72 1505 my $self = shift;
73 1505 ▶ $$self =~ m,^((?:[^:/?\#]+:)?(?://[^/?\#]*)?)([^?\#]*)(.*)$,s or die;
Branch: 1 branch
BranchTrueFalseTotal
$$self =~ m[^((?:[^:/?\#]+:)?(?://[^/?\#]*)?)([^?\#]*)(.*)$]s or die 1505 0 1505
74  
75 1505 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 402 1103 1505
76 402 $$self = $1;
77 402 my $rest = $3;
78 402 my $new_path = shift;
79 402 ▶ $new_path = "" unless defined $new_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_path 2 400 402
80 402 $new_path =~ s/([^$PCHAR])/ URI::Escape::escape_char($1)/ego;
81 402 utf8::downgrade($new_path);
82 402 _check_path($new_path, $$self);
83 402 $$self .= $new_path . $rest;
84 }
85 1505 $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 883 ▶sub _check_path {
Subroutine _check_path: called CC=10 SCAR=26.5
106 883 my ($path, $pre) = @_;
107 883 my $prefix;
108 883 ▶ if ($pre =~ m,/,) { # authority present
Branch: 1 branch
BranchTrueFalseTotal
if ($pre =~ m[/]) { } 713 170 883
109 713 ▶ $prefix = "/" if length($path) && $path !~ m,^[/?\#],;
Branch: 1 branch
BranchTrueFalseTotal
if length $path and not $path =~ m[^[/?\#]] 222 491 713
Condition100%3/3
length $path and not $path =~ m[^[/?\#]]
!ll&&!rl&&r
125366222
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 170 ▶ if ($path =~ m,^//,) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ m[^//]) { } 0 170 170
elsif (not length $pre and $path =~ m[^[^:/?\#]+:]) { } 0 199 199
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
291410
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 883 ▶ substr($_[0], 0, 0) = $prefix if defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix 222 661 883
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 286 ▶sub abs {
Subroutine abs: called CC=26 SCAR=32.6
Pod documented
157 286 my $self = shift;
158 286 ▶ my $base = shift || Carp::croak("Missing base argument");
Condition50%1/2
shift() || Carp::croak('Missing base argument')
l!l
2860
MC/DC0%0/1
shift() || Carp::croak('Missing base argument')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
159  
160 286 ▶ if (my $scheme = $self->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scheme = $self->scheme) 29 257 286
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 264 ▶ $base = URI->new($base) unless ref $base;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 117 147 264
167 264 my $abs = $self->clone;
168 264 $abs->scheme($base->scheme);
169 264 ▶ return $abs if $$self =~ m,^(?:$URI::scheme_re:)?//,o;
Branch: 1 branch
BranchTrueFalseTotal
if $$self =~ m[^(?:$URI::scheme_re:)?//]o 11 253 264
170 253 $abs->authority($base->authority);
171  
172 253 my $path = $self->path;
173 253 ▶ return $abs if $path =~ m,^/,;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 19 234 253
174  
175 234 ▶ if (!length($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $path) 27 207 234
176 27 my $abs = $base->clone;
177 27 my $query = $self->query;
178 27 ▶ $abs->query($query) if defined $query;
Branch: 1 branch
BranchTrueFalseTotal
if defined $query 9 18 27
179 27 my $fragment = $self->fragment;
180 27 ▶ $abs->fragment($fragment) if defined $fragment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fragment 15 12 27
181 27 return $abs;
182 }
183  
184 207 my $p = $base->path;
185 207 $p =~ s,[^/]+$,,;
186 207 $p .= $path;
187 207 my @p = split('/', $p, -1);
188 207 ▶ shift(@p) if @p && !length($p[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @p and not length $p[0] 204 3 207
Condition66%2/3
@p and not length $p[0]
!ll&&!rl&&r
03204
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 207 my $i = 1;
190 207 ▶ while ($i < @p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @p) 664 207 871
191  
192 #print "$i ", join("/", @p), " ($p[$i])\n";
193 664 ▶ if ($p[$i - 1] eq ".") {
Branch: 2 branches
BranchTrueFalseTotal
if ($p[$i - 1] eq '.') { } 35 629 664
elsif ($p[$i] eq '..' and $p[$i - 1] ne '..') { } 108 521 629
194 35 splice(@p, $i - 1, 1);
195 35 ▶ $i-- if $i > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 1 34 1 35
196 }
197 ▶ elsif ($p[$i] eq ".." && $p[$i - 1] ne "..") {
Condition100%3/3
$p[$i] eq ".." and $p[$i - 1] ne ".."
!ll&&!rl&&r
51011108
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 108 splice(@p, $i - 1, 2);
199 108 ▶ if ($i > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 78 30 108
200 78 $i--;
201 78 ▶ push(@p, "") if $i == @p;
Branch: 1 branch
BranchTrueFalseTotal
if $i == @p 4 74 78
202 }
203 }
204 else {
205 521 $i++;
206 }
207 }
208 207 ▶ $p[-1] = "" if @p && $p[-1] eq "."; # trailing "/."
Branch: 1 branch
BranchTrueFalseTotal
if @p and $p[-1] eq "." 8 199 207
Condition100%3/3
@p and $p[-1] eq "."
!ll&&!rl&&r
41958
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 207 ▶ if ($URI::ABS_REMOTE_LEADING_DOTS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($URI::ABS_REMOTE_LEADING_DOTS) 3 204 207
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 207 $abs->path("/" . join("/", @p));
213 207 $abs;
214 }
215  
216 # The opposite of $url->abs. Return a URI which is as relative as possible
217 73 ▶sub rel {
Subroutine rel: called CC=19 SCAR=29.5
Pod documented
218 73 my $self = shift;
219 73 ▶ my $base = shift || Carp::croak("Missing base argument");
Condition50%1/2
shift() || Carp::croak('Missing base argument')
l!l
730
MC/DC0%0/1
shift() || Carp::croak('Missing base argument')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
220 73 my $rel = $self->clone;
221 73 ▶ $base = URI->new($base) unless ref $base;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 73 0 73
222  
223 #my($scheme, $auth, $path) = @{$rel}{qw(scheme authority path)};
224 73 my $scheme = $rel->scheme;
225 73 my $auth = $rel->canonical->authority;
226 73 my $path = $rel->path;
227  
228 73 ▶ if (!defined($scheme) && !defined($auth)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scheme or defined $auth) 0 73 73
Condition33%1/3
defined $scheme or defined $auth
l!l&&r!l&&!r
7300
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 73 my $bscheme = $base->scheme;
236 73 my $bauth = $base->canonical->authority;
237 73 my $bpath = $base->path;
238  
239 73 for ($bscheme, $bauth, $auth) {
240 219 ▶ $_ = '' unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 17 202 219
241 }
242  
243 73 ▶ unless ($scheme eq $bscheme && $auth eq $bauth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme eq $bscheme and $auth eq $bauth) 5 68 73
Condition100%3/3
$scheme eq $bscheme and $auth eq $bauth
!ll&&!rl&&r
2368
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 68 ▶ for ($path, $bpath) { $_ = "/$_" unless m,^/,; }
Branch: 1 branch
BranchTrueFalseTotal
unless m[^/] 2 134 136
250  
251 # Make it relative by eliminating scheme and authority
252 # (empty the path first so a leading "//" can't be reparsed as authority)
253 68 $rel->path(q{});
254 68 $rel->scheme(undef);
255 68 $rel->authority(undef);
256  
257 # This loop is based on code from Nicolai Langfeldt <janl@ifi.uio.no>.
258 # First we calculate common initial path components length ($li).
259 68 my $li = 1;
260 68 while (1) {
261 148 my $i = index($path, '/', $li);
262 last
263 148 ▶ 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) 68 80 148
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
65380
$i < 0 or $i != index($bpath, "/", $li)
l!l&&r!l&&!r
501583
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
264 || $i != index($bpath, '/', $li)
265 || substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li);
266 80 $li = $i + 1;
267 }
268  
269 # then we nuke it from both paths
270 68 substr($path, 0, $li) = '';
271 68 substr($bpath, 0, $li) = '';
272  
273 68 ▶ 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) { } 2 66 68
Condition100%6/6
$path eq $bpath and defined $rel->fragment and not defined $rel->query
!ll&&!rl&&r
6332
$path eq $bpath and defined $rel->fragment
!ll&&!rl&&r
5675
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
274 2 $rel->path("");
275 }
276 else {
277 # Add one "../" for each path component left in the base path
278 66 $path = ('../' x $bpath =~ tr{/}{/}) . $path;
279 66 ▶ $path = "./" if $path eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 6 60 66
280  
281 # leading "/" reads as an absolute path, "//" as an authority
282 66 ▶ $path = "./$path" if $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 3 63 66
283 66 $rel->path($path);
284 }
285  
286 68 $rel;
287 }
288  
289 52 1;
« URI/file.pm ↑ Summary URI/_query.pm »