File Coverage

blib/lib/MIME/Type.pm
Criterion Covered Total %
statement 47 52 90.3
branch 16 24 66.6
condition 5 7 71.4
subroutine 22 25 88.0
pod 19 21 90.4
total 109 129 84.5


line stmt bran cond sub pod time code
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::Type;{
13             our $VERSION = '2.30';
14             }
15              
16              
17 6     6   230780 use strict;
  6         12  
  6         225  
18 6     6   39 use warnings;
  6         10  
  6         349  
19              
20 6     6   32 use Carp 'croak';
  6         10  
  6         430  
21              
22             #--------------------
23              
24             #--------------------
25              
26             use overload
27 6         40 '""' => 'type',
28 6     6   3936 cmp => 'cmp';
  6         11566  
29              
30             #--------------------
31              
32 2207     2207 1 370984 sub new(@) { (bless {}, shift)->init( {@_} ) }
33              
34             sub init($)
35 2207     2207 0 4228 { my ($self, $args) = @_;
36              
37             my $type = $self->{MT_type} = $args->{type}
38 2207 50       8607 or croak "ERROR: Type parameter is obligatory.";
39              
40 2207   33     6775 $self->{MT_simplified} = $args->{simplified} || $self->simplified($type);
41 2207   100     6202 $self->{MT_extensions} = $args->{extensions} || [];
42              
43             $self->{MT_encoding}
44             = $args->{encoding} ? $args->{encoding}
45 2207 100       5721 : $self->mediaType eq 'text' ? 'quoted-printable'
    100          
46             : 'base64';
47              
48 2207 50       5592 $self->{MT_system} = $args->{system} if defined $args->{system};
49 2207 100       6740 $self->{MT_charset} = $args->{charset} if defined $args->{charset};
50 2207         14492 $self;
51             }
52              
53             #--------------------
54              
55 17334     17334 1 56044 sub type() { $_[0]->{MT_type} }
56              
57              
58             sub simplified(;$)
59 4417     4417 1 6508 { my $thing = shift;
60 4417 100       18667 @_ or return $thing->{MT_simplified};
61              
62 2239         3531 my $mime = shift;
63              
64 2239 0       24243 $mime =~ m!^\s*(?:x\-)?([\w.+-]+)/(?:x\-)?([\w.+-]+)\s*$!i ? lc "$1/$2"
    50          
65             : $mime eq 'text' ? 'text/plain' # some silly mailers...
66             : $mime; # doesn't follow rules, f.i. one word
67             }
68              
69              
70 4356     4356 1 5993 sub extensions() { @{$_[0]->{MT_extensions}} }
  4356         12464  
71 4207     4207 1 19831 sub encoding() { $_[0]->{MT_encoding} }
72 0     0 1 0 sub system() { $_[0]->{MT_system} }
73              
74              
75 0     0 1 0 sub charset() { $_[0]->{MT_charset} }
76              
77             #--------------------
78              
79 2107 50   2107 1 4488 sub mediaType() { $_[0]->simplified =~ m!^([\w.-]+)/! ? $1 : undef }
80 2     2 0 13 sub mainType() { $_[0]->mediaType } # Backwards compatibility
81              
82              
83 4 50   4 1 23 sub subType() { $_[0]->simplified =~ m!/([\w+.-]+)$! ? $1 : undef }
84              
85              
86 8     8 1 65 sub isRegistered() { lc($_[0]->type) !~ m{^x\-|/x\-} }
87              
88              
89             # http://tools.ietf.org/html/rfc4288#section-3
90 4     4 1 18 sub isVendor() { $_[0]->simplified =~ m!/vnd\.! }
91 4     4 1 15 sub isPersonal() { $_[0]->simplified =~ m!/prs\.! }
92 4     4 1 16 sub isExperimental() { $_[0]->simplified =~ m!/x\.! }
93              
94              
95 4     4 1 696 sub isBinary() { $_[0]->encoding eq 'base64' }
96 4     4 1 15 sub isText() { $_[0]->encoding ne 'base64' }
97             *isAscii = \&isText;
98              
99              
100             # simplified names only!
101             my %sigs = map +($_ => 1),
102             qw(application/pgp-keys application/pgp application/pgp-signature
103             application/pkcs10 application/pkcs7-mime application/pkcs7-signature
104             text/vCard);
105              
106 0     0 1 0 sub isSignature() { $sigs{$_[0]->simplified} }
107              
108              
109             sub cmp($)
110 37     37 1 2962 { my ($self, $other) = @_;
111 37 100       116 my $type = ref $other ? $other->simplified : (ref $self)->simplified($other);
112 37         55 $self->simplified cmp $type;
113             }
114              
115 19     19 1 2011 sub equals($) { $_[0]->cmp($_[1])==0 }
116              
117              
118             my %ctext;
119             $ctext{$_} = 'US-ASCII' for qw/plain cql cql-expression cql-identifier css directory dns encaprtp enriched/;
120             $ctext{$_} = 'UTF-8' for qw/cache-manifest calendar csv csv-schema ecmascript/;
121             $ctext{$_} = '_REQUIRED' for qw//;
122              
123             sub defaultCharset()
124 2     2 1 7 { my $self = shift;
125 2         8 my $st = (lc $self->subType) =~ s/^x-//r;
126 2   100     24 my $default = $ctext{$st} // return undef;
127              
128 1 50       7 if($default eq '_REQUIRED')
129 0         0 { warn "MediaType ".($self->subType)." requires an explicit character-set.";
130 0         0 return undef;
131             }
132              
133 1         9 $default;
134             }
135              
136             1;