MIME/Types.pm

filter: Statementstmt 91.5% 141 / 154 Branchbran 72.7% 64 / 88 Conditioncond 45.8% 11 / 24 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 77.7% 21 / 27 Podpod 86.6% 13 / 15 totaltotal 78.5% 252 / 321 CC 62 SCAR 22.1
CC
62
Cov
81%
_read_db
24.1
types
12.0
by_mediatype
8.1
CRAP 9.1
↑ Summary MojoX/MIME/Types.pm »
1 # This code is part of Perl distribution MIME-Types version 2.30.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 1999-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package MIME::Types;{
13 4 our $VERSION = '2.30';
14 }
15  
16  
17 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use MIME::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use File::Basename qw/dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #--------------------
26  
27 4 my %typedb;
28 4 ▶sub new(@) { (bless {}, shift)->init( {@_} ) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
29  
30 sub init($)
31 4 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.1
Pod undocumented
32 4 ▶ keys %typedb or $self->_read_db($args);
Branch: 1 branch
BranchTrueFalseTotal
keys %typedb or $self->_read_db($args) 0 4 4
33 4 $self;
34 }
35  
36 sub _read_db($)
37 4 ▶{ my ($self, $args) = @_;
Subroutine _read_db: called CC=18 SCAR=31.8
38 4 my $skip_extensions = $args->{skip_extensions};
39 4 my $only_complete = $args->{only_complete};
40 4 my $only_iana = $args->{only_iana};
41  
42 my $db = $ENV{PERL_MIME_TYPE_DB} || $args->{db_file} ||
43 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
44  
45 4 ▶ open my $dbh, '<:encoding(utf8)', $db
Branch: 1 branch
BranchTrueFalseTotal
open my $dbh, "<:encoding(utf8)", $db or die "cannot open type database in ${db}: $!\n" 4 0 4
46 or die "cannot open type database in $db: $!\n";
47  
48 4 while(1)
49 196 { my $header = $dbh->getline;
50 196 ▶ defined $header or last;
Branch: 1 branch
BranchTrueFalseTotal
defined $header or last 192 4 196
51 192 chomp $header;
52  
53 # This logic is entangled with the bin/collect_types script
54 192 my ($count, $major, $is_iana, $has_ext) = split /\:/, $header;
55 192 ▶ my $skip_section = $major eq 'EXTENSIONS' ? $skip_extensions
Branch: 1 branch
BranchTrueFalseTotal
$major eq 'EXTENSIONS' ? : 4 188 192
Condition33%3/9
$only_iana and not $is_iana or $only_complete and not $has_ext
l!l&&r!l&&!r
00188
$only_iana and not $is_iana
!ll&&!rl&&r
18800
$only_complete and not $has_ext
!ll&&!rl&&r
18800
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
56 : (($only_iana && !$is_iana) || ($only_complete && !$has_ext));
57  
58 192 (my $section = $major) =~ s/^x-//;
59 192 ▶ if($major eq 'EXTENSIONS')
Branch: 1 branch
BranchTrueFalseTotal
if ($major eq 'EXTENSIONS') { } 4 188 192
60 4 ▶ { while(my $line = $dbh->getline)
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $dbh->getline) 9368 0 9368
61 9368 ▶ { last if $line =~ m/^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 4 9364 9368
62 9364 ▶ next if $skip_section;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_section 0 9364 9364
63 9364 chomp $line;
64 9364 ▶ $typedb{$section}{$1} = $2 if $line =~ m/(.*);(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /(.*);(.*)/ 9364 0 9364
65 }
66 }
67 else
68 188 ▶ { while(my $line = $dbh->getline)
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $dbh->getline) 13204 0 13204
69 13204 ▶ { last if $line =~ m/^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 188 13016 13204
70 13016 ▶ next if $skip_section;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_section 0 13016 13016
71 13016 chomp $line;
72 13016 ▶ $typedb{$section}{$1} = "$major/$line" if $line =~ m/^(?:x-)?([^;]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^(?:x-)?([^;]+)/ 13016 0 13016
73 }
74 }
75 }
76  
77 4 $dbh->close;
78 }
79  
80 # Catalyst-Plugin-Static-Simple uses it :(
81 0 ▶sub create_type_index {}
Subroutine create_type_index: not called CC=1 SCAR=0.0
Pod undocumented
82  
83 #--------------------
84  
85 sub type($)
86 4385 ▶{ my $spec = lc $_[1];
Subroutine type: called CC=6 SCAR=18.4
Pod documented
87 4385 ▶ $spec = 'text/plain' if $spec eq 'text'; # old mailers
Branch: 1 branch
BranchTrueFalseTotal
if $spec eq "text" 0 4385 4385
88  
89 4385 ▶ $spec =~ m!^(?:x\-)?([^/]+)/(?:x-)?(.*)!
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ m[^(?:x\-)?([^/]+)/(?:x-)?(.*)] or return 4385 0 4385
90 or return;
91  
92 4385 ▶ my $section = $typedb{$1} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $typedb{$1} or return 4385 0 4385
93 4385 ▶ my $record = $section->{$2} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $record = $section->{$2} or return 4385 0 4385
94 4385 ▶ return $record if ref $record; # already extended
Branch: 1 branch
BranchTrueFalseTotal
if ref $record 2192 2193 4385
95  
96 2193 my $simple = $2;
97 2193 my ($type, $ext, $enc, $char) = split m/\;/, $record;
98 2193 my $os = undef; # XXX TODO
99  
100 2193 $section->{$simple} = MIME::Type->new(
101 type => $type,
102 extensions => [split /\,/, $ext],
103 encoding => $enc,
104 system => $os,
105 charset => $char,
106 );
107 }
108  
109  
110 sub mimeTypeOf($)
111 29 ▶{ my $self = shift;
Subroutine mimeTypeOf: called CC=3 SCAR=11.0
Pod documented
112 29 my $ext = lc(shift);
113  
114 # Extensions may contains multiple dots (rare)
115 29 while(1)
116 51 ▶ { if(my $type = $typedb{EXTENSIONS}{$ext})
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $typedb{"EXTENSIONS"}{$ext}) 27 24 51
117 27 { return $self->type($type);
118 }
119 24 ▶ $ext =~ s/.*?\.// or last;
Branch: 1 branch
BranchTrueFalseTotal
$ext =~ s/.*?\.// or last 22 2 24
120 }
121  
122 2 undef;
123 }
124  
125  
126 sub addType(@)
127 3 ▶{ my $self = shift;
Subroutine addType: called CC=3 SCAR=11.0
Pod documented
128  
129 3 foreach my $type (@_)
130 3 { my ($major, $minor) = split m!/!, $type->simplified;
131 3 $typedb{$major}{$minor} = $type;
132 3 $typedb{EXTENSIONS}{$_} = $type for $type->extensions;
133 }
134 3 $self;
135 }
136  
137  
138 sub types()
139 0 ▶{ my $self = shift;
Subroutine types: not called CC=3 SCAR=24.8
Pod documented
140 0 my @types;
141 0 foreach my $section (keys %typedb)
142 0 ▶ { next if $section eq 'EXTENSIONS';
Branch: 1 branch
BranchTrueFalseTotal
if $section eq "EXTENSIONS" 0 0 0
143 0 push @types, map $_->type("$section/$_"), sort keys %{$typedb{$section}};
144 }
145 0 @types;
146 }
147  
148  
149 sub listTypes()
150 6 ▶{ my $self = shift;
Subroutine listTypes: called CC=6 SCAR=17.9
Pod documented
151 6 my @types;
152 6 foreach my $section (keys %typedb)
153 144 ▶ { next if $section eq 'EXTENSIONS';
Branch: 1 branch
BranchTrueFalseTotal
if $section eq "EXTENSIONS" 6 138 144
154 138 foreach my $sub (sort keys %{$typedb{$section}})
155 19320 { my $record = $typedb{$section}{$sub};
156 19320 ▶ push @types, ref $record ? $record->type : $record =~ m/^([^;]+)/ ? $1 : die;
Branch: 2 branches
BranchTrueFalseTotal
ref $record ? : 8701 10619 19320
$record =~ /^([^;]+)/ ? : 10619 0 10619
157 }
158 }
159 6 @types;
160 }
161  
162  
163 0 ▶sub extensions { keys %{$typedb{EXTENSIONS}} }
Subroutine extensions: not called CC=1 SCAR=6.9
Pod documented
164 0 ▶sub _MojoExtTable() {$typedb{EXTENSIONS}}
Subroutine _MojoExtTable: not called CC=1 SCAR=6.9
165  
166 #--------------------
167  
168 sub httpAccept($)
169 7 ▶{ my $self = shift;
Subroutine httpAccept: called CC=7 SCAR=19.5
Pod documented
170 7 my @listed;
171  
172 7 foreach (split /\,\s*/, shift)
173 {
174 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 17 0 17
175 \s* (?: \;\s*q\=\s* ([0-9]+(?:\.[0-9]*)?) \s* )?
176 (\;.* | )
177 $ !x or next;
178  
179 17 my $mime = "$1/$2$4";
180 17 ▶ my $q = defined $3 ? $3 : 1; # q, default=1
Branch: 1 branch
BranchTrueFalseTotal
defined $3 ? : 2 15 17
181  
182 # most complex first
183 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
184  
185 # keep order
186 17 $q -= @listed*0.0001;
187  
188 17 push @listed, [ $mime => $q ];
189 }
190 7 map $_->[0], sort {$b->[1] <=> $a->[1]} @listed;
191 }
192  
193  
194 sub httpAcceptBest($@)
195 8 ▶{ my $self = shift;
Subroutine httpAcceptBest: called CC=6 SCAR=18.5
Pod documented
196 8 ▶ my @accept = ref $_[0] eq 'ARRAY' ? @{(shift)} : $self->httpAccept(shift);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 4 4 8
197 8 my $match;
198  
199 8 foreach my $acc (@accept)
200 10 { $acc =~ s/\s*\;.*//; # remove attributes
201 17 ▶ my $m = $acc !~ s#/\*$## ? first { $_->equals($acc) } @_
Subroutine __ANON__: called CC=1 SCAR=0.0
202 : $acc eq '*' ? $_[0] # $acc eq */*
203 0 ▶ : 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
204 10 ▶ return $m if defined $m;
Branch: 1 branch
BranchTrueFalseTotal
if defined $m 7 3 10
205 }
206  
207 1 ();
208 }
209  
210  
211 sub httpAcceptSelect($@)
212 4 ▶{ my ($self, $accept) = (shift, shift);
Subroutine httpAcceptSelect: called CC=7 SCAR=19.6
Pod documented
213 4 ▶ my $fns = !@_ ? return () : ref $_[0] eq 'ARRAY' ? shift : [@_];
Branch: 2 branches
BranchTrueFalseTotal
!@_ ? : 0 4 4
ref $_[0] eq 'ARRAY' ? : 4 0 4
214  
215 4 ▶ unless(defined $accept)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $accept) 1 3 4
216 1 { my $fn = $fns->[0];
217 1 return ($fn, $self->mimeTypeOf($fn));
218 }
219  
220 # create mapping type -> filename
221 3 my (%have, @have);
222 3 foreach my $fn (@$fns)
223 6 ▶ { my $type = $self->mimeTypeOf($fn) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->mimeTypeOf($fn) or next 6 0 6
224 6 $have{$type->simplified} = $fn;
225 6 push @have, $type;
226 }
227  
228 3 my $type = $self->httpAcceptBest($accept, @have);
229 3 ▶ defined $type ? ($have{$type}, $type) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 2 1 3
230 }
231  
232 # OLD INTERFACE (version 0.06 and lower)
233  
234 #--------------------
235  
236 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
237 4 our @EXPORT_OK = qw(by_suffix by_mediatype import_mime_types);
238  
239  
240 4 my $mime_types;
241  
242 sub by_suffix($)
243 7 ▶{ my $filename = shift;
Subroutine by_suffix: called CC=4 SCAR=13.9
Pod documented
244 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
245 7 my $mime = $mime_types->mimeTypeOf($filename);
246  
247 7 ▶ my @data = defined $mime ? ($mime->type, $mime->encoding) : ('','');
Branch: 1 branch
BranchTrueFalseTotal
defined $mime ? : 5 2 7
248 7 ▶ wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 4 7
249 }
250  
251  
252 sub by_mediatype($)
253 8 ▶{ my $type = shift;
Subroutine by_mediatype: called CC=8 SCAR=20.9
Pod documented
254 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
255  
256 8 my @found;
257 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
258 2 { my $mime = $mime_types->type($type);
259 2 ▶ @found = $mime if $mime;
Branch: 1 branch
BranchTrueFalseTotal
if $mime 2 0 2
260 }
261 else
262 6 ▶ { my $search = ref $type eq 'Regexp' ? $type : qr/$type/i;
Branch: 1 branch
BranchTrueFalseTotal
ref $type eq 'Regexp' ? : 1 5 6
263 6 @found = map $mime_types->type($_), grep $_ =~ $search, $mime_types->listTypes;
264 }
265  
266 8 my @data;
267 8 foreach my $mime (@found)
268 4350 { push @data, map +[$_, $mime->type, $mime->encoding], $mime->extensions;
269 }
270  
271 8 ▶ wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
272 }
273  
274  
275 sub import_mime_types($)
276 0 ▶{ my $filename = shift;
Subroutine import_mime_types: not called CC=1 SCAR=0.6
Pod documented
277 4 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
278 0 croak <<'CROAK';
279 import_mime_types is not supported anymore: if you have types to add
280 please send them to the author.
281 CROAK
282 }
283  
284 4 1;
↑ Summary MojoX/MIME/Types.pm »