Lite/Util/URI.pm

filter: Statementstmt 96.3% 79 / 82 Branchbran 82.3% 28 / 34 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 87.6% 121 / 138 CC 20 SCAR 22.3
CC
20
Cov
90%
_remove_dot_segments
15.9
normalize
6.0
_encode
2.0
CRAP 9.3
« Lite/Message/Decoder.pm ↑ Summary Lite/Message.pm »
1 package OpenID::Lite::Util::URI;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use List::MoreUtils qw(none any);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 8 ▶sub _build_url_regexp {
Subroutine _build_url_regexp: called CC=1 SCAR=0.0
11 8 my $class = shift;
12 8 my $digit = q{[0-9]};
13 8 my $upalpha = q{[A-Z]};
14 8 my $lowalpha = q{[a-z]};
15 8 my $alpha = qq{(?:$lowalpha|$upalpha)};
16 8 my $alphanum = qq{(?:$alpha|$digit)};
17 8 my $hex = qq{(?:$digit|[A-Fa-f])};
18 8 my $escaped = qq{%$hex$hex};
19 8 my $mark = q{[-_.!~*'()]};
20 8 my $unreserved = qq{(?:$alphanum|$mark)};
21 8 my $reserved = q{[;/?:@&=+$,]};
22 8 my $uric = qq{(?:$reserved|$unreserved|$escaped)};
23 8 my $query = qq{$uric*};
24 8 my $pchar = qq{(?:$unreserved|$escaped|} . q{[:@&=+$,])};
25 8 my $param = qq{$pchar*};
26 8 my $segment = qq{$pchar*(?:;$param)*};
27 8 my $path_segments = qq{$segment(?:/$segment)*};
28 8 my $abs_path = qq{/$path_segments};
29 8 my $port = qq{$digit*};
30 8 my $IPv4address = qq{$digit+\\.$digit+\\.$digit+\\.$digit+};
31 8 my $toplabel = qq{(?:$alpha|$alpha(?:$alphanum|-)*$alphanum)};
32 8 my $domainlabel = qq{(?:$alphanum|$alphanum(?:$alphanum|-)*$alphanum)};
33 8 my $hostname = qq{(?:$domainlabel\\.)*$toplabel\\.?};
34 8 my $host = qq{(?:$hostname|$IPv4address)};
35 8 my $fragment = qq{$uric*};
36 8 my $pattern = qq{https?://$host(?::$port)?(?:$abs_path(?:\\?$query)?)?(?:#$fragment)?};
37 8 return $pattern;
38 }
39  
40 8 my $REGEX = __PACKAGE__->_build_url_regexp();
41  
42 106 ▶sub is_uri {
Subroutine is_uri: called CC=1 SCAR=0.0
Pod undocumented
43 106 my $class = shift;
44 106 my $uri = shift;
45 106 return $uri =~ /^$REGEX$/o;
46 }
47  
48 133 ▶sub normalize {
Subroutine normalize: called CC=6 SCAR=17.9
Pod undocumented
49 133 my $class = shift;
50 133 my $uri = shift;
51 133 my $u = URI->new($uri);
52 133 ▶ return unless $u->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $u->scheme 6 127 133
53 127 ▶ return if ( none { $_ eq $u->scheme } qw(http https));
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $u->scheme; } , "http", "https") 3 124 127
54 124 ▶ return unless $u->can('host') && $u->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $u->can("host") and $u->host 1 123 124
Condition66%2/3
$u->can("host") and $u->host
!ll&&!rl&&r
01123
MC/DC50%1/2
$u->can("host") and $u->host
$u->can("host") $u->host
Truth table66%2/3
A and B
A= $u->can("host")B= $u->host
ABresult
0X0
100
111
55  
56 123 my $path = $class->_remove_dot_segments($u->path);
57 123 ▶ $path = '/' if length($path) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $path == 0 12 111 123
58 123 $u->path($path);
59  
60 123 my $u_str = $u->canonical->as_string;
61 123 $u_str =~ s/(%[a-fA-F0-9]{2})/uc $class->_encode($1)/eg;
62 123 return $u_str;
63 }
64  
65 6 ▶sub _encode {
Subroutine _encode: called CC=2 SCAR=7.0
66 6 my ( $class, $u ) = @_;
67 6 my $num = substr($u, 1);
68 6 my $packed = pack('H*', $num);
69 6 ▶ return $packed =~ /[A-Za-z0-9._~-]/ ? $packed : $u;
Branch: 1 branch
BranchTrueFalseTotal
$packed =~ /[A-Za-z0-9._~-]/ ? : 0 6 6
70 }
71  
72 123 ▶sub _remove_dot_segments {
Subroutine _remove_dot_segments: called CC=14 SCAR=27.7
73 123 my ( $class, $path ) = @_;
74 123 my @result_segments;
75 123 ▶ while ( length($path) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $path > 0) 146 123 269
76 146 ▶ if ($path =~ m!^\.\./!) {
Branch: 7 branches
BranchTrueFalseTotal
if ($path =~ m[^\.\./]) { } 0 146 146
elsif ($path =~ m[^\./]) { } 0 146 146
elsif ($path =~ m[^/\./]) { } 4 142 146
elsif ($path eq '/.') { } 1 141 142
elsif ($path =~ m[^/\.\./]) { } 7 134 141
elsif ($path eq '/..') { } 1 133 134
elsif ($path eq '..' or $path eq '.') { } 0 133 133
77 0 $path = substr($path, 3);
78 } elsif ($path =~ m!^\./!) {
79 0 $path = substr($path, 2);
80 } elsif ($path =~ m!^/\./!) {
81 4 $path = substr($path, 2);
82 } elsif ($path eq q{/.}) {
83 1 $path = q{/};
84 } elsif ($path =~ m!^/\.\./!) {
85 7 $path = substr($path, 3);
86 7 ▶ pop(@result_segments) if @result_segments > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @result_segments > 0 6 1 7
87 } elsif ($path eq q{/..}) {
88 1 $path = q{/};
89 1 ▶ pop(@result_segments) if @result_segments > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @result_segments > 0 1 0 1
90 ▶ } elsif ($path eq q{..} || $path eq q{.}) {
Condition33%1/3
$path eq ".." or $path eq "."
l!l&&r!l&&!r
00133
MC/DC0%0/2
$path eq ".." or $path eq "."
$path eq ".." $path eq "."
Truth table33%1/3
A or B
A= $path eq ".."B= $path eq "."
ABresult
1X1
011
000
91 0 $path = q{};
92 } else {
93 133 my $i = 0;
94 133 ▶ $i = 1 if substr($path, 0, 1) eq q{/};
Branch: 1 branch
BranchTrueFalseTotal
if substr($path, 0, 1) eq "/" 133 0 133
95 133 $i = index($path, q{/}, $i);
96 133 ▶ $i = length($path) unless $i >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $i >= 0 111 22 133
97 133 push(@result_segments, substr($path, 0, $i));
98 133 $path = substr($path, $i);
99 }
100 }
101 123 return join('', @result_segments);
102 }
103  
104 8 1;
« Lite/Message/Decoder.pm ↑ Summary Lite/Message.pm »