| 1 | package URL::Normalize; | ||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use URI qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use URI::QueryParam qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | URL::Normalize - Normalize/optimize URLs. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.33 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 12 | our $VERSION = '0.33'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use URL::Normalize; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $normalizer = URL::Normalize->new( 'http://www.example.com/display?lang=en&article=fred' ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Normalize the URL of your choosing. | ||||||||||||||||||||||||||||
| 27 | $normalizer->remove_social_query_params; | ||||||||||||||||||||||||||||
| 28 | $normalizer->make_canonical; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Get the normalized version back. | ||||||||||||||||||||||||||||
| 31 | my $url = $normalizer->url; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | This is NOT a perfect solution. If you normalize a URL using all the methods in | ||||||||||||||||||||||||||||
| 38 | this module, there is a high probability that the URL will stop "working." This | ||||||||||||||||||||||||||||
| 39 | is merely a helper module for those of you who wants to either normalize a URL | ||||||||||||||||||||||||||||
| 40 | using only a few of the safer methods, and/or for those of you who wants to | ||||||||||||||||||||||||||||
| 41 | generate a unique "ID" from any given URL. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | When writing a web crawler, for example, it's always very costly to check if a | ||||||||||||||||||||||||||||
| 44 | URL has been fetched/seen when you have millions or billions of URLs in a sort | ||||||||||||||||||||||||||||
| 45 | of database. This module can help you create a unique "ID", which you then can | ||||||||||||||||||||||||||||
| 46 | use as a key in a key/value-store; the key is the normalized URL, whereas all | ||||||||||||||||||||||||||||
| 47 | the URLs that converts to the normalized URL are part of the value (normally an | ||||||||||||||||||||||||||||
| 48 | array or hash); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 'http://www.example.com/' = { | ||||||||||||||||||||||||||||
| 51 | 'http://www.example.com:80/' => 1, | ||||||||||||||||||||||||||||
| 52 | 'http://www.example.com/index.html' => 1, | ||||||||||||||||||||||||||||
| 53 | 'http://www.example.com/?' => 1, | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Above, all the URLs inside the hash normalizes to the key if you run these | ||||||||||||||||||||||||||||
| 57 | methods: | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =over 4 | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =item * C<make_canonical> | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item * C<remove_directory_index> | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item * C<remove_empty_query> | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =back | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | You could think of this module as a URL-specific L<Bloom::Filter> helper. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 new( $url ) | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Constructs a new URL::Normalize object: | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | my $normalizer = URL::Normalize->new( 'http://www.example.com/some/path' ); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | You can also send in just the path: | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | my $normalizer = URL::Normalize->new( '/some/path' ); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | The latter is not recommended, however, so you should look into L<URI>'s | ||||||||||||||||||||||||||||
| 84 | C<new_abs> to create absolute URLs before you use them with URL::Normalize. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 METHODS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | around BUILDARGS => sub { | ||||||||||||||||||||||||||||
| 93 | 113 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 94 | 113 | my $class = shift; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 113 | ▶ | if ( @_ == 1 && !ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and not ref $_[0]
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1
not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||
| 97 | 26 | return $class->$orig( url => $_[0] ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 87 | return $class->$orig( @_ ); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 12 | }; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 url | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Get the current URL, preferably after you have run one or more of the | ||||||||||||||||||||||||||||
| 107 | normalization methods. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 12 | has 'url' => ( | |||||||||||||||||||||||||||
| 112 | isa => 'Str', | ||||||||||||||||||||||||||||
| 113 | is => 'ro', | ||||||||||||||||||||||||||||
| 114 | required => 1, | ||||||||||||||||||||||||||||
| 115 | writer => '_set_url', | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | has 'dir_index_regexps' => ( | ||||||||||||||||||||||||||||
| 119 | traits => [ 'Array' ], | ||||||||||||||||||||||||||||
| 120 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 121 | is => 'rw', | ||||||||||||||||||||||||||||
| 122 | handles => { | ||||||||||||||||||||||||||||
| 123 | 'add_directory_index_regexp' => 'push', | ||||||||||||||||||||||||||||
| 124 | }, | ||||||||||||||||||||||||||||
| 125 | default => sub { | ||||||||||||||||||||||||||||
| 126 | [ | ||||||||||||||||||||||||||||
| 127 | 113 | ▶ | '/default\.aspx?', | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | '/index\.cgi', | ||||||||||||||||||||||||||||
| 129 | '/index\.php\d?', | ||||||||||||||||||||||||||||
| 130 | '/index\.pl', | ||||||||||||||||||||||||||||
| 131 | '/index\.s?html?', | ||||||||||||||||||||||||||||
| 132 | ]; | ||||||||||||||||||||||||||||
| 133 | }, | ||||||||||||||||||||||||||||
| 134 | 12 | ); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | has 'social_query_params' => ( | ||||||||||||||||||||||||||||
| 137 | traits => [ 'Array' ], | ||||||||||||||||||||||||||||
| 138 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 139 | is => 'rw', | ||||||||||||||||||||||||||||
| 140 | handles => { | ||||||||||||||||||||||||||||
| 141 | 'add_social_query_param' => 'push', | ||||||||||||||||||||||||||||
| 142 | }, | ||||||||||||||||||||||||||||
| 143 | default => sub { | ||||||||||||||||||||||||||||
| 144 | [ | ||||||||||||||||||||||||||||
| 145 | 113 | ▶ | 'ncid', | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 146 | 'utm_campaign', | ||||||||||||||||||||||||||||
| 147 | 'utm_medium', | ||||||||||||||||||||||||||||
| 148 | 'utm_source', | ||||||||||||||||||||||||||||
| 149 | ], | ||||||||||||||||||||||||||||
| 150 | }, | ||||||||||||||||||||||||||||
| 151 | 12 | ); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 get_url | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | DEPRECATED! Use C<url> instead. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | sub get_url { | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | Carp::carp( "The 'get_url' method is deprecated; start using the 'url' method instead." ); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | return $self->url; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 URI | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Returns a L<URI> representation of the current URL. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub URI { | ||||||||||||||||||||||||||||
| 174 | 130 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
URI: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 130 | my $URI = undef; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 130 | eval { | |||||||||||||||||||||||||||
| 179 | 130 | $URI = URI->new( $self->url ); | |||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 130 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | Carp::carp( "Failed to create a URI object from URL '" . $self->url . "'" ); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 130 | return $URI; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 make_canonical | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Just a shortcut for URI::URL->new->canonical->as_string, and involves the | ||||||||||||||||||||||||||||
| 192 | following steps (at least): | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =over 4 | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =item * Converts the scheme and host to lower case. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =item * Capitalizes letters in escape sequences. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =item * Decodes percent-encoded octets of unreserved characters. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =item * Removes the default port (port 80 for http). | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =back | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Example: | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 209 | url => 'HTTP://www.example.com:80/%7Eusername/', | ||||||||||||||||||||||||||||
| 210 | ); | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | $normalizer->make_canonical; | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | print $normalizer->url; # http://www.example.com/~username/ | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | sub make_canonical { | ||||||||||||||||||||||||||||
| 219 | 38 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
make_canonical: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 38 | ▶ | if ( my $URI = $self->URI ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 38 | $self->_set_url( $URI->canonical->as_string ); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||
| 225 | 0 | Carp::carp( "Can't make non-URI URLs canonical." ); | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head2 remove_dot_segments | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | The C<.>, C<..> and C<...> segments will be removed and "folded" (or "flattened", if | ||||||||||||||||||||||||||||
| 232 | you prefer) from the URL. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | This method does NOT follow the algorithm described in L<RFC 3986: Uniform Resource Indentifier|http://tools.ietf.org/html/rfc3986>, | ||||||||||||||||||||||||||||
| 235 | but rather flattens each path segment. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Also keep in mind that this method doesn't (can't) account for symbolic links | ||||||||||||||||||||||||||||
| 238 | on the server side. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | Example: | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 243 | url => 'http://www.example.com/../a/b/../c/./d.html', | ||||||||||||||||||||||||||||
| 244 | ); | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | $normalizer->remove_dot_segments; | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | print $normalizer->get_url; # http://www.example.com/a/c/d.html | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =cut | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | sub remove_dot_segments { | ||||||||||||||||||||||||||||
| 253 | 24 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_dot_segments: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 24 | my @old_path_segments = (); | |||||||||||||||||||||||||||
| 256 | 24 | my @new_path_segments = (); | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 24 | my $URI = $self->URI; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 24 | ▶ | if ( my $URI = $self->URI ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 24 | @old_path_segments = split( '/', $URI->path_segments ); | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 24 | foreach my $segment ( @old_path_segments ) { | |||||||||||||||||||||||||||
| 264 | 112 | ▶ | if ( $segment eq '.' || $segment eq '...' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$segment eq "." or $segment eq "..."
MC/DC100%2/2
$segment eq "." or $segment eq "..."
$segment eq "."
$segment eq "..."
Truth table100%3/3
A or B
A= $segment eq "."B= $segment eq "..."
| |||||||||||||||||||||||||||||
| 265 | 6 | next; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 106 | ▶ | if ( $segment eq '..' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 30 | pop( @new_path_segments ); | |||||||||||||||||||||||||||
| 270 | 30 | next; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 76 | push( @new_path_segments, $segment ); | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 24 | ▶ | if ( @new_path_segments ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 18 | $URI->path_segments( @new_path_segments ); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | else { | ||||||||||||||||||||||||||||
| 280 | 6 | $URI->path_segments( '' ); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 24 | my $new_url = $URI->as_string; | |||||||||||||||||||||||||||
| 284 | 24 | ▶ | $new_url = '/' . $new_url if ( $self->url =~ m,^/, ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 24 | ▶ | $new_url = $new_url . '/' if ( $self->url =~ m,/$, ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 24 | $new_url =~ s,^/+,/,; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 24 | $self->_set_url( $new_url ); | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 24 | $self->make_canonical; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =head2 remove_directory_index | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | Removes well-known directory indexes, eg. C<index.html>, C<default.asp> etc. | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Example: | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 301 | url => 'http://www.example.com/index.cgi?foo=/', | ||||||||||||||||||||||||||||
| 302 | ); | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | $normalizer->remove_directory_index; | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | print $normalizer->url; # http://www.example.com/?foo=/ | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | The default regular expressions for matching a directory index are: | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =over 4 | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =item * C</default\.aspx?> | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =item * C</index\.cgi> | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =item * C</index\.php\d?> | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =item * C</index\.pl> | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | =item * C</index\.s?html?> | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =back | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | You can override these by sending in your own list of regular expressions | ||||||||||||||||||||||||||||
| 325 | when creating the URL::Normalizer object: | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 328 | url => 'http://www.example.com/index.cgi?foo=/', | ||||||||||||||||||||||||||||
| 329 | dir_index_regexps => [ 'MyDirIndex\.html' ], # etc. | ||||||||||||||||||||||||||||
| 330 | ); | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | You can also choose to add regular expressions after the URL::Normalize | ||||||||||||||||||||||||||||
| 333 | object has been created: | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 336 | url => 'http://www.example.com/index.cgi?foo=/', | ||||||||||||||||||||||||||||
| 337 | dir_index_regexps => [ 'MyDirIndex\.html' ], # etc. | ||||||||||||||||||||||||||||
| 338 | ); | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # ... | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | push( @{$normalizer->dir_index_regexps}, 'MyDirIndex\.html' ); | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | Keep in mind that the regular expression are NOT case-sensitive, so the | ||||||||||||||||||||||||||||
| 345 | default C</default\.aspx?> expression will NOT match C</Default\.aspx?>. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =cut | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | sub remove_directory_index { | ||||||||||||||||||||||||||||
| 350 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_directory_index: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 18 | ▶ | if ( my $URI = $self->URI ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | 18 | ▶ | if ( my $path = $URI->path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 18 | foreach my $regex ( @{$self->dir_index_regexps} ) { | |||||||||||||||||||||||||||
| 355 | 90 | $path =~ s,$regex,/,; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 18 | $URI->path( $path ); | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 18 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head2 sort_query_parameters | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | Sorts the URL's query parameters alphabetically. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | Uppercased parameters will be lowercased during sorting, and if there are | ||||||||||||||||||||||||||||
| 370 | multiple values for a parameter, the key/value-pairs will be sorted as well. | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | Example: | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 375 | url => 'http://www.example.com/?b=2&c=3&a=0&A=1', | ||||||||||||||||||||||||||||
| 376 | ); | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | $normalizer->sort_query_parameters; | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | print $normalizer->url; # http://www.example.com/?a=0&A=1&b=2&c=3 | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | sub sort_query_parameters { | ||||||||||||||||||||||||||||
| 385 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
sort_query_parameters: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 15 | ▶ | if ( my $URI = $self->URI ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | 15 | ▶ | if ( $URI->as_string =~ m,\?, ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 12 | ▶ | 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
| |||||||||||||||||||||||||||||
| 390 | 12 | my $query_string = ''; | |||||||||||||||||||||||||||
| 391 | 12 | my %new_query_hash = (); | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 12 | foreach my $key ( sort { lc($a) cmp lc($b) } keys %{$query_hash} ) { | |||||||||||||||||||||||||||
| 394 | 34 | my $values = $query_hash->{ $key }; | |||||||||||||||||||||||||||
| 395 | 34 | ▶ | unless ( ref $values ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | 32 | $values = [ $values ]; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 34 | foreach my $value ( @{$values} ) { | |||||||||||||||||||||||||||
| 400 | 36 | push( @{ $new_query_hash{lc($key)}->{$value} }, $key ); | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 12 | foreach my $sort_key ( sort keys %new_query_hash ) { | |||||||||||||||||||||||||||
| 405 | 30 | foreach my $value ( sort keys %{$new_query_hash{$sort_key}} ) { | |||||||||||||||||||||||||||
| 406 | 36 | foreach my $key ( @{$new_query_hash{$sort_key}->{$value}} ) { | |||||||||||||||||||||||||||
| 407 | 36 | $query_string .= $key . '=' . $value . '&'; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 12 | $query_string =~ s,&$,,; | |||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 12 | $URI->query( $query_string ); | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 15 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =head2 remove_duplicate_query_parameters | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Removes duplicate query parameters, i.e. where the key/value combination is | ||||||||||||||||||||||||||||
| 424 | identical with another key/value combination. | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | Example: | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 429 | url => 'http://www.example.com/?a=1&a=2&b=4&a=1&c=4', | ||||||||||||||||||||||||||||
| 430 | ); | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | $normalizer->remove_duplicate_query_parameters; | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | print $normalizer->url; # http://www.example.com/?a=1&a=2&b=3&c=4 | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =cut | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | sub remove_duplicate_query_parameters { | ||||||||||||||||||||||||||||
| 439 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_duplicate_query_parameters: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 2 | ▶ | if ( my $URI = $self->URI ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | 2 | my %seen = (); | |||||||||||||||||||||||||||
| 443 | 2 | my @new_query = (); | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 2 | foreach my $key ( $URI->query_param ) { | |||||||||||||||||||||||||||
| 446 | 6 | my @values = $URI->query_param( $key ); | |||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | 6 | foreach my $value ( @values ) { | |||||||||||||||||||||||||||
| 449 | 10 | ▶ | unless ( $seen{$key}->{$value} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 450 | 8 | push( @new_query, { key => $key, value => $value } ); | |||||||||||||||||||||||||||
| 451 | 8 | $seen{$key}->{$value}++; | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 2 | my $query_string = ''; | |||||||||||||||||||||||||||
| 457 | 2 | foreach ( @new_query ) { | |||||||||||||||||||||||||||
| 458 | 8 | $query_string .= $_->{key} . '=' . $_->{value} . '&'; | |||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 2 | $query_string =~ s,&$,,; | |||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 2 | $URI->query( $query_string ); | |||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 2 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =head2 remove_empty_query_parameters | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | Removes empty query parameters, i.e. where there are keys with no value. This | ||||||||||||||||||||||||||||
| 472 | only removes BLANK values, not values considered to be no value, like zero (0). | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | Example: | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 477 | url => 'http://www.example.com/?a=1&b=&c=3', | ||||||||||||||||||||||||||||
| 478 | ); | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | $normalizer->remove_empty_query_parameters; | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | print $normalizer->url; # http://www.example.com/?a=1&c=3 | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =cut | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | sub remove_empty_query_parameters { | ||||||||||||||||||||||||||||
| 487 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_empty_query_parameters: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 2 | ▶ | if ( my $URI = $self->URI ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 490 | 2 | foreach my $key ( $URI->query_param ) { | |||||||||||||||||||||||||||
| 491 | 6 | my @values = $URI->query_param( $key ); | |||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 6 | $URI->query_param_delete( $key ); | |||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 6 | foreach my $value ( @values ) { | |||||||||||||||||||||||||||
| 496 | 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
| |||||||||||||||||||||||||||||
| 497 | 4 | $URI->query_param_append( $key, $value ); | |||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | 2 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =head2 remove_empty_query | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | Removes empty query from the URL. | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | Example: | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 513 | url => 'http://www.example.com/foo?', | ||||||||||||||||||||||||||||
| 514 | ); | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | $normalizer->remove_empty_query; | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | print $Normalize->url; # http://www.example.com/foo | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | =cut | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | sub remove_empty_query { | ||||||||||||||||||||||||||||
| 523 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_empty_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | 3 | my $url = $self->url; | |||||||||||||||||||||||||||
| 526 | 3 | $url =~ s,\?$,,; | |||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | 3 | $self->_set_url( $url ); | |||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | =head2 remove_fragment | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | Removes the fragment from the URL, but only if they are at the end of the URL. | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | For example C<http://www.example.com/#foo> will be translated to | ||||||||||||||||||||||||||||
| 536 | C<http://www.example.com/>, but C<http://www.example.com/#foo/bar> will stay the | ||||||||||||||||||||||||||||
| 537 | same. | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | Example: | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 542 | url => 'http://www.example.com/bar.html#section1', | ||||||||||||||||||||||||||||
| 543 | ); | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | $normalizer->remove_fragment; | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | print $normalizer->url; # http://www.example.com/bar.html | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | You should probably use this with caution, as most web frameworks today allows | ||||||||||||||||||||||||||||
| 550 | fragments for logic, for example: | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | =over 4 | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | =item * C<http://www.example.com/players#all> | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | =item * C<http://www.example.com/players#banned> | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | =item * C<http://www.example.com/players#top> | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | =back | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | ...can all result in very different results, despite their "unfragmented" URL | ||||||||||||||||||||||||||||
| 563 | being the same. | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | =cut | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | sub remove_fragment { | ||||||||||||||||||||||||||||
| 568 | 26 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_fragment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | 26 | my $url = $self->url; | |||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | 26 | $url =~ s{#(?:/|[^?/]*)$}{}; | |||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | 26 | $self->_set_url( $url ); | |||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | =head2 remove_fragments | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | Removes EVERYTHING after a C<#>. As with C<remove_fragment>, you should use this | ||||||||||||||||||||||||||||
| 580 | with caution, because a lot of web applications these days returns different | ||||||||||||||||||||||||||||
| 581 | output in response to what the fragment is, for example: | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | =over 4 | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | =item * C<http://www.example.com/users#list> | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | =item * C<http://www.example.com/users#edit> | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | =back | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | ...etc. | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | =cut | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | sub remove_fragments { | ||||||||||||||||||||||||||||
| 596 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_fragments: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | 0 | my $url = $self->get_url; | |||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 0 | $url =~ s/#.*//; | |||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | 0 | $self->_set_url( $url ); | |||||||||||||||||||||||||||
| 603 | } | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | =head2 remove_duplicate_slashes | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | Remove duplicate slashes from the URL. | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | Example: | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 612 | url => 'http://www.example.com/foo//bar.html', | ||||||||||||||||||||||||||||
| 613 | ); | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | $normalizer->remove_duplicate_slashes; | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | print $normalizer->url; # http://www.example.com/foo/bar.html | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | =cut | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | sub remove_duplicate_slashes { | ||||||||||||||||||||||||||||
| 622 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_duplicate_slashes: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | 5 | ▶ | if ( my $URI = $self->URI ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 625 | 5 | my $path = $URI->path; | |||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | 5 | $path =~ s,/+,/,g; | |||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | 5 | $URI->path( $path ); | |||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | 5 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | =head2 remove_social_query_parameters | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | Removes query parameters that are used for "social tracking." | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | For example, a lot of newspapers posts links to their articles on Twitter, | ||||||||||||||||||||||||||||
| 640 | and adds a lot of (for us) "noise" in the URL so that they are able to | ||||||||||||||||||||||||||||
| 641 | track the number of users clicking on that specific URL. This method | ||||||||||||||||||||||||||||
| 642 | attempts to remove those query parameters. | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | Example: | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 647 | url => 'http://www.example.com/?utm_campaign=SomeCampaignId', | ||||||||||||||||||||||||||||
| 648 | ); | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | print $normalize->url; # 'http://www.example.com/' | ||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||
| 652 | Default social query parameters are: | ||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | =over 4 | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | =item * C<ncid> | ||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||
| 658 | =item * C<utm_campaign> | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | =item * C<utm_medium> | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | =item * C<utm_source> | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | =back | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | You can override these default values when creating the URL::Normalize | ||||||||||||||||||||||||||||
| 667 | object: | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 670 | url => 'http://www.example.com/', | ||||||||||||||||||||||||||||
| 671 | social_query_params => [ 'your', 'list' ], | ||||||||||||||||||||||||||||
| 672 | ); | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | You can also choose to add parameters after the URL::Normalize object | ||||||||||||||||||||||||||||
| 675 | has been created: | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | my $normalizer = URL::Normalize->new( | ||||||||||||||||||||||||||||
| 678 | url => 'http://www.example.com/', | ||||||||||||||||||||||||||||
| 679 | ); | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | push( @{$normalizer->social_query_params}, 'QueryParam' ); | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | =cut | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | sub remove_social_query_parameters { | ||||||||||||||||||||||||||||
| 686 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_social_query_parameters: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | 2 | ▶ | if ( my $URI = $self->URI ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 689 | 2 | foreach ( @{$self->social_query_params} ) { | |||||||||||||||||||||||||||
| 690 | 8 | $URI->query_param_delete( $_ ); | |||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | 2 | $self->_set_url( $URI->as_string ); | |||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||
| 695 | } | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | =over 4 | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | =item * L<URI> | ||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||
| 703 | =item * L<URI::URL> | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | =item * L<URI::QueryParam> | ||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||
| 707 | =item * L<RFC 3986: Uniform Resource Indentifier|http://tools.ietf.org/html/rfc3986> | ||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||
| 709 | =item * L<Wikipedia: URL normalization|http://en.wikipedia.org/wiki/URL_normalization> | ||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | =back | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | Tore Aursand, C<< <toreau at gmail.com> >> | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | =head1 BUGS | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | Please report any bugs or feature requests to the web interface at L<https://rt.cpan.org/Dist/Display.html?Name=URL-Normalize> | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | perldoc URL::Normalize | ||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||
| 727 | You can also look for information at: | ||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||
| 729 | =over 4 | ||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||
| 731 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | L<http://annocpan.org/dist/URL-Normalize> | ||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | L<http://cpanratings.perl.org/d/URL-Normalize> | ||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||
| 739 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | L<http://search.cpan.org/dist/URL-Normalize/> | ||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||
| 743 | =back | ||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||
| 745 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | The MIT License (MIT) | ||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||
| 749 | Copyright (c) 2012-2015 Tore Aursand | ||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||
| 751 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||||||||||||||||||||||||
| 752 | of this software and associated documentation files (the "Software"), to deal | ||||||||||||||||||||||||||||
| 753 | in the Software without restriction, including without limitation the rights | ||||||||||||||||||||||||||||
| 754 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||||||||||||||||||||
| 755 | copies of the Software, and to permit persons to whom the Software is | ||||||||||||||||||||||||||||
| 756 | furnished to do so, subject to the following conditions: | ||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | The above copyright notice and this permission notice shall be included in all | ||||||||||||||||||||||||||||
| 759 | copies or substantial portions of the Software. | ||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||||||||||||||||||
| 762 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||||||||||||||||||||||
| 763 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||||||||||||||||||||||||
| 764 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||||||||||||||||||||
| 765 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||||||||||||||||||||||||
| 766 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||||||||||||||||||||||||
| 767 | SOFTWARE. | ||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||
| 769 | =cut | ||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | 12 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | 12 | 1; # End of URL::Normalize | |||||||||||||||||||||||||||