SSL/PublicSuffix.pm

filter: Statementstmt 14.5% 23 / 158 Branchbran 2.0% 2 / 98 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 26.3% 5 / 19 Podpod 80.0% 4 / 5 totaltotal 11.4% 34 / 296 CC 56 SCAR 55.5
CC
56
Cov
9%
public_suffix
600.0
update_self_from_url
210.0
from_file
132.0
CRAP 256.8
« SSL/Utils.pm ↑ Summary SSL/Intercept.pm »
1  
2 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package IO::Socket::SSL::PublicSuffix;
5 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # for updates
8 26 ▶use constant URL => 'http://publicsuffix.org/list/effective_tld_names.dat';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 IO::Socket::SSL::PublicSuffix - provide access to Mozillas list of effective TLD names
13  
14 =head1 SYNOPSIS
15  
16 # use builtin default
17 use IO::Socket::SSL::PublicSuffix;
18 $ps = IO::Socket::SSL::PublicSuffix->default;
19  
20 # load from string
21 $ps = IO::Socket::SSL::PublicSuffix->from_string("*.uk\n*");
22  
23 # load from file or file handle
24 $ps = IO::Socket::SSL::PublicSuffix->from_file($filename);
25 $ps = IO::Socket::SSL::PublicSuffix->from_file(\*STDIN);
26  
27  
28 # --- string in -> string out
29 # $rest -> whatever.host
30 # $tld -> co.uk
31 my ($rest,$tld) = $ps->public_suffix('whatever.host.co.uk');
32 my $tld = $ps->public_suffix('whatever.host.co.uk');
33  
34 # $root_domain -> host.co.uk
35 my $root_domain = $ps->public_suffix('whatever.host.co.uk', 1);
36  
37 # --- array in -> array out
38 # $rest -> [qw(whatever host)]
39 # $tld -> [qw(co uk)]
40 my ($rest,$tld) = $ps->public_suffix([qw(whatever host co uk)]);
41  
42 ----
43  
44 # To update this file with the current list:
45 perl -MIO::Socket::SSL::PublicSuffix -e 'IO::Socket::SSL::PublicSuffix::update_self_from_url()'
46  
47  
48  
49 =head1 DESCRIPTION
50  
51 This module uses the list of effective top level domain names from the mozilla
52 project to determine the public top level domain for a given hostname.
53  
54 =head2 Method
55  
56 =over 4
57  
58 =item class->default(%args)
59  
60 Returns object with builtin default.
61 C<min_suffix> can be given in C<%args> to specify the minimal suffix, default
62 is 1.
63  
64 =item class->from_string(string,%args)
65  
66 Returns object with configuration from string.
67 See method C<default> for C<%args>.
68  
69 =item class->from_file( file name| file handle, %args )
70  
71 Returns object with configuration from file or file handle.
72 See method C<default> for C<%args>.
73  
74 =item $self->public_suffix( $host|\@host, [ $add ] )
75  
76 In array context the function returns the non-tld part and the tld part of the
77 given hostname, in scalar context only the tld part.
78 It adds C<$add> parts of the non-tld part to the tld, e.g. with C<$add=1> it
79 will return the root domain.
80  
81 If there were no explicit matches against the public suffix configuration it
82 will fall back to a suffix of length 1.
83  
84 The function accepts a string or an array-ref (e.g. host split by C<.>). In the
85 first case it will return string(s), in the latter case array-ref(s).
86  
87 International hostnames or labels can be in ASCII (IDNA form starting with
88 C<xn-->) or unicode. In the latter case an IDNA handling library like
89 L<Net::IDN:::Encode>, L<Net::LibIDN> or recent versions of L<URI> need to be
90 installed.
91  
92 =item ($self|class)->can_idn
93  
94 Returns true if IDN support is available.
95  
96 =back
97  
98 =head1 FILES
99  
100 http://publicsuffix.org/list/effective_tld_names.dat
101  
102 =head1 SEE ALSO
103  
104 Domain::PublicSuffix, Mozilla::PublicSuffix
105  
106 =head1 BUGS
107  
108 Q: Why yet another module, we already have L<Domain::PublicSuffix> and
109 L<Mozilla::PublicSuffix>.
110 A: Because the public suffix data change more often than these modules do,
111 IO::Socket::SSL needs this list and it is more easy this way to keep it
112 up-to-date.
113  
114  
115 =head1 AUTHOR
116  
117 Steffen Ullrich
118  
119 =cut
120  
121  
122 BEGIN {
123 26 ▶ if ( eval {
Subroutine BEGIN: called CC=5 SCAR=27.4
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { require URI::_idna; defined &URI::_idna::encode and defined &URI::_idna::decode } }) { } 26 0 26
elsif (eval { do { require Net::IDN::Encode } }) { } 0 0 0
elsif (eval { do { require Net::LibIDN; require Encode } }) { } 0 0 0
124 26 require URI::_idna;
125 26 ▶ defined &URI::_idna::encode && defined &URI::_idna::decode
Branch: 1 branch
BranchTrueFalseTotal
defined &URI::_idna::encode and defined &URI::_idna::decode 26 0 26
126 }) {
127 26 *idn_to_ascii = \&URI::_idna::encode;
128 26 *idn_to_unicode = \&URI::_idna::decode;
129 26 ▶ *can_idn = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
130 0 } elsif ( eval { require Net::IDN::Encode } ) {
131 0 *idn_to_ascii = \&Net::IDN::Encode::domain_to_ascii;
132 0 *idn_to_unicode = \&Net::IDN::Encode::domain_to_unicode;
133 0 ▶ *can_idn = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 } elsif ( eval { require Net::LibIDN; require Encode } ) {
135 # Net::LibIDN does not use utf-8 flag and expects raw data
136 *idn_to_ascii = sub {
137 0 ▶ Net::LibIDN::idn_to_ascii(Encode::encode('utf-8',$_[0]),'utf-8');
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 },
139 *idn_to_unicode = sub {
140 0 ▶ Encode::decode('utf-8',Net::LibIDN::idn_to_unicode($_[0],'utf-8'));
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 },
142 0 ▶ *can_idn = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 } else {
144 0 ▶ *idn_to_ascii = sub { croak "idn_to_ascii(@_) - no IDNA library installed" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 ▶ *idn_to_unicode = sub { croak "idn_to_unicode(@_) - no IDNA library installed" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 ▶ *can_idn = sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 }
148 }
149  
150 {
151 26 my %default;
152 sub default {
153 0 ▶ my (undef,%args) = @_;
Subroutine default: not called CC=4 SCAR=30.0
Pod documented
154 0 my $min_suffix = delete $args{min_suffix};
155 0 ▶ $min_suffix = 1 if ! defined $min_suffix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min_suffix 0 0 0
156 0 ▶ %args and die "unknown args: ".join(" ",sort keys %args);
Branch: 1 branch
BranchTrueFalseTotal
%args and die 'unknown args: ' . join(" ", sort(keys %args)) 0 0 0
157 0 ▶ return $default{$min_suffix} ||= shift->from_string(_default_data(),
Condition0%0/3
$default{$min_suffix} ||= (shift())->from_string(_default_data(), "min_suffix", $min_suffix)
l!l&&r!l&&!r
000
MC/DC0%0/2
$default{$min_suffix} ||= (shift())->from_string(_default_data(), "min_suffix", $min_suffix)
$default{$min_suffix} (shift())->from_string(_default_data(), "min_suffix", $min_suffix)
Truth table0%0/3
A ||= B
A= $default{$min_suffix}B= (shift())->from_string(_default_data(), "min_suffix", $min_suffix)
ABresult
1X1
011
000
158 min_suffix => $min_suffix);
159 }
160 }
161  
162 26 sub from_string {
163 0 ▶ my $class = shift;
Subroutine from_string: not called CC=1 SCAR=6.9
Pod documented
164 0 my $data = shift;
165 0 open( my $fh,'<', \$data );
166 0 return $class->from_file($fh,@_);
167 }
168  
169 sub from_file {
170 0 ▶ my ($class,$file,%args) = @_;
Subroutine from_file: not called CC=11 SCAR=48.8
Pod documented
171 0 my $min_suffix = delete $args{min_suffix};
172 0 ▶ $min_suffix = 1 if ! defined $min_suffix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min_suffix 0 0 0
173 0 ▶ %args and die "unknown args: ".join(" ",sort keys %args);
Branch: 1 branch
BranchTrueFalseTotal
%args and die 'unknown args: ' . join(" ", sort(keys %args)) 0 0 0
174  
175 0 my $fh;
176 0 ▶ if ( ref($file)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
elsif (not open($fh, '<', $file)) { } 0 0 0
177 0 $fh = $file
178 } elsif ( ! open($fh,'<',$file)) {
179 0 die "failed to open $file: $!";
180 }
181 0 my %tree;
182 0 local $/ = "\n";
183 0 ▶ while ( my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
184 0 $line =~s{//.*}{};
185 0 $line =~s{\s+$}{};
186 0 ▶ $line eq '' and next;
Branch: 1 branch
BranchTrueFalseTotal
$line eq "" and next 0 0 0
187 0 my $p = \%tree;
188 0 ▶ $line = idn_to_ascii($line) if $line !~m{\A[\x00-\x7f]*\Z};
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A[\x00-\x7f]*\Z/ 0 0 0
189 0 my $not = $line =~s{^!}{};
190 0 my @path = split(m{\.},$line);
191 0 for(reverse @path) {
192 0 ▶ $p = $p->{$_} ||= {}
Condition0%0/2
$p->{$_} ||= {}
l!l
00
MC/DC0%0/1
$p->{$_} ||= {}
$p->{$_}
Truth table0%0/2
A
A= $p->{$_}
Aresult
11
00
193 }
194 0 ▶ $p->{'\0'} = $not ? -1:1;
Branch: 1 branch
BranchTrueFalseTotal
$not ? : 0 0 0
195 }
196 0 return bless {
197 tree => \%tree,
198 min_suffix => $min_suffix
199 },$class;
200 }
201  
202  
203 sub public_suffix {
204 0 ▶ my ($self,$name,$add) = @_;
Subroutine public_suffix: not called CC=24 SCAR=64.0
Pod documented
205 0 my $want; # [a]rray, [s]tring, [u]nicode-string
206 0 ▶ if ( ref($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) { } 0 0 0
207 0 $want = 'a';
208 0 $name = [ @$name ]; # don't change input
209 } else {
210 0 ▶ return if ! defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
211 0 ▶ if ( $name !~m{\A[\x00-\x7f]*\Z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name =~ /\A[\x00-\x7f]*\Z/) { } 0 0 0
212 0 $name = idn_to_ascii($name);
213 0 $want = 'u';
214 } else {
215 0 $want = 's';
216 }
217 0 $name = lc($name);
218 0 $name =~s{\.$}{};
219 0 $name = [ $name =~m{([^.]+)}g ];
220 }
221 0 ▶ @$name or return;
Branch: 1 branch
BranchTrueFalseTotal
@$name or return 0 0 0
222 0 $_ = lc($_) for(@$name);
223  
224 0 my (%wild,%host,%xcept,@stack,$choices);
225 0 my $p = $self->{tree};
226 0 ▶ for( my $i=0; $i<@$name; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$name) 0 0 0
227 0 $choices = [];
228 0 ▶ if ( my $px = $p->{ $name->[$#$name-$i] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $px = $$p{$$name[$name->$#* - $i]}) 0 0 0
229 # name match, continue with next path element
230 0 push @$choices,$px;
231 0 ▶ if ( my $end = $px->{'\0'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $end = $px->{"\\0"}) 0 0 0
232 0 ▶ ( $end>0 ? \%host : \%xcept )->{$i+1} = $end;
Branch: 1 branch
BranchTrueFalseTotal
$end > 0 ? : 0 0 0
233 }
234 }
235 0 ▶ if ( my $px = $p->{'*'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $px = $p->{"*"}) 0 0 0
236 # wildcard match, continue with next path element
237 0 push @$choices,$px;
238 0 ▶ if ( my $end = $px->{'\0'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $end = $px->{"\\0"}) 0 0 0
239 0 ▶ ( $end>0 ? \%wild : \%xcept )->{$i+1} = $end;
Branch: 1 branch
BranchTrueFalseTotal
$end > 0 ? : 0 0 0
240 }
241 }
242  
243  
244 next_choice:
245 0 ▶ if ( @$choices ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$choices) 0 0 0
246 0 $p = shift(@$choices);
247 0 ▶ push @stack, [ $choices, $i ] if @$choices;
Branch: 1 branch
BranchTrueFalseTotal
if @$choices 0 0 0
248 0 next; # go deeper
249 }
250  
251 # backtrack
252 0 ▶ @stack or last;
Branch: 1 branch
BranchTrueFalseTotal
@stack or last 0 0 0
253 0 ($choices,$i) = @{ pop(@stack) };
254 0 goto next_choice;
255 }
256  
257 #warn Dumper([\%wild,\%host,\%xcept]); use Data::Dumper;
258  
259  
260 # remove all exceptions from wildcards
261 0 ▶ delete @wild{ keys %xcept } if %xcept;
Branch: 1 branch
BranchTrueFalseTotal
if %xcept 0 0 0
262 # get longest match
263 0 my ($len) = sort { $b <=> $a } (
264 0 keys(%wild), keys(%host), map { $_-1 } keys(%xcept));
265 # if we have no matches use a minimum of min_suffix
266 0 ▶ $len = $self->{min_suffix} if ! defined $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
267 0 ▶ $len += $add if $add;
Branch: 1 branch
BranchTrueFalseTotal
if $add 0 0 0
268 0 my $suffix;
269 0 ▶ if ( $len < @$name ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len < @$name) { } 0 0 0
elsif ($len > 0) { } 0 0 0
270 0 $suffix = [ splice( @$name, -$len, $len ) ];
271 } elsif ( $len > 0 ) {
272 0 $suffix = $name;
273 0 $name = []
274 } else {
275 0 $suffix = []
276 }
277  
278 0 ▶ if ( $want ne 'a' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want ne "a") 0 0 0
279 0 $suffix = join('.',@$suffix);
280 0 $name = join('.',@$name);
281 0 ▶ if ( $want eq 'u' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want eq "u") 0 0 0
282 0 $suffix = idn_to_unicode($suffix);
283 0 $name = idn_to_unicode($name);
284 }
285 }
286  
287 0 ▶ return wantarray ? ($name,$suffix):$suffix;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
288 }
289  
290  
291 {
292 26 my $data;
293 sub _default_data {
294 0 ▶ if ( ! defined $data ) {
Subroutine _default_data: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
295 0 $data = do { local $/; <DATA> };
296 0 ▶ $data =~s{^// ===END ICANN DOMAINS.*}{}ms
Branch: 1 branch
BranchTrueFalseTotal
$data =~ s[^// ===END ICANN DOMAINS.*][]ms or die "cannot find END ICANN DOMAINS" 0 0 0
297 or die "cannot find END ICANN DOMAINS";
298 }
299 0 return $data;
300 }
301 }
302  
303 26 sub update_self_from_url {
304 0 ▶ my $url = shift || URL();
Subroutine update_self_from_url: not called CC=14 SCAR=53.5
Pod undocumented
Condition0%0/2
shift() || 'http://publicsuffix.org/list/effective_tld_names.dat'
l!l
00
MC/DC0%0/1
shift() || 'http://publicsuffix.org/list/effective_tld_names.dat'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
305 0 my $dst = __FILE__;
306 0 ▶ -w $dst or die "cannot write $dst";
Branch: 1 branch
BranchTrueFalseTotal
-w $dst or die "cannot write $dst" 0 0 0
307 0 ▶ open( my $fh,'<',$dst ) or die "open $dst: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $dst or die "open ${dst}: $!" 0 0 0
308 0 my $code = '';
309 0 local $/ = "\n";
310 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
311 0 $code .= $_;
312 0 ▶ $code =~m{\A__DATA__\r?\n\Z} and last;
Branch: 1 branch
BranchTrueFalseTotal
$code =~ /\A__DATA__\r?\n\Z/ and last 0 0 0
313 }
314 0 close($fh);
315  
316 0 require LWP::UserAgent;
317 0 ▶ my $resp = LWP::UserAgent->new->get($url)
Branch: 1 branch
BranchTrueFalseTotal
my $resp = "LWP::UserAgent"->new->get($url) or die "no response from $url" 0 0 0
318 or die "no response from $url";
319 0 ▶ die "no success url=$url code=".$resp->code." ".$resp->message
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
320 if ! $resp->is_success;
321 0 my $content = $resp->decoded_content;
322 0 ▶ while ( $content =~m{(.*\n)}g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /(.*\n)/g) 0 0 0
323 0 my $line = $1;
324 0 ▶ if ( $line =~m{\S} && $line !~m{\A\s*//} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\S/ and not $line =~ m[\A\s*//]) { } 0 0 0
Condition0%0/3
$line =~ /\S/ and not $line =~ m[\A\s*//]
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /\S/ and not $line =~ m[\A\s*//]
$line =~ /\S/ not $line =~ m[\A\s*//]
Truth table0%0/3
A and not(B)
A= $line =~ /\S/B= not $line =~ m[\A\s*//]
ABresult
0X0
100
111
325 0 $line =~s{//.*}{};
326 0 $line =~s{\s+$}{};
327 0 ▶ $line eq '' and next;
Branch: 1 branch
BranchTrueFalseTotal
$line eq "" and next 0 0 0
328 0 ▶ if ( $line !~m{\A[\x00-\x7f]+\Z} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /\A[\x00-\x7f]+\Z/) 0 0 0
329 0 $line = idn_to_ascii($line);
330 }
331 0 $code .= "$line\n";
332 } else {
333 0 $code .= "$line";
334 }
335 }
336  
337 0 ▶ open( $fh,'>:utf8',$dst ) or die "open $dst: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:utf8", $dst or die "open ${dst}: $!" 0 0 0
338 0 print $fh $code;
339 }
340  
341 26 1;
342 __DATA__
« SSL/Utils.pm ↑ Summary SSL/Intercept.pm »