Login/URI.pm

filter: Statementstmt 82.7% 67 / 81 Branchbran 50.0% 17 / 34 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 70.5% 96 / 136 CC 20 SCAR 30.2
CC
20
Cov
71%
_remove_dot_segments
37.0
normalize
6.5
_encode
6.0
CRAP 20.5
↑ Summary Login.pm »
1 package OpenID::Login::URI;
2 {
3 4 $OpenID::Login::URI::VERSION = '0.1.2';
4 }
5  
6 # ABSTRACT: OpenID Identifier validation and encoding for OpenID::Login.
7  
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use List::MoreUtils qw(none any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶sub _build_url_regexp {
Subroutine _build_url_regexp: called CC=1 SCAR=0.0
14 4 my $class = shift;
15 4 my $digit = q{[0-9]};
16 4 my $upalpha = q{[A-Z]};
17 4 my $lowalpha = q{[a-z]};
18 4 my $alpha = qq{(?:$lowalpha|$upalpha)};
19 4 my $alphanum = qq{(?:$alpha|$digit)};
20 4 my $hex = qq{(?:$digit|[A-Fa-f])};
21 4 my $escaped = qq{%$hex$hex};
22 4 my $mark = q{[-_.!~*'()]};
23 4 my $unreserved = qq{(?:$alphanum|$mark)};
24 4 my $reserved = q{[;/?:@&=+$,]};
25 4 my $uric = qq{(?:$reserved|$unreserved|$escaped)};
26 4 my $query = qq{$uric*};
27 4 my $pchar = qq{(?:$unreserved|$escaped|} . q{[:@&=+$,])};
28 4 my $param = qq{$pchar*};
29 4 my $segment = qq{$pchar*(?:;$param)*};
30 4 my $path_segments = qq{$segment(?:/$segment)*};
31 4 my $abs_path = qq{/$path_segments};
32 4 my $port = qq{$digit*};
33 4 my $IPv4address = qq{$digit+\\.$digit+\\.$digit+\\.$digit+};
34 4 my $toplabel = qq{(?:$alpha|$alpha(?:$alphanum|-)*$alphanum)};
35 4 my $domainlabel = qq{(?:$alphanum|$alphanum(?:$alphanum|-)*$alphanum)};
36 4 my $hostname = qq{(?:$domainlabel\\.)*$toplabel\\.?};
37 4 my $host = qq{(?:$hostname|$IPv4address)};
38 4 my $fragment = qq{$uric*};
39 4 my $pattern = qq{https?://$host(?::$port)?(?:$abs_path(?:\\?$query)?)?(?:#$fragment)?};
40 4 return $pattern;
41 }
42  
43 4 my $REGEX = __PACKAGE__->_build_url_regexp();
44  
45  
46 12 ▶sub is_uri {
Subroutine is_uri: called CC=1 SCAR=0.0
Pod documented
47 12 my $class = shift;
48 12 my $uri = shift;
49 12 return $uri =~ /^$REGEX$/o;
50 }
51  
52  
53 12 ▶sub normalize {
Subroutine normalize: called CC=6 SCAR=18.7
Pod documented
54 12 my $class = shift;
55 12 my $uri = shift;
56  
57 12 my $u = URI->new($uri);
58 12 ▶ return unless $u->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $u->scheme 0 12 12
59 12 ▶ return if ( none { $_ eq $u->scheme } qw(http https) );
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $u->scheme; } , "http", "https") 0 12 12
60 12 ▶ return unless $u->can('host') && $u->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $u->can("host") and $u->host 0 12 12
Condition33%1/3
$u->can("host") and $u->host
!ll&&!rl&&r
0012
MC/DC0%0/2
$u->can("host") and $u->host
$u->can("host") $u->host
Truth table33%1/3
A and B
A= $u->can("host")B= $u->host
ABresult
0X0
100
111
61  
62 12 my $path = $class->_remove_dot_segments( $u->path );
63 12 ▶ $path = '/' if length($path) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $path == 0 5 7 12
64 12 $u->path($path);
65  
66 12 my $u_str = $u->canonical->as_string;
67 0 $u_str =~ s/(%[a-fA-F0-9]{2})/uc $class->_encode($1)/eg;
68 12 return $u_str;
69 }
70  
71 0 ▶sub _encode {
Subroutine _encode: not called CC=2 SCAR=17.9
72 0 my ( $class, $u ) = @_;
73 0 my $num = substr( $u, 1 );
74 0 my $packed = pack( 'H*', $num );
75 0 ▶ return $packed =~ /[A-Za-z0-9._~-]/ ? $packed : $u;
Branch: 1 branch
BranchTrueFalseTotal
$packed =~ /[A-Za-z0-9._~-]/ ? : 0 0 0
76 }
77  
78 12 ▶sub _remove_dot_segments {
Subroutine _remove_dot_segments: called CC=14 SCAR=36.1
79 12 my ( $class, $path ) = @_;
80 12 my @result_segments;
81 12 ▶ while ( length($path) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $path > 0) 8 12 20
82 8 ▶ if ( $path =~ m!^\.\./! ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($path =~ m[^\.\./]) { } 0 8 8
elsif ($path =~ m[^\./]) { } 0 8 8
elsif ($path =~ m[^/\./]) { } 0 8 8
elsif ($path eq '/.') { } 0 8 8
elsif ($path =~ m[^/\.\./]) { } 0 8 8
elsif ($path eq '/..') { } 0 8 8
elsif ($path eq '..' or $path eq '.') { } 0 8 8
83 0 $path = substr( $path, 3 );
84 } elsif ( $path =~ m!^\./! ) {
85 0 $path = substr( $path, 2 );
86 } elsif ( $path =~ m!^/\./! ) {
87 0 $path = substr( $path, 2 );
88 } elsif ( $path eq q{/.} ) {
89 0 $path = q{/};
90 } elsif ( $path =~ m!^/\.\./! ) {
91 0 $path = substr( $path, 3 );
92 0 ▶ pop(@result_segments) if @result_segments > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @result_segments > 0 0 0 0
93 } elsif ( $path eq q{/..} ) {
94 0 $path = q{/};
95 0 ▶ pop(@result_segments) if @result_segments > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @result_segments > 0 0 0 0
96 ▶ } elsif ( $path eq q{..} || $path eq q{.} ) {
Condition33%1/3
$path eq ".." or $path eq "."
l!l&&r!l&&!r
008
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
97 0 $path = q{};
98 } else {
99 8 my $i = 0;
100 8 ▶ $i = 1 if substr( $path, 0, 1 ) eq q{/};
Branch: 1 branch
BranchTrueFalseTotal
if substr($path, 0, 1) eq "/" 8 0 8
101 8 $i = index( $path, q{/}, $i );
102 8 ▶ $i = length($path) unless $i >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $i >= 0 7 1 8
103 8 push( @result_segments, substr( $path, 0, $i ) );
104 8 $path = substr( $path, $i );
105 }
106 }
107 12 return join( '', @result_segments );
108 }
109  
110 4 1;
111  
112  
113  
114 =pod
115  
116 =head1 NAME
117  
118 OpenID::Login::URI - OpenID Identifier validation and encoding for OpenID::Login.
119  
120 =head1 VERSION
121  
122 version 0.1.2
123  
124 =head1 METHODS
125  
126 =head2 is_uri
127  
128 Determines if supplied parameter is an uri.
129  
130 =head2 normalize
131  
132 Normalizes and encodes an supplied uri if necessary.
133  
134 =head1 AUTHOR
135  
136 Holger Eiboeck <realholgi@cpan.org>
137  
138 =head1 COPYRIGHT AND LICENSE
139  
140 This software is copyright (c) 2013 by Holger Eiboeck.
141  
142 This is free software; you can redistribute it and/or modify it under
143 the same terms as the Perl 5 programming language system itself.
144  
145 =cut
146  
147  
148 __END__
↑ Summary Login.pm »