blib/lib/MIME/Types.pm

filter: Statementstmt 91.6% 143 / 156 Branchbran 72.0% 62 / 86 Conditioncond 45.8% 11 / 24 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 78.5% 22 / 28 Podpod 86.6% 13 / 15 totaltotal 78.5% 253 / 322 SCAR 44.5
CC
61
Cov
81%
_read_db
22.6
types
12.0
by_mediatype
8.1
CRAP 85.7
« blib/lib/MIME/Type.pm ↑ Summary lib/MIME/Type.pm »
1 # Copyrights 1999-2022 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of distribution MIME::Types. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package MIME::Types;
10 4 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 $VERSION = '2.24';
12  
13  
14 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use MIME::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 4 my %typedb;
23 4 ▶sub new(@) { (bless {}, shift)->init( {@_} ) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
24  
25 sub init($)
26 4 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.1
Pod undocumented
27 4 ▶ keys %typedb or $self->_read_db($args);
Branch: 1 branch
BranchTrueFalseTotal
keys %typedb or $self->_read_db($args) 4 0 4
28 4 $self;
29 }
30  
31 sub _read_db($)
32 4 ▶{ my ($self, $args) = @_;
Subroutine _read_db: called CC=18 SCAR=31.2
33 4 my $skip_extensions = $args->{skip_extensions};
34 4 my $only_complete = $args->{only_complete};
35 4 my $only_iana = $args->{only_iana};
36  
37 my $db = $ENV{PERL_MIME_TYPE_DB}
38 || $args->{db_file}
39 4 ▶ || File::Spec->catfile(dirname(__FILE__), 'types.db');
Condition33%2/6
$ENV{'PERL_MIME_TYPE_DB'} || $args->{'db_file'} || 'File::Spec'->catfile(dirname(__FILE__), 'types.db')
l!l&&r!l&&!r
040
$ENV{"PERL_MIME_TYPE_DB"} or $args->{"db_file"}
l!l&&r!l&&!r
004
MC/DC0%0/3
$ENV{'PERL_MIME_TYPE_DB'} || $args->{'db_file'} || 'File::Spec'->catfile(dirname(__FILE__), 'types.db')
$ENV{"PERL_MIME_TYPE_DB"} $args->{"db_file"} 'File::Spec'->catfile(dirname(__FILE__), 'types.db')
Truth table25%1/4
A or B || C
A= $ENV{"PERL_MIME_TYPE_DB"}B= $args->{"db_file"}C= 'File::Spec'->catfile(dirname(__FILE__), 'types.db')
ABCresult
1XX1
01X1
0011
0000
40  
41 4 local *DB;
42 4 ▶ open DB, '<:encoding(utf8)', $db
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open DB, "<:encoding(utf8)", $db or die "cannot open type database in ${db}: $!\n" 0 4 4
43 or die "cannot open type database in $db: $!\n";
44  
45 4 while(1)
46 192 { my $header = <DB>;
47 192 ▶ defined $header or last;
Branch: 1 branch
BranchTrueFalseTotal
defined $header or last 4 188 192
48 188 chomp $header;
49  
50 # This logic is entangled with the bin/collect_types script
51 188 my ($count, $major, $is_iana, $has_ext) = split /\:/, $header;
52 188 ▶ my $skip_section = $major eq 'EXTENSIONS' ? $skip_extensions
Branch: 1 branch
BranchTrueFalseTotal
$major eq 'EXTENSIONS' ? : 4 184 188
Condition33%3/9
$only_iana and not $is_iana or $only_complete and not $has_ext
l!l&&r!l&&!r
00184
$only_iana and not $is_iana
!ll&&!rl&&r
18400
$only_complete and not $has_ext
!ll&&!rl&&r
18400
MC/DC0%0/4
$only_iana and not $is_iana or $only_complete and not $has_ext
$only_iana not $is_iana $only_complete not $has_ext
Truth table14%1/7
A and not(B) or C and not(D)
A= $only_ianaB= not $is_ianaC= $only_completeD= not $has_ext
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
53 : (($only_iana && !$is_iana) || ($only_complete && !$has_ext));
54  
55 #warn "Skipping section $header\n" if $skip_section;
56 188 (my $section = $major) =~ s/^x-//;
57 188 ▶ if($major eq 'EXTENSIONS')
Branch: 1 branch
BranchTrueFalseTotal
if ($major eq 'EXTENSIONS') { } 4 184 188
58 4 { local $_;
59 4 ▶ while(<DB>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DB)) 9016 0 9016
60 9016 ▶ { last if m/^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 4 9012 9016
61 9012 ▶ next if $skip_section;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_section 0 9012 9012
62 9012 chomp;
63 9012 ▶ $typedb{$section}{$1} = $2 if m/(.*);(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if /(.*);(.*)/ 9012 0 9012
64 }
65 }
66 else
67 184 { local $_;
68 184 ▶ while(<DB>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DB)) 12456 0 12456
69 12456 ▶ { last if m/^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 184 12272 12456
70 12272 ▶ next if $skip_section;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_section 0 12272 12272
71 12272 chomp;
72 12272 ▶ $typedb{$section}{$1} = "$major/$_" if m/^(?:x-)?([^;]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(?:x-)?([^;]+)/ 12272 0 12272
73 }
74 }
75 }
76  
77 4 close DB;
78 }
79  
80 # Catalyst-Plugin-Static-Simple uses it :(
81 ▶sub create_type_index {}
Subroutine create_type_index: not called CC=1 SCAR=0.0
Pod undocumented
82  
83 #-------------------------------------------
84  
85 sub type($)
86 4159 ▶{ my $spec = lc $_[1];
Subroutine type: called CC=6 SCAR=18.4
Pod documented
87 4159 ▶ $spec = 'text/plain' if $spec eq 'text'; # old mailers
Branch: 1 branch
BranchTrueFalseTotal
if $spec eq "text" 0 4159 4159
88  
89 4159 ▶ $spec =~ m!^(?:x\-)?([^/]+)/(?:x-)?(.*)!
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ m[^(?:x\-)?([^/]+)/(?:x-)?(.*)] or return 0 4159 4159
90 or return;
91  
92 4159 ▶ my $section = $typedb{$1} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $typedb{$1} or return 0 4159 4159
93 4159 ▶ my $record = $section->{$2} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $record = $section->{$2} or return 0 4159 4159
94 4159 ▶ return $record if ref $record; # already extended
Branch: 1 branch
BranchTrueFalseTotal
if ref $record 2079 2080 4159
95  
96 2080 my $simple = $2;
97 2080 my ($type, $ext, $enc) = split m/\;/, $record;
98 2080 my $os = undef; # XXX TODO
99  
100 2080 $section->{$simple} = MIME::Type->new
101 ( type => $type
102 , extensions => [split /\,/, $ext]
103 , encoding => $enc
104 , system => $os
105 );
106 }
107  
108  
109 sub mimeTypeOf($)
110 27 ▶{ my ($self, $name) = @_;
Subroutine mimeTypeOf: called CC=2 SCAR=6.9
Pod documented
111 27 (my $ext = lc $name) =~ s/.*\.//;
112 27 ▶ my $type = $typedb{EXTENSIONS}{$ext} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $typedb{"EXTENSIONS"}{$ext} or return 2 25 27
113 25 $self->type($type);
114 }
115  
116  
117 sub addType(@)
118 3 ▶{ my $self = shift;
Subroutine addType: called CC=3 SCAR=11.0
Pod documented
119  
120 3 foreach my $type (@_)
121 3 { my ($major, $minor) = split m!/!, $type->simplified;
122 3 $typedb{$major}{$minor} = $type;
123 3 $typedb{EXTENSIONS}{$_} = $type for $type->extensions;
124 }
125 3 $self;
126 }
127  
128  
129 sub types()
130 0 ▶{ my $self = shift;
Subroutine types: not called CC=3 SCAR=24.8
Pod documented
131 0 my @types;
132 0 foreach my $section (keys %typedb)
133 0 ▶ { next if $section eq 'EXTENSIONS';
Branch: 1 branch
BranchTrueFalseTotal
if $section eq "EXTENSIONS" 0 0 0
134 push @types, map $_->type("$section/$_"),
135 0 sort keys %{$typedb{$section}};
136 }
137 0 @types;
138 }
139  
140  
141 sub listTypes()
142 6 ▶{ my $self = shift;
Subroutine listTypes: called CC=6 SCAR=17.9
Pod documented
143 6 my @types;
144 6 foreach my $section (keys %typedb)
145 138 ▶ { next if $section eq 'EXTENSIONS';
Branch: 1 branch
BranchTrueFalseTotal
if $section eq "EXTENSIONS" 6 132 138
146 132 foreach my $sub (sort keys %{$typedb{$section}})
147 18246 { my $record = $typedb{$section}{$sub};
148 18246 ▶ push @types, ref $record ? $record->type
Branch: 2 branches
BranchTrueFalseTotal
ref $record ? : 8253 9993 18246
$record =~ /^([^;]+)/ ? : 9993 0 9993
149 : $record =~ m/^([^;]+)/ ? $1 : die;
150 }
151 }
152 6 @types;
153 }
154  
155  
156 0 ▶sub extensions { keys %{$typedb{EXTENSIONS}} }
Subroutine extensions: not called CC=1 SCAR=6.9
Pod documented
157 0 ▶sub _MojoExtTable() {$typedb{EXTENSIONS}}
Subroutine _MojoExtTable: not called CC=1 SCAR=6.9
158  
159 #-------------
160  
161 sub httpAccept($)
162 7 ▶{ my $self = shift;
Subroutine httpAccept: called CC=7 SCAR=19.5
Pod documented
163 7 my @listed;
164  
165 7 foreach (split /\,\s*/, shift)
166 {
167 17 ▶ m!^ ([a-zA-Z0-9-]+ | \*) / ( [a-zA-Z0-9+-]+ | \* )
Branch: 1 branch
BranchTrueFalseTotal
m[^ ([a-zA-Z0-9-]+ | \*) / ( [a-zA-Z0-9+-]+ | \* ) \s* (?: \;\s*q\=\s* ([0-9]+(?:\.[0-9]*)?) \s* )? (\;.* | ) $ ]x or next 0 17 17
168 \s* (?: \;\s*q\=\s* ([0-9]+(?:\.[0-9]*)?) \s* )?
169 (\;.* | )
170 $ !x or next;
171  
172 17 my $mime = "$1/$2$4";
173 17 ▶ my $q = defined $3 ? $3 : 1; # q, default=1
Branch: 1 branch
BranchTrueFalseTotal
defined $3 ? : 2 15 17
174  
175 # most complex first
176 17 ▶ $q += $4 ? +0.01 : $1 eq '*' ? -0.02 : $2 eq '*' ? -0.01 : 0;
Branch: 3 branches
BranchTrueFalseTotal
$4 ? : 0 17 17
$1 eq '*' ? : 1 16 17
$2 eq '*' ? : 1 15 16
177  
178 # keep order
179 17 $q -= @listed*0.0001;
180  
181 17 push @listed, [ $mime => $q ];
182 }
183 7 map $_->[0], sort {$b->[1] <=> $a->[1]} @listed;
184 }
185  
186  
187 sub httpAcceptBest($@)
188 8 ▶{ my $self = shift;
Subroutine httpAcceptBest: called CC=6 SCAR=18.5
Pod documented
189 8 ▶ my @accept = ref $_[0] eq 'ARRAY' ? @{(shift)} : $self->httpAccept(shift);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 4 4 8
190 8 my $match;
191  
192 8 foreach my $acc (@accept)
193 10 { $acc =~ s/\s*\;.*//; # remove attributes
194 17 ▶ my $m = $acc !~ s#/\*$## ? first { $_->equals($acc) } @_
Subroutine __ANON__: called CC=1 SCAR=0.0
195 : $acc eq '*' ? $_[0] # $acc eq */*
196 10 ▶ : first { $_->mediaType eq $acc } @_;
Subroutine __ANON__: not called CC=1 SCAR=2.6
Branch: 2 branches
BranchTrueFalseTotal
!($acc =~ s[/\*$][]) ? : 10 0 10
$acc eq '*' ? : 0 0 0
197 10 ▶ return $m if defined $m;
Branch: 1 branch
BranchTrueFalseTotal
if defined $m 7 3 10
198 }
199  
200 1 ();
201 }
202  
203  
204 sub httpAcceptSelect($@)
205 4 ▶{ my ($self, $accept) = (shift, shift);
Subroutine httpAcceptSelect: called CC=7 SCAR=19.6
Pod documented
206 4 ▶ my $fns = !@_ ? return () : ref $_[0] eq 'ARRAY' ? shift : [@_];
Branch: 2 branches
BranchTrueFalseTotal
!@_ ? : 0 4 4
ref $_[0] eq 'ARRAY' ? : 4 0 4
207  
208 4 ▶ unless(defined $accept)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $accept) 1 3 4
209 1 { my $fn = $fns->[0];
210 1 return ($fn, $self->mimeTypeOf($fn));
211 }
212  
213 # create mapping type -> filename
214 3 my (%have, @have);
215 3 foreach my $fn (@$fns)
216 6 ▶ { my $type = $self->mimeTypeOf($fn) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->mimeTypeOf($fn) or next 0 6 6
217 6 $have{$type->simplified} = $fn;
218 6 push @have, $type;
219 }
220  
221 3 my $type = $self->httpAcceptBest($accept, @have);
222 3 ▶ defined $type ? ($have{$type}, $type) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 2 1 3
223 }
224  
225 #-------------------------------------------
226 # OLD INTERFACE (version 0.06 and lower)
227  
228  
229 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
230 4 our @EXPORT_OK = qw(by_suffix by_mediatype import_mime_types);
231  
232  
233 4 my $mime_types;
234  
235 sub by_suffix($)
236 7 ▶{ my $filename = shift;
Subroutine by_suffix: called CC=4 SCAR=13.9
Pod documented
237 7 ▶ $mime_types ||= MIME::Types->new;
Condition66%2/3
$mime_types ||= "MIME::Types"->new
l!l&&r!l&&!r
610
MC/DC0%0/2
$mime_types ||= "MIME::Types"->new
$mime_types "MIME::Types"->new
Truth table66%2/3
A ||= B
A= $mime_typesB= "MIME::Types"->new
ABresult
1X1
011
000
238 7 my $mime = $mime_types->mimeTypeOf($filename);
239  
240 7 ▶ my @data = defined $mime ? ($mime->type, $mime->encoding) : ('','');
Branch: 1 branch
BranchTrueFalseTotal
defined $mime ? : 5 2 7
241 7 ▶ wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 4 7
242 }
243  
244  
245 sub by_mediatype($)
246 8 ▶{ my $type = shift;
Subroutine by_mediatype: called CC=8 SCAR=20.9
Pod documented
247 8 ▶ $mime_types ||= MIME::Types->new;
Condition33%1/3
$mime_types ||= "MIME::Types"->new
l!l&&r!l&&!r
800
MC/DC0%0/2
$mime_types ||= "MIME::Types"->new
$mime_types "MIME::Types"->new
Truth table33%1/3
A ||= B
A= $mime_typesB= "MIME::Types"->new
ABresult
1X1
011
000
248  
249 8 my @found;
250 8 ▶ if(!ref $type && index($type, '/') >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $type and index($type, '/') != -1) { } 2 6 8
Condition100%3/3
not ref $type and index($type, "/") != -1
!ll&&!rl&&r
152
MC/DC100%2/2
not ref $type and index($type, "/") != -1
not ref $type index($type, "/") != -1
Truth table100%3/3
not(A) and B
A= not ref $typeB= index($type, "/") != -1
ABresult
0X0
100
111
251 2 { my $mime = $mime_types->type($type);
252 2 ▶ @found = $mime if $mime;
Branch: 1 branch
BranchTrueFalseTotal
if $mime 2 0 2
253 }
254 else
255 6 ▶ { my $search = ref $type eq 'Regexp' ? $type : qr/$type/i;
Branch: 1 branch
BranchTrueFalseTotal
ref $type eq 'Regexp' ? : 1 5 6
256 6 @found = map $mime_types->type($_),
257 grep $_ =~ $search,
258 $mime_types->listTypes;
259 }
260  
261 8 my @data;
262 8 foreach my $mime (@found)
263 4126 { push @data, map [$_, $mime->type, $mime->encoding],
264 $mime->extensions;
265 }
266  
267 8 ▶ wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
268 }
269  
270  
271 sub import_mime_types($)
272 0 ▶{ my $filename = shift;
Subroutine import_mime_types: not called CC=1 SCAR=0.6
Pod documented
273 4 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 0 croak <<'CROAK';
275 import_mime_types is not supported anymore: if you have types to add
276 please send them to the author.
277 CROAK
278 }
279  
280 4 1;
« blib/lib/MIME/Type.pm ↑ Summary lib/MIME/Type.pm »