| 1 | package Net::Google::SafeBrowsing4::URI; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Digest::SHA qw(sha256); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Net::IP::Lite qw(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | Net::Google::SafeBrowsing4::URI - URI management Class for the Google SafeBrowsing (version 4) API. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | use Net::Google::SafeBrowsing4::URI; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | my $gsb_uri = Net::Google::SafeBrowsing4::URI->new('http://my.example.site:80/path/to/file.html?query=param#fragment'); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | my @uris = $gsb_uri->generate_lookupuris(); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | Net::Google::SafeBrowsing4::URI takes care of normalizing URLs, extracting suffix/prefix expressions, calculating hashes. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =item new | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | my $gsb_uri = Net::Google::SafeBrowsing4::URI->new('http://my.example.site:80/path/to/file.html?query=param#fragment'); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | Initializes the object. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =item $uri The URL to parse | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 262 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 262 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 262 | ▶ | if ((scalar(@args) == 0) || !$args[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @args == 0 or not $args[0]
MC/DC100%2/2
scalar @args == 0 or not $args[0]
scalar @args == 0
not $args[0]
Truth table100%3/3
A or not(B)
A= scalar @args == 0B= not $args[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 259 | my $self = { | |||||||||||||||||||||||||||||||||||||||||
| 63 | rawuri => $args[0], | ||||||||||||||||||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 259 | bless($self, $class); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 259 | ▶ | return $self->_normalize() ? $self : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =item as_string | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | Returns the normalized URI as string. | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | sub as_string { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 432 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 432 | return '' . $self->{uri}; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =item generate_lookupuris | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | Generates all partial/full URIs supported by Google SafeBrowsing. See "suffix/prefix expressions" topic in GSBv4 API reference. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | Returns a list of L<Net::Google::SafeBrowsing4::URI> objects. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | sub generate_lookupuris { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_lookupuris: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 8 | my @uris = (); | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 8 | $self->as_string() =~ /^(https?:\/\/)([^\/]+)(\/.*)$/i; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 8 | my ($scheme, $host, $path_query) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # Collect host suffixes | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 8 | my @domains = (); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | ▶ | if ($host !~ /^\d+\.\d+\.\d+\.\d+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | my @parts = split(/\./, $host); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | splice(@parts, 0, -6); # take 5 top most compoments | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | ▶ | while (scalar(@parts) > 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 9 | shift(@parts); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | push(@domains, join(".", @parts) ); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 8 | push(@domains, $host); | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # Collect path & query prefixes | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 8 | my @paths = (); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 8 | my @parts = split(/\//, $path_query); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 8 | my $part_count = scalar(@parts); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 8 | ▶ | $part_count = $part_count > 4 ? 4 : $part_count - 1; # limit to 4 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 8 | my $previous = ""; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 8 | ▶ | for (my $i = 0; $i < $part_count; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 8 | $previous .= "/" . $parts[$i] ."/"; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 8 | push(@paths, $previous); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 8 | ▶ | if ($path_query =~ /^([^\?]+)\?.*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | push(@paths, $1); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 8 | push(@paths, $path_query); | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # Assemble the list of Net::Google::SafeBrowsing4::URI objects | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 8 | foreach my $domain (@domains) { | |||||||||||||||||||||||||||||||||||||||||
| 130 | 17 | foreach my $path (@paths) { | |||||||||||||||||||||||||||||||||||||||||
| 131 | 33 | my $gsb_uri = Net::Google::SafeBrowsing4::URI->new($scheme . $domain . $path); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 33 | push(@uris, $gsb_uri); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 8 | return @uris; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =item hash | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | Generates the SHA-256 hash of the URI (with scheme removed). | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | sub hash { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | my $uri = $self->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 149 | 2 | $uri =~ s/^https?:\/\///i; | |||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 2 | return sha256($uri); | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =item _normalize | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | Parses and normalizes the URI. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | sub _normalize { | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 259 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 259 | my $modified_rawuri = $self->{rawuri}; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | # Remove third and more slashes after the scheme | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 259 | $modified_rawuri =~ s/^(\s*https?:\/\/)\/+/$1/si; | |||||||||||||||||||||||||||||||||||||||||
| 172 | # Remove any Tab, CR, LF characters from the URI | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 259 | $modified_rawuri =~ s/[\r\n\t]+//sgi; | |||||||||||||||||||||||||||||||||||||||||
| 174 | # Recursive percent-unescape (everything but '#' not to confuse URI parser) | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 259 | ▶ | while ($modified_rawuri =~ s{%(?!23)([[:xdigit:]]{2})}{chr(hex($1))}esg) { } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | # Parse URI | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 259 | my $uri_obj = URI->new($modified_rawuri); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 259 | ▶ | if (ref($uri_obj) !~ /^URI::https?$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 29 | ▶ | if (!$uri_obj->scheme() || (!$uri_obj->has_recognized_scheme() && $modified_rawuri =~ /^[^:]+:\d{1,5}(?:\/|$)/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $uri_obj->scheme or not $uri_obj->has_recognized_scheme and $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
not $uri_obj->has_recognized_scheme and $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
MC/DC66%2/3
not $uri_obj->scheme or not $uri_obj->has_recognized_scheme and $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
not $uri_obj->scheme
not $uri_obj->has_recognized_scheme
$modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
Truth table75%3/4
not(A) or not(B) and C
A= not $uri_obj->schemeB= not $uri_obj->has_recognized_schemeC= $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 25 | $uri_obj = URI->new("http://" . $modified_rawuri); | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | # Only http and https URIs are supported | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 259 | ▶ | if (ref($uri_obj) !~ /^URI::https?$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | # Remove userinfo | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 255 | $uri_obj->userinfo(undef); | |||||||||||||||||||||||||||||||||||||||||
| 191 | # Remove port | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 255 | $uri_obj->port(undef); | |||||||||||||||||||||||||||||||||||||||||
| 193 | # Remove Fragment | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 255 | $uri_obj->fragment(undef); | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | # Host modifications | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 255 | my $modified_host = $uri_obj->host(); | |||||||||||||||||||||||||||||||||||||||||
| 198 | # Host part cannot be empty | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 255 | ▶ | if ($modified_host =~ /^\s*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | # Collapse consecutive dots into one | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 253 | $modified_host =~ s/\.\.+/\./sg; | |||||||||||||||||||||||||||||||||||||||||
| 204 | # Remove leading and trailing dot | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 253 | $modified_host =~ s/^\.|\.$//sg; | |||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # IPv4 canonicalizations | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 253 | $modified_host = _normalize_ip($modified_host); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 253 | ▶ | if (!defined($modified_host)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 11 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 242 | $uri_obj->host($modified_host); | |||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | # Empty/separator-only host | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 242 | ▶ | if ($uri_obj->host() =~ /^[\s.\/]*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | # Numeric TLD (and not IPv4) | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 241 | ▶ | if ($uri_obj->host() =~ /\.\d[^\.]*$/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$uri_obj->host =~ /\.\d[^\.]*$/ and not $uri_obj->host =~ /^(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})(?:\.(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})){3}$/
MC/DC100%2/2
$uri_obj->host =~ /\.\d[^\.]*$/ and not $uri_obj->host =~ /^(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})(?:\.(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})){3}$/
$uri_obj->host =~ /\.\d[^\.]*$/
not $uri_obj->host =~ /^(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})(?:\.(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})){3}$/
Truth table100%3/3
A and not(B)
A= $uri_obj->host =~ /\.\d[^\.]*$/B= not $uri_obj->host =~ /^(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})(?:\.(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})){3}$/
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | && $uri_obj->host() !~ /^(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})(?:\.(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})){3}$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 238 | my $modified_path = $uri_obj->path(); | |||||||||||||||||||||||||||||||||||||||||
| 226 | # Eliminate current directory /./ parts | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 238 | ▶ | while ($modified_path =~ s/\/\.(?:\/|$)/\//sg) {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | # Eliminate parent directory /something/./ parts | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 238 | ▶ | while ($modified_path =~ s/\/[^\/]+\/\.\.(?:\/|$)/\//sg) {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | # Eliminate double // slashes from path | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 238 | $modified_path =~ s/\/\/+/\//sg; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 238 | $uri_obj->path($modified_path); | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | # Fix some percent encoding | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 238 | my $modified_path_query = $uri_obj->path_query(); | |||||||||||||||||||||||||||||||||||||||||
| 236 | # Fix lone percent signs % | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 238 | $modified_path_query =~ s/%(?![[:xdigit:]]{2})/%25/sg; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 238 | $uri_obj->path_query($modified_path_query); | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 238 | my $canonical = $uri_obj->canonical(); | |||||||||||||||||||||||||||||||||||||||||
| 241 | # Fix caret escaping | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 238 | $canonical=~ s/%5E/\^/sg; | |||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 238 | $self->{uri} = $canonical; | |||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 238 | return $self->{uri}; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | =head1 PRIVATE FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | =item _normalize_ip | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | Function for recognising various IPv4 formatted addresses and convert them to I<dotted-decimal-quad> format (111.11.1.1) | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | =item $host | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | Hostname to parse as IP Address | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | sub _normalize_ip { | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 253 | ▶ | my $host = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_ip: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # Shortcut: If it doesn't look like an IPv4, then return early | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 253 | ▶ | if ($host !~ /^(?:0x[[:xdigit:]]+|\d+)(?:\.(?:0x[[:xdigit:]]+|\d+))*$/si) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 113 | return $host; | |||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | # Most formats are detected and converted by Net::IP::Lite | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 140 | my $ip = Net::IP::Lite->new($host); | |||||||||||||||||||||||||||||||||||||||||
| 281 | 140 | ▶ | if ($ip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 93 | return $ip->transform(); | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | # One and two dots case is missing: xxx.xxxxxxxxxx, xxx.xxx.xxxxxx | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 47 | my $bits = 32; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 47 | my @segments = split(/\./, $host); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 47 | my $segment_count = scalar(@segments); | |||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 47 | my $decimal = 0; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 47 | ▶ | for (my $i = 0; $i < $segment_count; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 122 | my $is_last_segment = $i >= $segment_count - 1; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 122 | ▶ | my $segment = _parse_ipv4_segment($segments[$i], !$is_last_segment ? 8 : $bits); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 122 | ▶ | if (!defined($segment)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 11 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 111 | $bits -= 8; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 111 | ▶ | $decimal += $segment << (!$is_last_segment ? $bits : 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 36 | $ip = Net::IP::Lite->new($decimal); | |||||||||||||||||||||||||||||||||||||||||
| 302 | 36 | return $ip->transform(); | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | =item _parse_ipv4_segment | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | my $decimal = _parse_ipv4_part($segment, $bits) | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | Transforms one IPv4 segment to decimal with range checking. | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | =item $segment | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | Decimal/octal/hexadecimal value to parse | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | =item $bits | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | Bit length for range checking | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | sub _parse_ipv4_segment { | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 122 | ▶ | my $segment = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_ipv4_segment: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 122 | my $bits = shift; | |||||||||||||||||||||||||||||||||||||||||
| 338 | 122 | my $decimal; | |||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | 122 | ▶ | if ($segment =~ /^0+([0-7]{0,10}|[0-3][0-7]{10})$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 34 | $decimal = oct($1); | |||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | elsif ($segment =~ /^0x0*([[:xdigit:]]{1,8})$/si) { | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 35 | $decimal = hex($1); | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | elsif ($segment =~ /^[1-9]\d+$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 49 | $decimal = $segment; | |||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 4 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 118 | ▶ | if ($decimal >= (1 << $bits)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 7 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 111 | return $decimal; | |||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | Some URI normalizatuion cases are still missing: | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =item Highbit characters in hostname are punycoded, not percent encoded. | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | Google's GO Client percent-escapes based on the hostname is a valid unicode string or not | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | Tamás Fehérvári, E<lt>geever@users.sourceforge.net<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 374 | Julien Sobrier, E<lt>julien@sobrier.netE<gt>, | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | Copyright (C) 2016 by Julien Sobrier, Tamás Fehérvári | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 381 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||||||||||||||||
| 382 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||