| 1 | package Data::FormValidator::URI; | ||||||||||
| 2 | |||||||||||
| 3 | ############################################################################### | ||||||||||
| 4 | # Required inclusions. | ||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | ############################################################################### | ||||||||||
| 10 | # Export our methods. | ||||||||||
| 11 | 3 | ▶ | use base qw( Exporter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | our @EXPORT = qw( | |||||||||
| 13 | FV_uri_filter | ||||||||||
| 14 | FV_uri | ||||||||||
| 15 | ); | ||||||||||
| 16 | |||||||||||
| 17 | ############################################################################### | ||||||||||
| 18 | # Version number. | ||||||||||
| 19 | 3 | 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 | 5 | ▶ | sub FV_uri_filter { | ||||||||
|
Subroutine
FV_uri_filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | 5 | my %opts = @_; | |||||||||
| 33 | |||||||||||
| 34 | 5 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 35 | 5 | my $val = shift; | |||||||||
| 36 | |||||||||||
| 37 | # Add default scheme if one was not provided in the URI. | ||||||||||
| 38 | 5 | ▶ | if ($opts{default_scheme}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 2 | ▶ | unless ($val =~ m{^\s*\w+://}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 1 | $val = $opts{default_scheme} . "://" . $val; | |||||||||
| 41 | } | ||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | # Correct typos in "://" | ||||||||||
| 45 | 5 | ▶ | if ($val =~ m{^\s*(\w+):/(\w.*)$}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | $val = join '://', $1, $2; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # Canonicalize the URI | ||||||||||
| 50 | { | ||||||||||
| 51 | 5 | my $uri = URI->new($val); | |||||||||
| 52 | 5 | ▶ | $val = $uri->canonical if ($uri); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 5 | return $val; | |||||||||
| 56 | 5 | }; | |||||||||
| 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 | 8 | ▶ | sub FV_uri { | ||||||||
|
Subroutine
FV_uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 8 | my %opts = @_; | |||||||||
| 70 | |||||||||||
| 71 | 8 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
| |||||||||||
| 72 | 8 | my $dfv = shift; | |||||||||
| 73 | 8 | my $val = shift; | |||||||||
| 74 | |||||||||||
| 75 | 8 | $dfv->name_this($dfv->get_current_constraint_field); | |||||||||
| 76 | |||||||||||
| 77 | 8 | my $uri = URI->new($val); | |||||||||
| 78 | |||||||||||
| 79 | # Fail if its not a valid URI at all | ||||||||||
| 80 | 8 | ▶ | return 0 unless ($uri); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | |||||||||||
| 82 | # URI must have a scheme | ||||||||||
| 83 | 8 | my $scheme = $uri->scheme; | |||||||||
| 84 | 8 | ▶ | return 0 unless $scheme; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | |||||||||||
| 86 | # Check list of supported schemes | ||||||||||
| 87 | 7 | ▶ | if ($opts{schemes}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 2 | ▶ | return 0 unless (grep { $_ eq $scheme } @{$opts{schemes}}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | # Check for embedeed user info | ||||||||||
| 92 | 6 | ▶ | unless ($opts{allow_userinfo}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 5 | ▶ | return 0 if ($uri->userinfo); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | # Check for valid hostname | ||||||||||
| 97 | 5 | ▶ | if ($opts{hostcheck}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 2 | ▶ | return 0 unless ($uri->can('host')); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | |||||||||||
| 100 | 2 | my $host = $uri->host; | |||||||||
| 101 | 2 | ▶ | return 0 unless ($host); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | |||||||||||
| 103 | 2 | my @bits = gethostbyname($host); | |||||||||
| 104 | 2 | ▶ | return 0 unless (@bits); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | # Looks good! | ||||||||||
| 108 | 4 | return 1; | |||||||||
| 109 | 8 | }; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 3 | 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<Data::FormValidator>, 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 <cpan@howlingfrog.com> | ||||||||||
| 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<Data::FormValidator> | ||||||||||
| 210 | |||||||||||
| 211 | =item L<URI> | ||||||||||
| 212 | |||||||||||
| 213 | =back | ||||||||||
| 214 | |||||||||||
| 215 | =cut | ||||||||||