| 1 | package URL::Normalize; | ||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | URL::Normalize - Normalize/optimize URLs. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Version 0.22 | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 13 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 13 | ▶ | use URI qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 13 | ▶ | use URI::QueryParam qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use URL::Normalize; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 25 | url => 'http://www.example.com/display?lang=en&article=fred', | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # ...or | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 31 | url => '/display?lang=en&article=fred', | ||||||||||||||||||||||||||||
| 32 | base => 'http://www.example.com', | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Get a normalized URL back | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $normalizer->do_all; # Perform all the normalizations available | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | print $normalizer->get_url; # or $normalizer->url; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub new { | ||||||||||||||||||||||||||||
| 44 | 81 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 81 | my $self = { | |||||||||||||||||||||||||||
| 47 | url => undef, | ||||||||||||||||||||||||||||
| 48 | base => undef, | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | 81 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 81 | $self->_init( @_ ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 81 | return $self; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub _init { | ||||||||||||||||||||||||||||
| 58 | 81 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=6 SCAR=21.9
| |||||||||||||||||||||||||||||
| 59 | 81 | my %opts = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 81 | ▶ | if ( defined $opts{url} && length $opts{url} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts{"url"} and length $opts{"url"}
MC/DC0%0/2
defined $opts{"url"} and length $opts{"url"}
defined $opts{"url"}
length $opts{"url"}
Truth table33%1/3
A and B
A= defined $opts{"url"}B= length $opts{"url"}
| |||||||||||||||||||||||||||||
| 62 | 81 | $self->{url} = $opts{url}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||
| 65 | 0 | Carp::croak( "The 'url' parameter is required!" ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 81 | ▶ | if ( defined $opts{base} && length $opts{base} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts{"base"} and length $opts{"base"}
MC/DC0%0/2
defined $opts{"base"} and length $opts{"base"}
defined $opts{"base"}
length $opts{"base"}
Truth table33%1/3
A and B
A= defined $opts{"base"}B= length $opts{"base"}
| |||||||||||||||||||||||||||||
| 69 | 0 | $self->{base} = $opts{base}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 81 | my $URI = $self->get_URI; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 81 | ▶ | unless ( $URI->scheme ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | Carp::croak( "Illegal 'url' and/or 'base' values for '$URI'!" ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 81 | return 1; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub DESTROY { | ||||||||||||||||||||||||||||
| 82 | 81 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 81 | return 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | This is NOT a perfect solution. If you normalize a URL using all the methods in | ||||||||||||||||||||||||||||
| 90 | this module, there is a high probability that the URL will "stop working". This | ||||||||||||||||||||||||||||
| 91 | is merely a helper module for those of you who wants to either normalize a URL | ||||||||||||||||||||||||||||
| 92 | using only a few of the safer methods, and/or for those of you who wants to | ||||||||||||||||||||||||||||
| 93 | generate a unique "ID" from any given URL. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | When writing a web crawler, for example, it's always very costly to check if a | ||||||||||||||||||||||||||||
| 96 | URL has been fetched/seen when you have millions or billions of URLs in a sort | ||||||||||||||||||||||||||||
| 97 | of database. This module can help you create a unique "ID", which you then can | ||||||||||||||||||||||||||||
| 98 | use as a key in a key/value-store; the key is the normalized URL, whereas all | ||||||||||||||||||||||||||||
| 99 | the URLs that converts to the normalized URL are part of the value (normally an | ||||||||||||||||||||||||||||
| 100 | array or hash); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 'http://www.example.com/' = { | ||||||||||||||||||||||||||||
| 103 | 'http://www.example.com:80/' => 1, | ||||||||||||||||||||||||||||
| 104 | 'http://www.example.com/index.html' => 1, | ||||||||||||||||||||||||||||
| 105 | 'http://www.example.com/?' => 1, | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Above, all the URLs inside the hash normalizes to the key if you run these | ||||||||||||||||||||||||||||
| 109 | methods: | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =over 4 | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =item * C<make_canonical> | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item * C<remove_directory_index> | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =item * C<remove_empty_query> | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =back | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 new( %opts ) | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Constructs a new URL::Normalize object. Takes a hash as input argument; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 128 | url => '/foobar.html', # required | ||||||||||||||||||||||||||||
| 129 | base => 'http://www.example.com/', # optional | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 METHODS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 get_URI | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Returns the C<URI> object representing the current state of the URL. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | sub get_URI { | ||||||||||||||||||||||||||||
| 143 | 154 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_URI: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 154 | return URI->new( $self->get_url, $self->get_base ); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 get_url | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Returns the current URL. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub _get { | ||||||||||||||||||||||||||||
| 155 | 508 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_get: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 156 | 508 | my $key = shift; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 508 | ▶ | return ( defined($self->{$key}) && length($self->{$key}) ) ? $self->{$key} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{$key} and length $self->{$key}
MC/DC50%1/2
defined $self->{$key} and length $self->{$key}
defined $self->{$key}
length $self->{$key}
Truth table66%2/3
A and B
A= defined $self->{$key}B= length $self->{$key}
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub get_url { | ||||||||||||||||||||||||||||
| 162 | 342 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 342 | return $self->_get( 'url' ); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 url | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Returns the current URL. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub url { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | return shift->get_url; | ||||||||||||||||||||||||||
|
Subroutine
url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub _set_url { | ||||||||||||||||||||||||||||
| 178 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_set_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | 93 | my $url = shift; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 93 | $self->{url} = $url; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head2 get_base | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Returns the current base. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub get_base { | ||||||||||||||||||||||||||||
| 191 | 166 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_base: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 166 | return $self->_get( 'base' ); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head2 make_canonical | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Just a shortcut for URI::URL->new->canonical->as_string, and involves the | ||||||||||||||||||||||||||||
| 199 | following steps (at least): | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =over 4 | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =item * Converts the scheme and host to lower case. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =item * Capitalizes letters in escape sequences. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =item * Decodes percent-encoded octets of unreserved characters. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =item * Removes the default port (port 80 for http). | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =back | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Example: | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 216 | url => 'http://www.example.com/%7Eusername/', | ||||||||||||||||||||||||||||
| 217 | ); | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | $Normalize->make_canonical; | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | print $Normalize->get_url; # http://www.example.com/~username/ | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | sub make_canonical { | ||||||||||||||||||||||||||||
| 226 | 24 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
make_canonical: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # | ||||||||||||||||||||||||||||
| 229 | # Set new 'url' value | ||||||||||||||||||||||||||||
| 230 | # | ||||||||||||||||||||||||||||
| 231 | 24 | return $self->_set_url( $self->get_URI->canonical->as_string ); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head2 remove_dot_segments | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | The segments ".." and "." will be removed and "folded" (or flattened, if you | ||||||||||||||||||||||||||||
| 237 | prefer) from the URL. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | This method does NOT follow the algorithm described in RFC 3986, but rather | ||||||||||||||||||||||||||||
| 240 | flattens each path segment. It works just as well, it seems, but keep in mind | ||||||||||||||||||||||||||||
| 241 | that it also doesn't account for symbolic links on the server side. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Example: | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 246 | url => 'http://www.example.com/../a/b/../c/./d.html', | ||||||||||||||||||||||||||||
| 247 | ); | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | $normalizer->remove_dot_segments; | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | print $normalizer->get_url; # http://www.example.com/a/c/d.html | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =cut | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | sub remove_dot_segments { | ||||||||||||||||||||||||||||
| 256 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_dot_segments: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 12 | my $URI = URI->new( $self->get_url, $self->get_base ); | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 12 | my @old_segments = split( '/', $URI->path_segments ); | |||||||||||||||||||||||||||
| 261 | 12 | my @new_segments = (); | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 12 | foreach my $segment ( @old_segments ) { | |||||||||||||||||||||||||||
| 264 | 56 | ▶ | if ( $segment eq '.' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 2 | next; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 54 | ▶ | if ( $segment eq '..' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 15 | pop( @new_segments ); | |||||||||||||||||||||||||||
| 270 | 15 | next; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 39 | ▶ | if ( $segment eq '...' ) { # Should be treated as '.' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 1 | next; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 38 | push( @new_segments, $segment ); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 12 | ▶ | if ( @new_segments ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 9 | $URI->path_segments( @new_segments ); | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | else { | ||||||||||||||||||||||||||||
| 284 | 3 | $URI->path_segments( '' ); | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 12 | ▶ | if ( $self->get_url =~ m,/$, ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 3 | $self->_set_url( $URI->as_string . '/' ); | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | else { | ||||||||||||||||||||||||||||
| 291 | 9 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 12 | $self->make_canonical; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head2 remove_directory_index | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | Removes well-known directory indexes, eg. "index.html", "default.asp" etc. | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | Example: | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 304 | url => 'http://www.example.com/index.cgi?foo=/', | ||||||||||||||||||||||||||||
| 305 | ); | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | $normalizer->remove_directory_index; | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | print $normalizer->get_url; # http://www.example.com/?foo=/ | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | You are free to modify the global C<$DIRECTORY_INDEX_REGEXPS> arrayref to | ||||||||||||||||||||||||||||
| 312 | your own fitting: | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | $URL::Normalize::DIRECTORY_INDEX_REGEXPS = [ ... ]; | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | Default values are: | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | $DIRECTORY_INDEX_REGEXPS = [ | ||||||||||||||||||||||||||||
| 319 | '/default\.aspx?', | ||||||||||||||||||||||||||||
| 320 | '/index\.cgi', | ||||||||||||||||||||||||||||
| 321 | '/index\.php\d?', | ||||||||||||||||||||||||||||
| 322 | '/index\.pl', | ||||||||||||||||||||||||||||
| 323 | '/index\.s?html?', | ||||||||||||||||||||||||||||
| 324 | ]; | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 13 | our $DIRECTORY_INDEX_REGEXPS = [ | |||||||||||||||||||||||||||
| 329 | '/default\.aspx?', | ||||||||||||||||||||||||||||
| 330 | '/index\.cgi', | ||||||||||||||||||||||||||||
| 331 | '/index\.php\d?', | ||||||||||||||||||||||||||||
| 332 | '/index\.pl', | ||||||||||||||||||||||||||||
| 333 | '/index\.s?html?', | ||||||||||||||||||||||||||||
| 334 | ]; | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | sub remove_directory_index { | ||||||||||||||||||||||||||||
| 337 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_directory_index: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 18 | my $URI = $self->get_URI; | |||||||||||||||||||||||||||
| 340 | 18 | my $path = $URI->path; | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 18 | foreach my $regexp ( @{$DIRECTORY_INDEX_REGEXPS} ) { | |||||||||||||||||||||||||||
| 343 | 90 | $path =~ s,$regexp,/,; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 18 | $URI->path( $path ); | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # | ||||||||||||||||||||||||||||
| 349 | # Set new 'url' value | ||||||||||||||||||||||||||||
| 350 | # | ||||||||||||||||||||||||||||
| 351 | 18 | return $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =head2 sort_query_parameters | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | Sorts the query parameters alphabetically. | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | Uppercased parameters will be lowercased during sorting only, and if there are | ||||||||||||||||||||||||||||
| 360 | multiple values for a parameter, the key/value-pairs will be sorted as well. | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Example: | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 365 | url => 'http://www.example.com/?b=2&c=3&a=0&A=1', | ||||||||||||||||||||||||||||
| 366 | ); | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | $normalizer->sort_query_parameters; | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | print $normalizer->get_url; # http://www.example.com/?a=0&A=1&b=2&c=3 | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =cut | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | sub sort_query_parameters { | ||||||||||||||||||||||||||||
| 375 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
sort_query_parameters: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 8 | my $URI = $self->get_URI; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 8 | ▶ | if ( $self->get_URI->as_string =~ m,\?, ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | 6 | ▶ | my $query_hash = $URI->query_form_hash || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$URI->query_form_hash || {}
MC/DC0%0/1
$URI->query_form_hash || {}
$URI->query_form_hash
Truth table50%1/2
A
A= $URI->query_form_hash
| |||||||||||||||||||||||||||||
| 381 | 6 | my $query_string = ''; | |||||||||||||||||||||||||||
| 382 | 6 | my %new_query_hash = (); | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 6 | foreach my $key ( sort { lc($a) cmp lc($b) } keys %{$query_hash} ) { | |||||||||||||||||||||||||||
| 385 | 17 | my $values = $query_hash->{ $key }; | |||||||||||||||||||||||||||
| 386 | 17 | ▶ | unless ( ref $values ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 16 | $values = [ $values ]; | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 17 | foreach my $value ( @{$values} ) { | |||||||||||||||||||||||||||
| 391 | 18 | push( @{ $new_query_hash{lc($key)}->{$value} }, $key ); | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 6 | foreach my $sort_key ( sort keys %new_query_hash ) { | |||||||||||||||||||||||||||
| 396 | 15 | foreach my $value ( sort keys %{$new_query_hash{$sort_key}} ) { | |||||||||||||||||||||||||||
| 397 | 18 | foreach my $key ( @{$new_query_hash{$sort_key}->{$value}} ) { | |||||||||||||||||||||||||||
| 398 | 18 | $query_string .= $key . '=' . $value . '&'; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 6 | $query_string =~ s,&$,,; | |||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 6 | $URI->query( $query_string ); | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | # | ||||||||||||||||||||||||||||
| 409 | # Set new 'url' value | ||||||||||||||||||||||||||||
| 410 | # | ||||||||||||||||||||||||||||
| 411 | 8 | return $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =head2 remove_duplicate_query_parameters | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | Removes duplicate query parameters, i.e. where the key/value combination is | ||||||||||||||||||||||||||||
| 417 | identical with another key/value combination. | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | Example: | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 422 | url => 'http://www.example.com/?a=1&a=2&b=4&a=1&c=4', | ||||||||||||||||||||||||||||
| 423 | ); | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | $normalizer->remove_duplicate_query_parameters; | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | print $normalizer->get_url; # http://www.example.com/?a=1&a=2&b=3&c=4 | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =cut | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | sub remove_duplicate_query_parameters { | ||||||||||||||||||||||||||||
| 432 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_duplicate_query_parameters: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 1 | my $URI = $self->get_URI; | |||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 1 | my %seen = (); | |||||||||||||||||||||||||||
| 437 | 1 | my @new_query = (); | |||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 1 | foreach my $key ( $URI->query_param ) { | |||||||||||||||||||||||||||
| 440 | 3 | my @values = $URI->query_param( $key ); | |||||||||||||||||||||||||||
| 441 | 3 | foreach my $value ( @values ) { | |||||||||||||||||||||||||||
| 442 | 5 | ▶ | unless ( $seen{$key}->{$value} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 443 | 4 | push( @new_query, { key => $key, value => $value } ); | |||||||||||||||||||||||||||
| 444 | 4 | $seen{$key}->{$value}++; | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 1 | my $query_string = ''; | |||||||||||||||||||||||||||
| 450 | 1 | foreach ( @new_query ) { | |||||||||||||||||||||||||||
| 451 | 4 | $query_string .= $_->{key} . '=' . $_->{value} . '&'; | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 1 | $query_string =~ s,&$,,; | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 1 | $URI->query( $query_string ); | |||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | # | ||||||||||||||||||||||||||||
| 459 | # Set new 'url' value | ||||||||||||||||||||||||||||
| 460 | # | ||||||||||||||||||||||||||||
| 461 | 1 | return $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | =head2 remove_empty_query_parameters | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | Removes empty query parameters, i.e. where there are keys with no value. | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | Example: | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 471 | url => 'http://www.example.com/?a=1&b=&c=3', | ||||||||||||||||||||||||||||
| 472 | ); | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | $Normalize->remove_empty_query_parameters; | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | print $normalizer->get_url; # http://www.example.com/?a=1&c=3 | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | =cut | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | sub remove_empty_query_parameters { | ||||||||||||||||||||||||||||
| 481 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_empty_query_parameters: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 2 | my $URI = $self->get_URI; | |||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 2 | foreach my $key ( $URI->query_param ) { | |||||||||||||||||||||||||||
| 486 | 6 | my @values = $URI->query_param( $key ); | |||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | 6 | $URI->query_param_delete( $key ); | |||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 6 | foreach my $value ( @values ) { | |||||||||||||||||||||||||||
| 491 | 7 | ▶ | if ( defined $value && length $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and length $value
MC/DC50%1/2
defined $value and length $value
defined $value
length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||
| 492 | 4 | $URI->query_param_append( $key, $value ); | |||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 2 | return $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | =head2 remove_empty_query | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | Removes empty query from the URL. | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | Example: | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 507 | url => 'http://www.example.com/foo?', | ||||||||||||||||||||||||||||
| 508 | ); | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | $normalizer->remove_empty_query; | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | print $Normalize->get_url; # http://www.example.com/foo | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =cut | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | sub remove_empty_query { | ||||||||||||||||||||||||||||
| 517 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_empty_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 3 | my $url = $self->get_url; | |||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | 3 | $url =~ s,\?$,,; | |||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | # | ||||||||||||||||||||||||||||
| 524 | # Set new 'url' value | ||||||||||||||||||||||||||||
| 525 | # | ||||||||||||||||||||||||||||
| 526 | 3 | $self->_set_url( $url ); | |||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | =head2 remove_fragment | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | Removes the fragment from the URL, but only if they are at the end of the URL. | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | For example "http://www.example.com/#foo" will be translated to | ||||||||||||||||||||||||||||
| 534 | "http://www.example.com/", but "http://www.example.com/#foo/bar" will stay the | ||||||||||||||||||||||||||||
| 535 | same. | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | Example: | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 540 | url => 'http://www.example.com/bar.html#section1', | ||||||||||||||||||||||||||||
| 541 | ); | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | $normalizer->remove_fragment; | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | print $normalizer->get_url; # http://www.example.com/bar.html | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | You should probably use this with caution, as most web frameworks today allows | ||||||||||||||||||||||||||||
| 548 | fragments for logic, for example: | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | http://www.example.com/players#all | ||||||||||||||||||||||||||||
| 551 | http://www.example.com/players#banned | ||||||||||||||||||||||||||||
| 552 | http://www.example.com/players#top | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | ...can all result in very different results, despite their "unfragmented" URL | ||||||||||||||||||||||||||||
| 555 | being the same. | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | =cut | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | sub remove_fragment { | ||||||||||||||||||||||||||||
| 560 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_fragment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | 13 | my $url = $self->get_url; | |||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | 13 | $url =~ s{#(?:/|[^?/]*)$}{}; | |||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | 13 | $self->_set_url( $url ); | |||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | =head2 remove_fragments | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | Removes EVERYTHING after a '#'. You should use this sparsely, because a lot | ||||||||||||||||||||||||||||
| 572 | of web applications these days returns different output in response to what | ||||||||||||||||||||||||||||
| 573 | the fragment is, for example: | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | http://www.example.com/users#list | ||||||||||||||||||||||||||||
| 576 | http://www.example.com/users#edit | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | ...etc. | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | =cut | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | sub remove_fragments { | ||||||||||||||||||||||||||||
| 583 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_fragments: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | 0 | my $url = $self->get_url; | |||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | 0 | $url =~ s/#.*//; | |||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | 0 | $self->_set_url( $url ); | |||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | =head2 remove_duplicate_slashes | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | Remove duplicate slashes from the URL. | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | Example: | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 599 | url => 'http://www.example.com/foo//bar.html', | ||||||||||||||||||||||||||||
| 600 | ); | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | $normalizer->remove_duplicate_slashes; | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | print $normalizer->get_url; # http://www.example.com/foo/bar.html | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | =cut | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | sub remove_duplicate_slashes { | ||||||||||||||||||||||||||||
| 609 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_duplicate_slashes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | 5 | my $URI = $self->get_URI; | |||||||||||||||||||||||||||
| 612 | 5 | my $path = $URI->path; | |||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | 5 | $path =~ s,/+,/,g; | |||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | 5 | $URI->path( $path ); | |||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | # | ||||||||||||||||||||||||||||
| 619 | # Set new 'url' value | ||||||||||||||||||||||||||||
| 620 | # | ||||||||||||||||||||||||||||
| 621 | 5 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | =head2 remove_hostname_prefix | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | Removes 'www' followed by a potential number before the actual hostname. | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | Example: | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 631 | url => 'http://www.example.com/', | ||||||||||||||||||||||||||||
| 632 | ); | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | $normalizer->remove_hostname_prefix; | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | print $normalizer->get_url; # http://example.com/ | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | =cut | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | sub remove_hostname_prefix { | ||||||||||||||||||||||||||||
| 641 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_hostname_prefix: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | 5 | my $URI = $self->get_URI; | |||||||||||||||||||||||||||
| 644 | 5 | my $host = $URI->host; | |||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | # | ||||||||||||||||||||||||||||
| 647 | # Count the number of parts in the hostname. If it's more than two parts | ||||||||||||||||||||||||||||
| 648 | # in the URL, it's safe (...) to remove the "www\d*?\." prefix. | ||||||||||||||||||||||||||||
| 649 | # | ||||||||||||||||||||||||||||
| 650 | 5 | my @parts = split( /\./, $host ); | |||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||
| 652 | 5 | ▶ | if ( scalar(@parts) > 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 653 | 3 | $host =~ s,^www\d*?\.,,; | |||||||||||||||||||||||||||
| 654 | 3 | $URI->host( $host ); | |||||||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | # | ||||||||||||||||||||||||||||
| 658 | # Set new 'url' value | ||||||||||||||||||||||||||||
| 659 | # | ||||||||||||||||||||||||||||
| 660 | 5 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | =head2 remove_social_query_parameters | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | Removes query parameters that are used for "social tracking". | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | For example, a lot of newspapers posts links to their articles on Twitter, | ||||||||||||||||||||||||||||
| 668 | and adds a lot of "noisee" in the URL so that they are able to track the | ||||||||||||||||||||||||||||
| 669 | number of users clicking on that specific URL. This method attempts to | ||||||||||||||||||||||||||||
| 670 | remove those query parameters. | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | You are free to modify the global C<$SOCIAL_QUERY_PARAMETERS> arrayref to | ||||||||||||||||||||||||||||
| 673 | your own fitting: | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | $URL::Normalize::SOCIAL_QUERY_PARAMETERS = [ ... ]; | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | Default values are: | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | $SOCIAL_QUERY_PARAMETERS = [ | ||||||||||||||||||||||||||||
| 680 | 'ncid', | ||||||||||||||||||||||||||||
| 681 | 'utm_campaign', | ||||||||||||||||||||||||||||
| 682 | 'utm_medium', | ||||||||||||||||||||||||||||
| 683 | 'utm_source', | ||||||||||||||||||||||||||||
| 684 | ]; | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | =cut | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | 13 | our $SOCIAL_QUERY_PARAMETERS = [ | |||||||||||||||||||||||||||
| 689 | 'ncid', | ||||||||||||||||||||||||||||
| 690 | 'utm_campaign', | ||||||||||||||||||||||||||||
| 691 | 'utm_medium', | ||||||||||||||||||||||||||||
| 692 | 'utm_source', | ||||||||||||||||||||||||||||
| 693 | ]; | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | sub remove_social_query_parameters { | ||||||||||||||||||||||||||||
| 696 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_social_query_parameters: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | 2 | my $URI = $self->get_URI; | |||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | # | ||||||||||||||||||||||||||||
| 701 | # Remove unwanted query parameters | ||||||||||||||||||||||||||||
| 702 | # | ||||||||||||||||||||||||||||
| 703 | 2 | foreach ( @{$SOCIAL_QUERY_PARAMETERS} ) { | |||||||||||||||||||||||||||
| 704 | 8 | $URI->query_param_delete( $_ ); | |||||||||||||||||||||||||||
| 705 | } | ||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||
| 707 | # | ||||||||||||||||||||||||||||
| 708 | # Set new 'url' value | ||||||||||||||||||||||||||||
| 709 | # | ||||||||||||||||||||||||||||
| 710 | 2 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | =head2 do_all | ||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||
| 716 | Performs all of the normalization methods mentioned above. | ||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||
| 718 | =cut | ||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||
| 720 | sub do_all { | ||||||||||||||||||||||||||||
| 721 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
do_all: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | 0 | $self->make_canonical; | |||||||||||||||||||||||||||
| 724 | 0 | $self->remove_dot_segments; | |||||||||||||||||||||||||||
| 725 | 0 | $self->remove_directory_index; | |||||||||||||||||||||||||||
| 726 | 0 | $self->sort_query_parameters; | |||||||||||||||||||||||||||
| 727 | 0 | $self->remove_fragment; | |||||||||||||||||||||||||||
| 728 | 0 | $self->remove_duplicate_slashes; | |||||||||||||||||||||||||||
| 729 | 0 | $self->remove_duplicate_query_parameters; | |||||||||||||||||||||||||||
| 730 | 0 | $self->remove_empty_query_parameters; | |||||||||||||||||||||||||||
| 731 | 0 | $self->remove_hostname_prefix; | |||||||||||||||||||||||||||
| 732 | 0 | $self->remove_empty_query; | |||||||||||||||||||||||||||
| 733 | 0 | $self->remove_social_query_parameters; | |||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | 0 | return 1; | |||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||
| 738 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | =over 4 | ||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | =item * L<URI> | ||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||
| 744 | =item * L<URI::URL> | ||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | =item * L<URI::QueryParam> | ||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | =item * L<RFC 3986: Uniform Resource Indentifier|http://tools.ietf.org/html/rfc3986> | ||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||
| 750 | =item * L<Wikipedia: URL normalization|http://en.wikipedia.org/wiki/URL_normalization> | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | =back | ||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||
| 754 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||
| 756 | Tore Aursand, C<< <toreau at gmail.com> >> | ||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | =head1 BUGS | ||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | Please report any bugs or feature requests to the web interface at L<https://rt.cpan.org/Dist/Display.html?Name=URL-Normalize> | ||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||
| 762 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | perldoc URL::Normalize | ||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||
| 768 | You can also look for information at: | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | =over 4 | ||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | L<http://annocpan.org/dist/URL-Normalize> | ||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | L<http://cpanratings.perl.org/d/URL-Normalize> | ||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||
| 782 | L<http://search.cpan.org/dist/URL-Normalize/> | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | =back | ||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||
| 788 | Copyright 2012-2015 Tore Aursand. | ||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 791 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 792 | copy of the full license at: | ||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||
| 794 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | Any use, modification, and distribution of the Standard or Modified | ||||||||||||||||||||||||||||
| 797 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||||||||||||||||||||
| 798 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||||||||||||||||||||
| 799 | or distribute the Package, if you do not accept this license. | ||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | If your Modified Version has been derived from a Modified Version made | ||||||||||||||||||||||||||||
| 802 | by someone other than you, you are nevertheless required to ensure that | ||||||||||||||||||||||||||||
| 803 | your Modified Version complies with the requirements of this license. | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | This license does not grant you the right to use any trademark, service | ||||||||||||||||||||||||||||
| 806 | mark, tradename, or logo of the Copyright Holder. | ||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||
| 808 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||||||||||||||||||||
| 809 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||||||||||||||||||||
| 810 | otherwise transfer the Package with respect to any patent claims | ||||||||||||||||||||||||||||
| 811 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||||||||||||||||||||
| 812 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||||||||||||||||||||
| 813 | counterclaim) against any party alleging that the Package constitutes | ||||||||||||||||||||||||||||
| 814 | direct or contributory patent infringement, then this Artistic License | ||||||||||||||||||||||||||||
| 815 | to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||||||||||||||||||||
| 818 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||||||||||||||||||||
| 819 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||||||||||||||||
| 820 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||||||||||||||||||||
| 821 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||||||||||||||||||||
| 822 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||||||||||||||||||||
| 823 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||||||||||||||||||||
| 824 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||
| 826 | =cut | ||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||
| 828 | 13 | 1; # End of URL::Normalize | |||||||||||||||||||||||||||