| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package URI::Normalize; |
|
2
|
|
|
|
|
|
|
$URI::Normalize::VERSION = '0.002'; |
|
3
|
1
|
|
|
1
|
|
30676
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
37
|
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
49
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use base 'Exporter'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
129
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
7
|
use URI; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
29
|
|
|
9
|
1
|
|
|
1
|
|
4
|
use Scalar::Util qw( blessed ); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
554
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw( normalize_uri remove_dot_segments ); |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# ABSTRACT: Normalize URIs according to RFC 3986 |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub normalize_uri { |
|
17
|
4
|
|
|
4
|
1
|
1237
|
my $uri = shift; |
|
18
|
|
|
|
|
|
|
|
|
19
|
4
|
50
|
|
|
|
15
|
die '$uri is a required parameter to normalize_uri' unless defined $uri; |
|
20
|
|
|
|
|
|
|
|
|
21
|
4
|
50
|
33
|
|
|
49
|
$uri = URI->new($uri) unless blessed($uri) and $uri->isa('URI'); |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Start by placing the URI in canonical form |
|
24
|
4
|
|
|
|
|
9566
|
$uri = $uri->canonical; |
|
25
|
4
|
|
|
|
|
938
|
$uri = remove_dot_segments($uri); |
|
26
|
|
|
|
|
|
|
|
|
27
|
4
|
|
|
|
|
22
|
return $uri; |
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub remove_dot_segments { |
|
32
|
12
|
|
|
12
|
1
|
3569
|
my $uri = shift; |
|
33
|
|
|
|
|
|
|
|
|
34
|
12
|
50
|
|
|
|
28
|
die '$uri is a required parameter to normalize_uri' unless defined $uri; |
|
35
|
|
|
|
|
|
|
|
|
36
|
12
|
100
|
66
|
|
|
68
|
if (not (blessed($uri) and $uri->isa('URI'))) { |
|
37
|
8
|
|
|
|
|
21
|
$uri = URI->new($uri); |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
else { |
|
40
|
4
|
|
|
|
|
10
|
$uri = $uri->clone; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
12
|
|
|
|
|
469
|
my $input = $uri->path; |
|
44
|
12
|
|
|
|
|
104
|
my $output = ''; |
|
45
|
|
|
|
|
|
|
|
|
46
|
12
|
|
|
|
|
28
|
while (length $input > 0) { |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# A. ^./ and ^../ are deleted |
|
49
|
39
|
50
|
|
|
|
74
|
next if $input =~ s{ ^ [.][.]? / }{}x; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# B. ^/./ and ^/.$ are deleted |
|
52
|
39
|
100
|
|
|
|
89
|
next if $input =~ s{ ^ /[.] (?: / | $ ) }{/}x; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# C. ^/../ and ^/..$ remove last element of output and delete |
|
55
|
35
|
100
|
|
|
|
63
|
if ($input =~ s{ ^ /[.][.] (?: / | $ ) }{/}x) { |
|
56
|
10
|
|
|
|
|
17
|
my $segstart = rindex($output, '/'); |
|
57
|
10
|
100
|
|
|
|
25
|
next unless $segstart >= 0; |
|
58
|
|
|
|
|
|
|
|
|
59
|
5
|
|
|
|
|
8
|
my $segend = length($output) - $segstart; |
|
60
|
5
|
|
|
|
|
9
|
substr $output, $segstart, $segend, ''; |
|
61
|
5
|
|
|
|
|
11
|
next; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# D. ^.$ and ^..$ are deleted |
|
65
|
25
|
50
|
|
|
|
41
|
next if $input =~ s{ ^ [.][.]? $ }{}x; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# E. move ^/?[^/]* to output |
|
68
|
25
|
|
|
|
|
68
|
$input =~ s{ (/? [^/]*) }{}x; |
|
69
|
25
|
|
|
|
|
59
|
$output .= $1; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
12
|
|
|
|
|
28
|
$uri->path($output); |
|
73
|
12
|
|
|
|
|
298
|
return $uri; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |