Field/ContType.pm

filter: Statementstmt 78.1% 25 / 32 Branchbran 37.5% 3 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 8 / 12 Podpod 37.5% 3 / 8 totaltotal 65.0% 39 / 60 CC 5 SCAR 8.0
CC
5
Cov
70%
multipart_boundary
6.0
charset
2.1
type
2.1
CRAP 2.2
« Decoder/Base64.pm ↑ Summary Decoder/Binary.pm »
1 package MIME::Field::ContType;
2  
3  
4 =head1 NAME
5  
6 MIME::Field::ContType - a "Content-type" field
7  
8  
9 =head1 DESCRIPTION
10  
11 A subclass of Mail::Field.
12  
13 I<Don't use this class directly... its name may change in the future!>
14 Instead, ask Mail::Field for new instances based on the field name!
15  
16  
17 =head1 SYNOPSIS
18  
19 use Mail::Field;
20 use MIME::Head;
21  
22 # Create an instance from some text:
23 $field = Mail::Field->new('Content-type',
24 'text/HTML; charset="US-ASCII"');
25  
26 # Get the MIME type, like 'text/plain' or 'x-foobar'.
27 # Returns 'text/plain' as default, as per RFC 2045:
28 my ($type, $subtype) = split('/', $field->type);
29  
30 # Get generic information:
31 print $field->name;
32  
33 # Get information related to "message" type:
34 if ($type eq 'message') {
35 print $field->id;
36 print $field->number;
37 print $field->total;
38 }
39  
40 # Get information related to "multipart" type:
41 if ($type eq 'multipart') {
42 print $field->boundary; # the basic value, fixed up
43 print $field->multipart_boundary; # empty if not a multipart message!
44 }
45  
46 # Get information related to "text" type:
47 if ($type eq 'text') {
48 print $field->charset; # returns 'us-ascii' as default
49 }
50  
51  
52 =head1 PUBLIC INTERFACE
53  
54 =over 4
55  
56 =cut
57  
58 37 require 5.001;
59 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 37 ▶use MIME::Field::ParamVal;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 37 ▶use MIME::Words qw(decode_mimewords);
Subroutine BEGIN: called CC=1 SCAR=0.0
62 37 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 37 @ISA = qw(MIME::Field::ParamVal);
65  
66 # The package version, both in 1.23 style *and* usable by MakeMaker:
67 37 $VERSION = "5.522";
68  
69 # Install it:
70 37 bless([])->register('Content-type');
71  
72 #------------------------------
73 #
74 # Basic access/storage methods...
75 #
76 1 ▶sub charset {
Subroutine charset: called CC=2 SCAR=7.6
Pod undocumented
77 1 ▶ lc(shift->paramstr('charset', @_)) || 'us-ascii'; # RFC 2045
Branch: 1 branch
BranchTrueFalseTotal
lc((shift())->paramstr("charset", @_)) or "us-ascii" 1 0 1
78 }
79 0 ▶sub id {
Subroutine id: not called CC=1 SCAR=6.9
Pod undocumented
80 0 shift->paramstr('id', @_);
81 }
82 17 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
83 17 shift->paramstr('name', @_);
84 }
85 0 ▶sub number {
Subroutine number: not called CC=1 SCAR=6.9
Pod undocumented
86 0 shift->paramstr('number', @_);
87 }
88 0 ▶sub total {
Subroutine total: not called CC=1 SCAR=6.9
Pod undocumented
89 0 shift->paramstr('total', @_);
90 }
91  
92  
93 #------------------------------
94  
95 =item boundary
96  
97 Return the boundary field. The boundary is returned exactly
98 as given in the C<Content-type:> field; that is, the leading
99 double-hyphen (C<-->) is I<not> prepended.
100  
101 (Well, I<almost> exactly... from RFC 2046:
102  
103 (If a boundary appears to end with white space, the white space
104 must be presumed to have been added by a gateway, and must be deleted.)
105  
106 so we oblige and remove any trailing spaces.)
107  
108 Returns the empty string if there is no boundary, or if the boundary is
109 illegal (e.g., if it is empty after all trailing whitespace has been
110 removed).
111  
112 =cut
113  
114 5 ▶sub boundary {
Subroutine boundary: called CC=2 SCAR=7.0
Pod documented
115 5 my $value = shift->param('boundary', @_);
116 5 ▶ defined($value) || return '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value or return "" 5 0 5
117 5 $value = decode_mimewords($value);
118 5 $value =~ s/\s+$//; # kill trailing whitespace, per RFC 2046
119 5 return $value;
120 }
121  
122 #------------------------------
123  
124 =item multipart_boundary
125  
126 Like C<boundary()>, except that this will also return the empty
127 string if the message is not a multipart message. In other words,
128 there's an automatic sanity check.
129  
130 =cut
131  
132 0 ▶sub multipart_boundary {
Subroutine multipart_boundary: not called CC=2 SCAR=17.9
Pod documented
133 0 my $self = shift;
134 0 my ($type) = split('/', $self->type);
135 0 ▶ return '' if ($type ne 'multipart'); # not multipart!
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "multipart" 0 0 0
136 0 $self->boundary; # okay, return the boundary
137 }
138  
139 #------------------------------
140  
141 =item type
142  
143 Try real hard to determine the content type (e.g., C<"text/plain">,
144 C<"image/gif">, C<"x-weird-type">, which is returned
145 in all-lowercase.
146  
147 A happy thing: the following code will work just as you would want,
148 even if there's no subtype (as in C<"x-weird-type">)... in such a case,
149 the $subtype would simply be the empty string:
150  
151 ($type, $subtype) = split('/', $head->mime_type);
152  
153 If the content-type information is missing, it defaults to C<"text/plain">,
154 as per RFC 2045:
155  
156 Default RFC 2822 messages are typed by this protocol as plain text in
157 the US-ASCII character set, which can be explicitly specified as
158 "Content-type: text/plain; charset=us-ascii". If no Content-Type is
159 specified, this default is assumed.
160  
161 B<Note:> under the "be liberal in what we accept" principle, this routine
162 no longer syntax-checks the content type. If it ain't empty,
163 just downcase and return it.
164  
165 =cut
166  
167 38 ▶sub type {
Subroutine type: called CC=2 SCAR=7.6
Pod documented
168 38 ▶ lc(shift->paramstr('_', @_)) || 'text/plain'; # RFC 2045
Branch: 1 branch
BranchTrueFalseTotal
lc((shift())->paramstr("_", @_)) or "text/plain" 38 0 38
169 }
170  
171 #------------------------------
172  
173 =back
174  
175  
176 =head1 NOTES
177  
178 Since nearly all (if not all) parameters must have non-empty values
179 to be considered valid, we just return the empty string to signify
180 missing fields. If you need to get the I<real> underlying value,
181 use the inherited C<param()> method (which returns undef if the
182 parameter is missing).
183  
184 =head1 SEE ALSO
185  
186 L<MIME::Field::ParamVal>, L<Mail::Field>
187  
188 =head1 AUTHOR
189  
190 Eryq (F<eryq@zeegee.com>), ZeeGee Software Inc (F<http://www.zeegee.com>).
191 Dianne Skoll (dianne@skoll.ca)
192  
193 =cut
194  
195 37 1;
196  
197  
198  
« Decoder/Base64.pm ↑ Summary Decoder/Binary.pm »