Filename | /usr/share/perl5/MIME/Types.pm |
Statements | Executed 21 statements in 2.26ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 1.62ms | 1.75ms | BEGIN@13 | MIME::Types::
1 | 1 | 1 | 14µs | 84µs | BEGIN@16 | MIME::Types::
1 | 1 | 1 | 12µs | 30µs | BEGIN@7 | MIME::Types::
1 | 1 | 1 | 10µs | 81µs | BEGIN@218 | MIME::Types::
1 | 1 | 1 | 9µs | 39µs | BEGIN@262 | MIME::Types::
1 | 1 | 1 | 8µs | 33µs | BEGIN@15 | MIME::Types::
1 | 1 | 1 | 6µs | 16µs | BEGIN@11 | MIME::Types::
1 | 1 | 1 | 4µs | 4µs | BEGIN@14 | MIME::Types::
0 | 0 | 0 | 0s | 0s | _MojoExtTable | MIME::Types::
0 | 0 | 0 | 0s | 0s | __ANON__[:183] | MIME::Types::
0 | 0 | 0 | 0s | 0s | __ANON__[:185] | MIME::Types::
0 | 0 | 0 | 0s | 0s | _read_db | MIME::Types::
0 | 0 | 0 | 0s | 0s | addType | MIME::Types::
0 | 0 | 0 | 0s | 0s | by_mediatype | MIME::Types::
0 | 0 | 0 | 0s | 0s | by_suffix | MIME::Types::
0 | 0 | 0 | 0s | 0s | create_type_index | MIME::Types::
0 | 0 | 0 | 0s | 0s | extensions | MIME::Types::
0 | 0 | 0 | 0s | 0s | httpAccept | MIME::Types::
0 | 0 | 0 | 0s | 0s | httpAcceptBest | MIME::Types::
0 | 0 | 0 | 0s | 0s | httpAcceptSelect | MIME::Types::
0 | 0 | 0 | 0s | 0s | import_mime_types | MIME::Types::
0 | 0 | 0 | 0s | 0s | init | MIME::Types::
0 | 0 | 0 | 0s | 0s | listTypes | MIME::Types::
0 | 0 | 0 | 0s | 0s | mimeTypeOf | MIME::Types::
0 | 0 | 0 | 0s | 0s | new | MIME::Types::
0 | 0 | 0 | 0s | 0s | type | MIME::Types::
0 | 0 | 0 | 0s | 0s | types | MIME::Types::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # Copyrights 1999,2001-2014 by [Mark Overmeer]. | ||||
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.01. | ||||
5 | |||||
6 | package MIME::Types; | ||||
7 | 2 | 31µs | 2 | 48µs | # spent 30µs (12+18) within MIME::Types::BEGIN@7 which was called:
# once (12µs+18µs) by C4::Letters::BEGIN@23 at line 7 # spent 30µs making 1 call to MIME::Types::BEGIN@7
# spent 18µs making 1 call to vars::import |
8 | 1 | 300ns | $VERSION = '2.09'; | ||
9 | |||||
10 | |||||
11 | 2 | 19µs | 2 | 26µs | # spent 16µs (6+10) within MIME::Types::BEGIN@11 which was called:
# once (6µs+10µs) by C4::Letters::BEGIN@23 at line 11 # spent 16µs making 1 call to MIME::Types::BEGIN@11
# spent 10µs making 1 call to strict::import |
12 | |||||
13 | 2 | 650µs | 1 | 1.75ms | # spent 1.75ms (1.62+126µs) within MIME::Types::BEGIN@13 which was called:
# once (1.62ms+126µs) by C4::Letters::BEGIN@23 at line 13 # spent 1.75ms making 1 call to MIME::Types::BEGIN@13 |
14 | 2 | 20µs | 1 | 4µs | # spent 4µs within MIME::Types::BEGIN@14 which was called:
# once (4µs+0s) by C4::Letters::BEGIN@23 at line 14 # spent 4µs making 1 call to MIME::Types::BEGIN@14 |
15 | 2 | 25µs | 2 | 58µs | # spent 33µs (8+25) within MIME::Types::BEGIN@15 which was called:
# once (8µs+25µs) by C4::Letters::BEGIN@23 at line 15 # spent 33µs making 1 call to MIME::Types::BEGIN@15
# spent 25µs making 1 call to Exporter::import |
16 | 2 | 1.27ms | 2 | 96µs | # spent 84µs (14+70) within MIME::Types::BEGIN@16 which was called:
# once (14µs+70µs) by C4::Letters::BEGIN@23 at line 16 # spent 84µs making 1 call to MIME::Types::BEGIN@16
# spent 11µs making 1 call to List::Util::import |
17 | |||||
18 | |||||
19 | 1 | 200ns | my %typedb; | ||
20 | sub new(@) { (bless {}, shift)->init( {@_} ) } | ||||
21 | |||||
22 | sub init($) | ||||
23 | { my ($self, $args) = @_; | ||||
24 | keys %typedb or $self->_read_db($args); | ||||
25 | $self; | ||||
26 | } | ||||
27 | |||||
28 | sub _read_db($) | ||||
29 | { my ($self, $args) = @_; | ||||
30 | my $skip_extensions = $args->{skip_extensions}; | ||||
31 | my $only_complete = $args->{only_complete}; | ||||
32 | my $only_iana = $args->{only_iana}; | ||||
33 | |||||
34 | my $db = $args->{db_file} | ||||
35 | || File::Spec->catfile(dirname(__FILE__), 'types.db'); | ||||
36 | |||||
37 | local *DB; | ||||
38 | open DB, '<:encoding(utf8)', $db | ||||
39 | or die "cannot open type database in $db: $!\n"; | ||||
40 | |||||
41 | while(1) | ||||
42 | { my $header = <DB>; | ||||
43 | defined $header or last; | ||||
44 | chomp $header; | ||||
45 | |||||
46 | # This logic is entangled with the bin/collect_types script | ||||
47 | my ($count, $major, $is_iana, $has_ext) = split /\:/, $header; | ||||
48 | my $skip_section = $major eq 'EXTENSIONS' ? $skip_extensions | ||||
49 | : (($only_iana && !$is_iana) || ($only_complete && !$has_ext)); | ||||
50 | |||||
51 | #warn "Skipping section $header\n" if $skip_section; | ||||
52 | (my $section = $major) =~ s/^x-//; | ||||
53 | if($major eq 'EXTENSIONS') | ||||
54 | { local $_; | ||||
55 | while(<DB>) | ||||
56 | { last if m/^$/; | ||||
57 | next if $skip_section; | ||||
58 | chomp; | ||||
59 | $typedb{$section}{$1} = $2 if m/(.*);(.*)/; | ||||
60 | } | ||||
61 | } | ||||
62 | else | ||||
63 | { local $_; | ||||
64 | while(<DB>) | ||||
65 | { last if m/^$/; | ||||
66 | next if $skip_section; | ||||
67 | chomp; | ||||
68 | $typedb{$section}{$1} = "$major/$_" if m/^(?:x-)?([^;]+)/; | ||||
69 | } | ||||
70 | } | ||||
71 | } | ||||
72 | |||||
73 | close DB; | ||||
74 | } | ||||
75 | |||||
76 | # Catalyst-Plugin-Static-Simple uses it :( | ||||
77 | sub create_type_index {} | ||||
78 | |||||
79 | #------------------------------------------- | ||||
80 | |||||
81 | sub type($) | ||||
82 | { my $spec = lc $_[1]; | ||||
83 | $spec = 'text/plain' if $spec eq 'text'; # old mailers | ||||
84 | |||||
85 | $spec =~ m!^(?:x\-)?([^/]+)/(?:x-)?(.*)! | ||||
86 | or return; | ||||
87 | |||||
88 | my $section = $typedb{$1} or return; | ||||
89 | my $record = $section->{$2} or return; | ||||
90 | return $record if ref $record; # already extended | ||||
91 | |||||
92 | my $simple = $2; | ||||
93 | my ($type, $ext, $enc) = split m/\;/, $record; | ||||
94 | my $os = undef; # XXX TODO | ||||
95 | |||||
96 | $section->{$simple} = MIME::Type->new | ||||
97 | ( type => $type | ||||
98 | , extensions => [split /\,/, $ext] | ||||
99 | , encoding => $enc | ||||
100 | , system => $os | ||||
101 | ); | ||||
102 | } | ||||
103 | |||||
104 | |||||
105 | sub mimeTypeOf($) | ||||
106 | { my ($self, $name) = @_; | ||||
107 | (my $ext = lc $name) =~ s/.*\.//; | ||||
108 | my $type = $typedb{EXTENSIONS}{$ext} or return; | ||||
109 | $self->type($type); | ||||
110 | } | ||||
111 | |||||
112 | |||||
113 | sub addType(@) | ||||
114 | { my $self = shift; | ||||
115 | |||||
116 | foreach my $type (@_) | ||||
117 | { my ($major, $minor) = split m!/!, $type->simplified; | ||||
118 | $typedb{$major}{$minor} = $type; | ||||
119 | $typedb{EXTENSIONS}{$_} = $type for $type->extensions; | ||||
120 | } | ||||
121 | $self; | ||||
122 | } | ||||
123 | |||||
124 | |||||
125 | sub types() | ||||
126 | { my $self = shift; | ||||
127 | my @types; | ||||
128 | foreach my $section (keys %typedb) | ||||
129 | { next if $section eq 'EXTENSIONS'; | ||||
130 | push @types, map $_->type("$section/$_"), | ||||
131 | sort keys %{$typedb{$section}}; | ||||
132 | } | ||||
133 | @types; | ||||
134 | } | ||||
135 | |||||
136 | |||||
137 | sub listTypes() | ||||
138 | { my $self = shift; | ||||
139 | my @types; | ||||
140 | foreach my $section (keys %typedb) | ||||
141 | { next if $section eq 'EXTENSIONS'; | ||||
142 | foreach my $sub (sort keys %{$typedb{$section}}) | ||||
143 | { my $record = $typedb{$section}{$sub}; | ||||
144 | push @types, ref $record ? $record->type | ||||
145 | : $record =~ m/^([^;]+)/ ? $1 : die; | ||||
146 | } | ||||
147 | } | ||||
148 | @types; | ||||
149 | } | ||||
150 | |||||
151 | |||||
152 | sub extensions { keys %{$typedb{EXTENSIONS}} } | ||||
153 | sub _MojoExtTable() {$typedb{EXTENSIONS}} | ||||
154 | |||||
155 | #------------- | ||||
156 | |||||
157 | sub httpAccept($) | ||||
158 | { my $self = shift; | ||||
159 | my @listed; | ||||
160 | |||||
161 | foreach (split /\,\s*/, shift) | ||||
162 | { | ||||
163 | m!^ ([a-zA-Z0-9-]+ | \*) / ( [a-zA-Z0-9+-]+ | \* ) | ||||
164 | \s* (?: \;\s*q\=\s* ([0-9]+(?:\.[0-9]*)?) \s* )? | ||||
165 | (\;.* | ) | ||||
166 | $ !x or next; | ||||
167 | |||||
168 | my $mime = "$1/$2$4"; | ||||
169 | my $q = $3 || ($1 eq '*' ? -2 : $2 eq '*' ? -1 : 1); | ||||
170 | push @listed, [ $mime, $q-@listed*0.001 ]; | ||||
171 | } | ||||
172 | map $_->[0], sort {$b->[1] <=> $a->[1]} @listed; | ||||
173 | } | ||||
174 | |||||
175 | |||||
176 | sub httpAcceptBest($@) | ||||
177 | { my $self = shift; | ||||
178 | my @accept = ref $_[0] eq 'ARRAY' ? @{(shift)} : $self->httpAccept(shift); | ||||
179 | my $match; | ||||
180 | |||||
181 | foreach my $acc (@accept) | ||||
182 | { $acc =~ s/\s*\;.*//; # remove attributes | ||||
183 | my $m = $acc !~ s#/\*$## ? first { $_->equals($acc) } @_ | ||||
184 | : $acc eq '*' ? $_[0] # $acc eq */* | ||||
185 | : first { $_->mediaType eq $acc } @_; | ||||
186 | return $m if defined $m; | ||||
187 | } | ||||
188 | |||||
189 | (); | ||||
190 | } | ||||
191 | |||||
192 | |||||
193 | sub httpAcceptSelect($@) | ||||
194 | { my ($self, $accept) = (shift, shift); | ||||
195 | my $fns = !@_ ? return () : ref $_[0] eq 'ARRAY' ? shift : [@_]; | ||||
196 | |||||
197 | unless(defined $accept) | ||||
198 | { my $fn = $fns->[0]; | ||||
199 | return ($fn, $self->mimeTypeOf($fn)); | ||||
200 | } | ||||
201 | |||||
202 | # create mapping type -> filename | ||||
203 | my (%have, @have); | ||||
204 | foreach my $fn (@$fns) | ||||
205 | { my $type = $self->mimeTypeOf($fn) or next; | ||||
206 | $have{$type->simplified} = $fn; | ||||
207 | push @have, $type; | ||||
208 | } | ||||
209 | |||||
210 | my $type = $self->httpAcceptBest($accept, @have); | ||||
211 | defined $type ? ($have{$type}, $type) : (); | ||||
212 | } | ||||
213 | |||||
214 | #------------------------------------------- | ||||
215 | # OLD INTERFACE (version 0.06 and lower) | ||||
216 | |||||
217 | |||||
218 | 2 | 197µs | 2 | 151µs | # spent 81µs (10+70) within MIME::Types::BEGIN@218 which was called:
# once (10µs+70µs) by C4::Letters::BEGIN@23 at line 218 # spent 81µs making 1 call to MIME::Types::BEGIN@218
# spent 70µs making 1 call to base::import |
219 | 1 | 700ns | our @EXPORT_OK = qw(by_suffix by_mediatype import_mime_types); | ||
220 | |||||
221 | |||||
222 | 1 | 0s | my $mime_types; | ||
223 | |||||
224 | sub by_suffix($) | ||||
225 | { my $filename = shift; | ||||
226 | $mime_types ||= MIME::Types->new; | ||||
227 | my $mime = $mime_types->mimeTypeOf($filename); | ||||
228 | |||||
229 | my @data = defined $mime ? ($mime->type, $mime->encoding) : ('',''); | ||||
230 | wantarray ? @data : \@data; | ||||
231 | } | ||||
232 | |||||
233 | |||||
234 | sub by_mediatype($) | ||||
235 | { my $type = shift; | ||||
236 | $mime_types ||= MIME::Types->new; | ||||
237 | |||||
238 | my @found; | ||||
239 | if(!ref $type && index($type, '/') >= 0) | ||||
240 | { my $mime = $mime_types->type($type); | ||||
241 | @found = $mime if $mime; | ||||
242 | } | ||||
243 | else | ||||
244 | { my $search = ref $type eq 'Regexp' ? $type : qr/$type/i; | ||||
245 | @found = map $mime_types->type($_), | ||||
246 | grep $_ =~ $search, | ||||
247 | $mime_types->listTypes; | ||||
248 | } | ||||
249 | |||||
250 | my @data; | ||||
251 | foreach my $mime (@found) | ||||
252 | { push @data, map [$_, $mime->type, $mime->encoding], | ||||
253 | $mime->extensions; | ||||
254 | } | ||||
255 | |||||
256 | wantarray ? @data : \@data; | ||||
257 | } | ||||
258 | |||||
259 | |||||
260 | sub import_mime_types($) | ||||
261 | { my $filename = shift; | ||||
262 | 2 | 44µs | 2 | 70µs | # spent 39µs (9+30) within MIME::Types::BEGIN@262 which was called:
# once (9µs+30µs) by C4::Letters::BEGIN@23 at line 262 # spent 39µs making 1 call to MIME::Types::BEGIN@262
# spent 30µs making 1 call to Exporter::import |
263 | croak <<'CROAK'; | ||||
264 | import_mime_types is not supported anymore: if you have types to add | ||||
265 | please send them to the author. | ||||
266 | CROAK | ||||
267 | } | ||||
268 | |||||
269 | 1 | 3µs | 1; | ||
270 | __END__ |