PublicSuffix.pm

filter: Statementstmt 84.4% 119 / 141 Branchbran 81.2% 39 / 48 Conditioncond 78.2% 18 / 23 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 78.9% 15 / 19 Podpod 33.3% 2 / 6 totaltotal 80.0% 201 / 251 CC 38 SCAR 20.0
CC
38
Cov
83%
_load_data
16.1
get_root_domain
11.3
_parse_data_to_tree
8.0
CRAP 7.4
↑ Summary PublicSuffix/Default.pm »
1 package Domain::PublicSuffix;
2 10 $Domain::PublicSuffix::VERSION = '0.21';
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Domain::PublicSuffix::Default ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Net::IDN::Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 __PACKAGE__->mk_accessors(qw/
12 use_default
13 data_file
14 allow_unlisted_tld
15 domain_allow_underscore
16 tld_tree
17 error
18 root_domain
19 tld
20 suffix
21 /);
22  
23 =head1 NAME
24  
25 Domain::PublicSuffix - Parse a domain down to root
26  
27 =head1 SYNOPSIS
28  
29 use Domain::PublicSuffix;
30  
31 my $suffix = Domain::PublicSuffix->new({
32 'data_file' => '/tmp/effective_tld_names.dat'
33 });
34 my $root = $suffix->get_root_domain('www.google.com');
35 # $root now contains "google.com"
36  
37 $root = $suffix->get_root_domain('www.google.co.uk');
38 # $root now contains google.co.uk
39  
40 my $suf = $suffix->suffix();
41 # $suf now contains co.uk
42  
43 my $tld = $suffix->tld();
44 # $tld now contains uk
45  
46 =head1 DESCRIPTION
47  
48 This module utilizes the "effective_tld_names.dat" provided by Mozilla as a way
49 to effectively reduce a fully qualified domain name down to the absolute root.
50 The Mozilla PublicSuffix file is an open source, fully documented format that
51 shows absolute root TLDs, primarily for Mozilla's browser products to be able
52 to determine how far a cookie's security boundaries go.
53  
54 This module will attempt to search etc directories in /usr/share/publicsuffix,
55 /usr, /usr/local, and /opt/local for the effective_tld_names.dat file. If a file
56 is not found, a default file is loaded from Domain::PublicSuffix::Default, which
57 is current at the time of the module's release. You can override the data file
58 path by giving the new() method a 'data_file' argument.
59  
60 When creating a new PublicSuffix object, the module will load the data file as
61 specified, and use the internal structure to parse each domain sent to the
62 get_root_domain method. To re-parse the file, you must destroy and create a new
63 object, or execute the _parse_data_file method directly, though that is not
64 recommended.
65  
66 =head1 PUBLIC ACCESSORS
67  
68 =over 4
69  
70 =item error()
71  
72 On unsuccessful parse, contains a human-readable error string.
73  
74 =item suffix()
75  
76 Returns the effective tld of the last parsed domain. For the domain
77 'google.co.uk', this would return 'co.uk'.
78  
79 =item tld()
80  
81 Returns the true DNS tld of the last parsed domain. For the domain
82 'google.co.uk', this would return 'uk'.
83  
84 =back
85  
86 =cut
87  
88 =head1 PUBLIC METHODS
89  
90 =over 4
91  
92 =item new(\%arguments)
93  
94 Instantiate a PublicSuffix object. It is best to instantiate an object
95 and continue calling get_root_domain instead of continually recreating the
96 object, as the data file is read and parsed on instantiation.
97  
98 Can take a hashref of arguments:
99  
100 =over 4
101  
102 =item data_file
103  
104 A fully qualified path, to override the effective_tld_names.dat file.
105  
106 =item use_default
107  
108 Use the provided publicsuffix file, do not search for any other files.
109  
110 =item domain_allow_underscore
111  
112 A flag to indicate that underscores should be allowed in hostnames
113 (contra to the RFCs). Default: undef.
114  
115 =item allow_unlisted_tld
116  
117 A flag to indicate that unlisted TLDs should be passed through. This follows
118 the spec as listed on publicsuffix.org, but is not how this module works by
119 default, or before 0.16. Default: undef
120  
121 =back
122  
123 =back
124  
125 =cut
126  
127 11 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
128 11 my ( $class, @args ) = @_;
129  
130 11 my $self = $class->SUPER::new(@args);
131  
132 # Compatibility fix
133 11 ▶ if ( $args[0] and ref($args[0]) eq 'HASH' and $args[0]->{'dataFile'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] and ref $args[0] eq "HASH" and $args[0]{"dataFile"}) 1 10 11
Condition83%5/6
$args[0] and ref $args[0] eq "HASH" and $args[0]{"dataFile"}
!ll&&!rl&&r
191
$args[0] and ref $args[0] eq "HASH"
!ll&&!rl&&r
1010
MC/DC66%2/3
$args[0] and ref $args[0] eq "HASH" and $args[0]{"dataFile"}
$args[0] ref $args[0] eq "HASH" $args[0]{"dataFile"}
Truth table75%3/4
A and B and C
A= $args[0]B= ref $args[0] eq "HASH"C= $args[0]{"dataFile"}
ABCresult
0XX0
10X0
1100
1111
134 1 $self->data_file( $args[0]->{'dataFile'} );
135 }
136  
137 11 $self->tld_tree($self->_parse_data_to_tree());
138  
139 11 return $self;
140 }
141  
142 =over 4
143  
144 =item get_root_domain( $domain )
145  
146 Given a fully qualified domain name, return the parsed root domain name.
147 Returns undefined if an error occurs parsing the given domain, and fills
148 the error accessor with a human-readable error string.
149  
150 =back
151  
152 =cut
153  
154 103 ▶sub get_root_domain {
Subroutine get_root_domain: called CC=11 SCAR=24.3
Pod documented
155 103 my ( $self, $inbound ) = @_;
156  
157 103 ▶ unless ($inbound) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inbound) 1 102 103
158 1 $self->error('No input');
159 1 return;
160 }
161  
162 102 my $domain = lc($inbound);
163  
164 # Clear meta properties
165 102 foreach ( qw/tld suffix root_domain error/ ) {
166 408 undef( $self->{$_} );
167 }
168  
169 # Check if domain is valid
170 102 ▶ unless ( _validate_domain($domain) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_validate_domain($domain)) 15 87 102
171 15 $self->error('Malformed domain');
172 15 return;
173 }
174  
175 87 my @domain_array = split(/\./, $domain);
176 87 my $tld = pop(@domain_array);
177 87 ▶ unless ( defined $self->tld_tree->{$tld} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->tld_tree->{$tld}) 3 84 87
178 3 ▶ if ( $self->allow_unlisted_tld ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_unlisted_tld) { } 3 0 3
179 3 $self->tld($tld);
180 3 $self->suffix($tld);
181 3 ▶ if ( my $next = pop(@domain_array) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = pop @domain_array) { } 3 0 3
182 3 $self->root_domain( join( '.', $next, $tld ) );
183 } else {
184 0 $self->root_domain($tld);
185 }
186 3 return $self->root_domain;
187 } else {
188 0 $self->error('Invalid TLD');
189 0 return;
190 }
191 }
192  
193 84 $self->tld($tld);
194 84 my $raw_suffix = $self->get_suffix_for_domain($domain);
195  
196 # Leave if we still haven't found a suffix
197 84 ▶ if ( !$raw_suffix ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw_suffix) 0 84 84
198 0 $self->error('Domain not valid');
199 0 return;
200 }
201  
202 84 my $suffix = $raw_suffix;
203 84 $suffix =~ s/!//g;
204 84 $self->suffix($suffix);
205  
206 # Check if we're left with just a suffix
207 84 ▶ if ( $raw_suffix !~ /!/ and $self->suffix eq $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $raw_suffix =~ /!/ and $self->suffix eq $domain) 14 70 84
Condition100%3/3
not $raw_suffix =~ /!/ and $self->suffix eq $domain
!ll&&!rl&&r
66414
MC/DC100%2/2
not $raw_suffix =~ /!/ and $self->suffix eq $domain
not $raw_suffix =~ /!/ $self->suffix eq $domain
Truth table100%3/3
not(A) and B
A= not $raw_suffix =~ /!/B= $self->suffix eq $domain
ABresult
0X0
100
111
208 14 $self->error('Domain is already a suffix');
209 14 return;
210 }
211  
212 # Generate root domain using suffix
213 70 ▶ if ($raw_suffix =~ /!/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_suffix =~ /!/) { } 6 64 70
214 # Exception suffixes are also domains
215 6 $self->root_domain($suffix);
216 } else {
217 64 my $root_domain = $domain;
218 64 $root_domain =~ s/^.*\.(.*?\.$suffix)\.?$/$1/;
219 64 $self->root_domain($root_domain);
220 }
221  
222 70 return $self->root_domain;
223 }
224  
225 84 ▶sub get_suffix_for_domain {
Subroutine get_suffix_for_domain: called CC=5 SCAR=16.1
Pod undocumented
226 84 my ( $self, $domain ) = @_;
227  
228 84 my @labels = split( /\./, $domain );
229 84 my $point = $self->tld_tree;
230 84 my @suffix;
231 84 ▶ while ( my $label = pop(@labels) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = pop @labels) 216 15 231
232 # If there is a wildcard here, it is a suffix, except for !exceptions
233 # Theoretically, there would be further processing here for .*.
234 # wildcards, but those have not existed before in the list, so saving
235 # the work until it actually happens.
236 216 ▶ if ( $point->{'*'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($point->{'*'}) { } 16 200 216
elsif (not $point->{$label}) { } 63 137 200
237 16 my $exception = '!' . $label;
238 16 ▶ if ( $point->{$exception} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($point->{$exception}) 6 10 16
239 6 push( @suffix, $exception );
240 6 last;
241 }
242 } elsif (!$point->{$label}) {
243 # If we run out of rules at this point, the root is just below here
244 63 last;
245 }
246
247 147 push( @suffix, $label );
248 147 $point = $point->{$label};
249 }
250 84 return join('.', reverse(@suffix));
251 }
252  
253 11 ▶sub _load_data {
Subroutine _load_data: called CC=9 SCAR=27.8
254 11 my ($self) = @_;
255  
256 11 my $data_stream_ref;
257  
258 # Find an effective_tld_names.dat file
259 my @tld_lines;
260 11 my $dat;
261 11 ▶ if ( $self->use_default ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->use_default) { } 8 3 11
elsif ($self->data_file and -e $self->data_file) { } 0 3 3
262 8 $data_stream_ref = Domain::PublicSuffix::Default::retrieve();
263 ▶ } elsif ( $self->data_file and -e $self->data_file ) {
Condition66%2/3
$self->data_file and -e $self->data_file
!ll&&!rl&&r
120
MC/DC0%0/2
$self->data_file and -e $self->data_file
$self->data_file -e $self->data_file
Truth table66%2/3
A and B
A= $self->data_fileB= -e $self->data_file
ABresult
0X0
100
111
264 0 ▶ open( $dat, '<:encoding(UTF-8)', $self->data_file )
Branch: 1 branch
BranchTrueFalseTotal
open $dat, "<:encoding(UTF-8)", $self->data_file or die q[Cannot open '] . $self->data_file . q[': ] . $! 0 0 0
265 or die "Cannot open \'" . $self->data_file . "\': " . $!;
266 0 @tld_lines = <$dat>;
267 0 close($dat);
268 0 $data_stream_ref = \@tld_lines;
269  
270 } else {
271 3 my @paths = (
272 File::Spec->catdir(File::Spec->rootdir, qw/ usr share publicsuffix /),
273 File::Spec->catdir(File::Spec->rootdir, qw/ etc /),
274 File::Spec->catdir(File::Spec->rootdir, qw/ usr etc /),
275 File::Spec->catdir(File::Spec->rootdir, qw/ usr local etc /),
276 File::Spec->catdir(File::Spec->rootdir, qw/ opt local etc /),
277 );
278 3 foreach my $path (@paths) {
279 15 $path = File::Spec->catfile( $path, "effective_tld_names.dat" );
280 15 ▶ if ( -e $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) 0 15 15
281 0 ▶ open( $dat, '<:encoding(UTF-8)', $path )
Branch: 1 branch
BranchTrueFalseTotal
open $dat, "<:encoding(UTF-8)", $path or die q[Cannot open '] . $path . q[': ] . $! 0 0 0
282 or die "Cannot open \'" . $path . "\': " . $!;
283 0 @tld_lines = <$dat>;
284 0 close($dat);
285 0 $data_stream_ref = \@tld_lines;
286 0 last;
287 }
288 }
289 }
290  
291 # If we haven't found one, load the default
292 11 ▶ unless ( defined $data_stream_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data_stream_ref) 3 8 11
293 3 $data_stream_ref = Domain::PublicSuffix::Default::retrieve();
294 }
295  
296 11 return $data_stream_ref;
297 }
298  
299 11 ▶sub _parse_data_to_tree {
Subroutine _parse_data_to_tree: called CC=8 SCAR=20.8
300 11 my ($self) = @_;
301  
302 11 my $data_stream_ref = $self->_load_data();
303 11 my $tree = {};
304  
305 11 foreach (@{$data_stream_ref}) {
306 173250 chomp;
307
308 # Remove comments, skip if full line comment, remove if inline comment
309 173250 ▶ next if ( /^\// or /^[ \t]*?$/ );
Branch: 1 branch
BranchTrueFalseTotal
if m[^/] or /^[ \t]*?$/ 65791 107459 173250
Condition100%3/3
m[^/] or /^[ \t]*?$/
l!l&&r!l&&!r
4370322088107459
MC/DC100%2/2
m[^/] or /^[ \t]*?$/
m[^/] /^[ \t]*?$/
Truth table100%3/3
A or B
A= m[^/]B= /^[ \t]*?$/
ABresult
1X1
011
000
310 107459 s/[\s\x{0085}\x{000A}\x{000C}\x{000D}\x{0020}].*//;
311  
312 # Parse both unicode and ASCII representations, if needed
313 107459 my @tlds = ($_);
314 107459 my $ascii = Net::IDN::Encode::domain_to_ascii($_);
315 107459 ▶ push( @tlds, $ascii ) if ( $_ ne $ascii );
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne $ascii 5060 102399 107459
316  
317 107459 foreach (@tlds) {
318 # Split domain and convert to a tree
319 112519 my @domain = split( /\./, $_ );
320 112519 my $previous = $tree;
321 112519 ▶ while (my $label = pop(@domain)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = pop @domain) 253682 112519 366201
322 253682 $label =~ s/\s.*//;
323 253682 ▶ $previous->{$label} ||= {};
Condition100%2/2
$previous->{$label} ||= {}
l!l
135652118030
MC/DC100%1/1
$previous->{$label} ||= {}
$previous->{$label}
Truth table100%2/2
A
A= $previous->{$label}
Aresult
11
00
324 253682 $previous = $previous->{$label};
325 }
326 }
327 }
328 11 return $tree;
329 }
330  
331 102 ▶sub _validate_domain {
Subroutine _validate_domain: called CC=2 SCAR=7.1
332 102 my ($domain) = @_;
333  
334 102 ▶ return ( _validate_length($domain) and _validate_multiple_segments($domain) );
Condition66%2/3
_validate_length($domain) && _validate_multiple_segments($domain)
!ll&&!rl&&r
01587
MC/DC50%1/2
_validate_length($domain) && _validate_multiple_segments($domain)
_validate_length($domain) _validate_multiple_segments($domain)
Truth table66%2/3
A && B
A= _validate_length($domain)B= _validate_multiple_segments($domain)
ABresult
0X0
100
111
335 }
336  
337 # Domains must have more than one segment with length
338 102 ▶sub _validate_multiple_segments {
Subroutine _validate_multiple_segments: called CC=4 SCAR=13.9
339 102 my ($domain) = @_;
340  
341 102 my @segments = split( /\./, $domain );
342 102 ▶ return unless ( @segments > 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @segments > 1 11 91 102
343 91 foreach my $segment (@segments) {
344 269 ▶ return unless ( length($segment) > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless length $segment > 0 4 265 269
345 }
346 87 return 1;
347 }
348  
349 # Domains may not be more than 255 characters in length
350 102 ▶sub _validate_length {
Subroutine _validate_length: called CC=2 SCAR=7.6
351 102 my ($domain) = @_;
352  
353 102 my $length = length($domain);
354 102 ▶ return ( $length > 1 and $length <= 255 );
Condition33%1/3
$length > 1 && $length <= 255
!ll&&!rl&&r
00102
MC/DC0%0/2
$length > 1 && $length <= 255
$length > 1 $length <= 255
Truth table33%1/3
A && B
A= $length > 1B= $length <= 255
ABresult
0X0
100
111
355 }
356  
357 ### Compatibility
358  
359 0 ▶sub _parseDataFile {
Subroutine _parseDataFile: not called CC=1 SCAR=6.9
360 0 my ($self) = @_;
361  
362 0 $self->tld_tree($self->_parse_data_to_tree());
363 }
364 1 ▶sub getRootDomain {
Subroutine getRootDomain: called CC=1 SCAR=0.0
Pod undocumented
365 1 my ( $self, $domain ) = @_;
366  
367 1 return $self->get_root_domain($domain);
368 }
369  
370 0 ▶sub _validateDomain {
Subroutine _validateDomain: not called CC=1 SCAR=6.9
371 0 my ($self, $domain) = @_;
372  
373 0 return $self->_validate_domain($domain);
374 }
375  
376 0 ▶sub dataFile {
Subroutine dataFile: not called CC=1 SCAR=6.9
Pod undocumented
377 0 my ( $self, $data_file ) = @_;
378  
379 0 return $self->data_file($data_file);
380 }
381  
382 0 ▶sub rootDomain {
Subroutine rootDomain: not called CC=1 SCAR=6.9
Pod undocumented
383 0 my ( $self, $root_domain ) = @_;
384  
385 0 return $self->root_domain($root_domain);
386 }
387  
388 =head1 SEE ALSO
389  
390 =over 4
391  
392 =item * GitHub
393  
394 L<http://www.github.com/nmelnick/Domain-PublicSuffix>
395  
396 =item * Current List:
397  
398 L<http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1> [mxr.mozilla.org]
399  
400 =item * Mozilla Documentation:
401  
402 L<http://wiki.mozilla.org/Gecko:TLD_Service>
403  
404 =item * Public Info Site:
405  
406 L<http://publicsuffix.org/>
407  
408 =back
409  
410 =head1 BUGS
411  
412 Please report any bugs or feature requests to C<bug-domain-publicsuffix at rt.cpan.org>,
413 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Domain-PublicSuffix>.
414 I will be notified, and then you'll automatically be notified of progress on
415 your bug as I make changes.
416  
417 =head1 SUPPORT
418  
419 You can find documentation for this module with the perldoc command.
420  
421 perldoc Domain::PublicSuffix
422  
423 You can also look for information at:
424  
425 =over 4
426  
427 =item * RT: CPAN's request tracker
428  
429 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Domain-PublicSuffix>
430  
431 =item * AnnoCPAN: Annotated CPAN documentation
432  
433 L<http://annocpan.org/dist/Domain-PublicSuffix>
434  
435 =item * CPAN Ratings
436  
437 L<http://cpanratings.perl.org/d/Domain-PublicSuffix>
438  
439 =item * Search CPAN
440  
441 L<http://search.cpan.org/dist/Domain-PublicSuffix>
442  
443 =back
444  
445 =head1 CONTRIBUTORS
446  
447 dkg: Daniel Kahn Gillmor
448  
449 gavinc: Gavin Carr
450  
451 jwieland: Jason Wieland
452  
453 =head1 COPYRIGHT & LICENSE
454  
455 Copyright 2008-2020 Nicholas Melnick, C<nick at abstractwankery.com>.
456  
457 This program is free software; you can redistribute it and/or modify it
458 under the same terms as Perl itself.
459  
460 =cut
461  
462 10 1;
↑ Summary PublicSuffix/Default.pm »