line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::FormValidator::URI; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
############################################################################### |
4
|
|
|
|
|
|
|
# Required inclusions. |
5
|
2
|
|
|
2
|
|
199483
|
use strict; |
|
2
|
|
|
|
|
24
|
|
|
2
|
|
|
|
|
75
|
|
6
|
2
|
|
|
2
|
|
26
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
76
|
|
7
|
2
|
|
|
2
|
|
1168
|
use URI; |
|
2
|
|
|
|
|
13856
|
|
|
2
|
|
|
|
|
67
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
############################################################################### |
10
|
|
|
|
|
|
|
# Export our methods. |
11
|
2
|
|
|
2
|
|
13
|
use base qw( Exporter ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1429
|
|
12
|
|
|
|
|
|
|
our @EXPORT = qw( |
13
|
|
|
|
|
|
|
FV_uri_filter |
14
|
|
|
|
|
|
|
FV_uri |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
############################################################################### |
18
|
|
|
|
|
|
|
# Version number. |
19
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
############################################################################### |
22
|
|
|
|
|
|
|
# Subroutine: FV_uri_filter(%opts) |
23
|
|
|
|
|
|
|
############################################################################### |
24
|
|
|
|
|
|
|
# Filter method which cleans up the given value as best it can and returns |
25
|
|
|
|
|
|
|
# something that looks like a URI. |
26
|
|
|
|
|
|
|
# |
27
|
|
|
|
|
|
|
# The filtered URI will be canonicalized, and common typos will be corrected. |
28
|
|
|
|
|
|
|
# |
29
|
|
|
|
|
|
|
# Supported options: |
30
|
|
|
|
|
|
|
# default_scheme - Default URI scheme to use, if none was provided in the URI |
31
|
|
|
|
|
|
|
sub FV_uri_filter { |
32
|
5
|
|
|
5
|
1
|
2684
|
my %opts = @_; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
return sub { |
35
|
5
|
|
|
5
|
|
23
|
my $val = shift; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Add default scheme if one was not provided in the URI. |
38
|
5
|
100
|
|
|
|
16
|
if ($opts{default_scheme}) { |
39
|
2
|
100
|
|
|
|
12
|
unless ($val =~ m{^\s*\w+://}) { |
40
|
1
|
|
|
|
|
4
|
$val = $opts{default_scheme} . "://" . $val; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Correct typos in "://" |
45
|
5
|
100
|
|
|
|
30
|
if ($val =~ m{^\s*(\w+):/(\w.*)$}) { |
46
|
1
|
|
|
|
|
5
|
$val = join '://', $1, $2; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Canonicalize the URI |
50
|
|
|
|
|
|
|
{ |
51
|
5
|
|
|
|
|
7
|
my $uri = URI->new($val); |
|
5
|
|
|
|
|
19
|
|
52
|
5
|
50
|
|
|
|
11173
|
$val = $uri->canonical if ($uri); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
5
|
|
|
|
|
803
|
return $val; |
56
|
5
|
|
|
|
|
32
|
}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
############################################################################### |
60
|
|
|
|
|
|
|
# Subroutine: FV_uri(%opts) |
61
|
|
|
|
|
|
|
############################################################################### |
62
|
|
|
|
|
|
|
# Constraint method, which ensures that we have a valid URI. |
63
|
|
|
|
|
|
|
# |
64
|
|
|
|
|
|
|
# Supported options: |
65
|
|
|
|
|
|
|
# schemes - list-ref of valid schemes |
66
|
|
|
|
|
|
|
# hostcheck - host exists in URI and resolves as a valid host? (default off) |
67
|
|
|
|
|
|
|
# allow_userinfo - allow user info in URI (default off) |
68
|
|
|
|
|
|
|
sub FV_uri { |
69
|
8
|
|
|
8
|
1
|
4419
|
my %opts = @_; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
return sub { |
72
|
8
|
|
|
8
|
|
4313
|
my $dfv = shift; |
73
|
8
|
|
|
|
|
23
|
my $val = shift; |
74
|
|
|
|
|
|
|
|
75
|
8
|
|
|
|
|
32
|
$dfv->name_this($dfv->get_current_constraint_field); |
76
|
|
|
|
|
|
|
|
77
|
8
|
|
|
|
|
117
|
my $uri = URI->new($val); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Fail if its not a valid URI at all |
80
|
8
|
50
|
|
|
|
11349
|
return 0 unless ($uri); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# URI must have a scheme |
83
|
8
|
|
|
|
|
113
|
my $scheme = $uri->scheme; |
84
|
8
|
100
|
|
|
|
202
|
return 0 unless $scheme; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Check list of supported schemes |
87
|
7
|
100
|
|
|
|
29
|
if ($opts{schemes}) { |
88
|
2
|
100
|
|
|
|
3
|
return 0 unless (grep { $_ eq $scheme } @{$opts{schemes}}); |
|
4
|
|
|
|
|
19
|
|
|
2
|
|
|
|
|
6
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Check for embedeed user info |
92
|
6
|
100
|
|
|
|
24
|
unless ($opts{allow_userinfo}) { |
93
|
5
|
100
|
|
|
|
23
|
return 0 if ($uri->userinfo); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Check for valid hostname |
97
|
5
|
100
|
|
|
|
147
|
if ($opts{hostcheck}) { |
98
|
2
|
50
|
|
|
|
18
|
return 0 unless ($uri->can('host')); |
99
|
|
|
|
|
|
|
|
100
|
2
|
|
|
|
|
9
|
my $host = $uri->host; |
101
|
2
|
50
|
|
|
|
86
|
return 0 unless ($host); |
102
|
|
|
|
|
|
|
|
103
|
2
|
|
|
|
|
51906
|
my @bits = gethostbyname($host); |
104
|
2
|
100
|
|
|
|
56
|
return 0 unless (@bits); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Looks good! |
108
|
4
|
|
|
|
|
28
|
return 1; |
109
|
8
|
|
|
|
|
109
|
}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=for stopwords hostcheck canonicalized |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 NAME |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Data::FormValidator::URI - URI constraint/filter for Data::FormValidator |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 SYNOPSIS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
use Data::FormValidator; |
123
|
|
|
|
|
|
|
use Data::FormValidator::URI; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my $res = Data::FormValidator->check( |
126
|
|
|
|
|
|
|
{ |
127
|
|
|
|
|
|
|
website => 'http://www.example.com/path/to/some/resource.html', |
128
|
|
|
|
|
|
|
}, |
129
|
|
|
|
|
|
|
{ |
130
|
|
|
|
|
|
|
required => [qw( website )], |
131
|
|
|
|
|
|
|
field_filters => { |
132
|
|
|
|
|
|
|
website => FV_uri_filter(default => 'http'), |
133
|
|
|
|
|
|
|
}, |
134
|
|
|
|
|
|
|
constraint_methods => { |
135
|
|
|
|
|
|
|
website => FV_uri( |
136
|
|
|
|
|
|
|
schemes => [qw( http https )], |
137
|
|
|
|
|
|
|
hostcheck => 1, |
138
|
|
|
|
|
|
|
allow_userinfo => 0, |
139
|
|
|
|
|
|
|
), |
140
|
|
|
|
|
|
|
}, |
141
|
|
|
|
|
|
|
}, |
142
|
|
|
|
|
|
|
); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 DESCRIPTION |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This module provides a filter and a constraint method for use with |
147
|
|
|
|
|
|
|
C, to help make it easier to valid URIs. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 METHODS |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=over |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item FV_uri_filter(%opts) |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Filter method which cleans up the given value as best it can and returns |
156
|
|
|
|
|
|
|
something that looks like a URI. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
The filtered URI will be canonicalized, and common typos will be corrected. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Supported options: |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=over |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item default_scheme |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Default URI scheme to use, if none was provided in the URI |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=back |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item FV_uri(%opts) |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Constraint method, which ensures that we have a valid URI. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Supported options: |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=over |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item schemes |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
list-ref of valid schemes |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item hostcheck |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
host exists in URI and resolves as a valid host? (default off) |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item allow_userinfo |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
allow user info in URI (default off) |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=back |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=back |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 AUTHOR |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Graham TerMarsch |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 COPYRIGHT |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Copyright (C) 2013, Graham TerMarsch. All Rights Reserved. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under the terms |
203
|
|
|
|
|
|
|
of the Artistic 2.0 license. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 SEE ALSO |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=over |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item L |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item L |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=back |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=cut |