Head.pm

filter: Statementstmt 89.4% 127 / 142 Branchbran 78.9% 30 / 38 Conditioncond 71.4% 20 / 28 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 82.3% 28 / 34 Podpod 71.4% 15 / 21 totaltotal 81.6% 227 / 278 CC 35 SCAR 13.1
CC
35
Cov
85%
ambiguous_content
7.0
mime_type
6.8
decode
6.2
CRAP 3.7
« Decoder/QuotedPrint.pm ↑ Summary Decoder/Gzip64.pm »
1 package MIME::Head;
2  
3 27 ▶use MIME::WordDecoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 =head1 NAME
5  
6 MIME::Head - MIME message header (a subclass of Mail::Header)
7  
8  
9 =head1 SYNOPSIS
10  
11 Before reading further, you should see L<MIME::Tools> to make sure that
12 you understand where this module fits into the grand scheme of things.
13 Go on, do it now. I'll wait.
14  
15 Ready? Ok...
16  
17 =head2 Construction
18  
19 ### Create a new, empty header, and populate it manually:
20 $head = MIME::Head->new;
21 $head->replace('content-type', 'text/plain; charset=US-ASCII');
22 $head->replace('content-length', $len);
23  
24 ### Parse a new header from a filehandle:
25 $head = MIME::Head->read(\*STDIN);
26  
27 ### Parse a new header from a file, or a readable pipe:
28 $testhead = MIME::Head->from_file("/tmp/test.hdr");
29 $a_b_head = MIME::Head->from_file("cat a.hdr b.hdr |");
30  
31  
32 =head2 Output
33  
34 ### Output to filehandle:
35 $head->print(\*STDOUT);
36  
37 ### Output as string:
38 print STDOUT $head->as_string;
39 print STDOUT $head->stringify;
40  
41  
42 =head2 Getting field contents
43  
44 ### Is this a reply?
45 $is_reply = 1 if ($head->get('Subject') =~ /^Re: /);
46  
47 ### Get receipt information:
48 print "Last received from: ", $head->get('Received', 0);
49 @all_received = $head->get('Received');
50  
51 ### Print the subject, or the empty string if none:
52 print "Subject: ", $head->get('Subject',0);
53  
54 ### Too many hops? Count 'em and see!
55 if ($head->count('Received') > 5) { ...
56  
57 ### Test whether a given field exists
58 warn "missing subject!" if (! $head->count('subject'));
59  
60  
61 =head2 Setting field contents
62  
63 ### Declare this to be an HTML header:
64 $head->replace('Content-type', 'text/html');
65  
66  
67 =head2 Manipulating field contents
68  
69 ### Get rid of internal newlines in fields:
70 $head->unfold;
71  
72 ### Decode any Q- or B-encoded-text in fields (DEPRECATED):
73 $head->decode;
74  
75  
76 =head2 Getting high-level MIME information
77  
78 ### Get/set a given MIME attribute:
79 unless ($charset = $head->mime_attr('content-type.charset')) {
80 $head->mime_attr("content-type.charset" => "US-ASCII");
81 }
82  
83 ### The content type (e.g., "text/html"):
84 $mime_type = $head->mime_type;
85  
86 ### The content transfer encoding (e.g., "quoted-printable"):
87 $mime_encoding = $head->mime_encoding;
88  
89 ### The recommended name when extracted:
90 $file_name = $head->recommended_filename;
91  
92 ### The boundary text, for multipart messages:
93 $boundary = $head->multipart_boundary;
94  
95  
96 =head1 DESCRIPTION
97  
98 A class for parsing in and manipulating RFC-822 message headers, with
99 some methods geared towards standard (and not so standard) MIME fields
100 as specified in the various I<Multipurpose Internet Mail Extensions>
101 RFCs (starting with RFC 2045)
102  
103  
104 =head1 PUBLIC INTERFACE
105  
106 =cut
107  
108 #------------------------------
109  
110 27 require 5.002;
111  
112 ### Pragmas:
113 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 27 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 ### System modules:
117 27 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 ### Other modules:
120 27 ▶use Mail::Header 1.09 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
121 27 ▶use Mail::Field 1.05 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 ### Kit modules:
124 27 ▶use MIME::Words qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
125 27 ▶use MIME::Tools qw(:config :msgs);
Subroutine BEGIN: called CC=1 SCAR=0.0
126 27 ▶use MIME::Field::ParamVal;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 27 ▶use MIME::Field::ConTraEnc;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 27 ▶use MIME::Field::ContDisp;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 27 ▶use MIME::Field::ContType;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 27 @ISA = qw(Mail::Header);
132  
133 # The presence of more than one of the following headers
134 # in a given MIME entity could indicate an ambiguous parse
135 # and hence a security risk
136  
137 27 my $singleton_headers =
138 [
139 'content-type',
140 'content-disposition',
141 'content-transfer-encoding',
142 'content-id',
143 ];
144  
145 # The presence of a duplicated or empty parameters in one of the following
146 # headers in a given MIME entity could indicate an ambiguous parse and
147 # hence a security risk
148 27 my $singleton_parameter_headers =
149 [
150 'content-type',
151 'content-disposition',
152 ];
153  
154  
155 #------------------------------
156 #
157 # Public globals...
158 #
159 #------------------------------
160  
161 ### The package version, both in 1.23 style *and* usable by MakeMaker:
162 27 $VERSION = "5.520";
163  
164 ### Sanity (we put this test after our own version, for CPAN::):
165 27 ▶use Mail::Header 1.06 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167  
168 #------------------------------
169  
170 =head2 Creation, input, and output
171  
172 =over 4
173  
174 =cut
175  
176 #------------------------------
177  
178  
179 #------------------------------
180  
181 =item new [ARG],[OPTIONS]
182  
183 I<Class method, inherited.>
184 Creates a new header object. Arguments are the same as those in the
185 superclass.
186  
187 =cut
188  
189 3562 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
190 3562 my $class = shift;
191 3562 bless Mail::Header->new(@_), $class;
192 }
193  
194 =item ambiguous_content
195  
196 I<Instance method.>
197  
198 Returns true if this header has any the following properties:
199  
200 =over 4
201  
202 More than one Content-Type, Content-ID, Content-Transfer-Encoding or
203 Content-Disposition header.
204  
205 A Content-Type or Content-Disposition header contains a repeated
206 parameter.
207  
208 A Content-Type header contains a boundary that is ambiguous.
209  
210 =back
211  
212 Messages with ambiguous content should be treated as a security risk.
213 In particular, if MIME-tools is used in an email security tool,
214 ambiguous messages should not be delivered to end-users.
215  
216 =cut
217 1742 ▶sub ambiguous_content {
Subroutine ambiguous_content: called CC=7 SCAR=19.5
Pod documented
218 1742 my ($self) = @_;
219  
220 1742 foreach my $hdr (@$singleton_headers) {
221 6954 ▶ if ($self->count($hdr) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count($hdr) > 1) 9 6945 6954
222 9 return 1;
223 }
224 }
225  
226 1733 foreach my $hdr (@$singleton_parameter_headers) {
227 3456 ▶ if ($self->mime_attr($hdr . '.@duplicate_parameters')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mime_attr($hdr . '.@duplicate_parameters')) 8 3448 3456
228 8 return 1;
229 }
230 3448 ▶ if ($self->mime_attr($hdr . '.@empty_parameters')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mime_attr($hdr . '.@empty_parameters')) 5 3443 3448
231 5 return 1;
232 }
233 }
234  
235 1720 ▶ if ($self->mime_attr('content-type.@boundary_ambiguous')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mime_attr("content-type.\@boundary_ambiguous")) 6 1714 1720
236 6 return 1;
237 }
238  
239 1714 return 0;
240 }
241 #------------------------------
242  
243 =item from_file EXPR,OPTIONS
244  
245 I<Class or instance method>.
246 For convenience, you can use this to parse a header object in from EXPR,
247 which may actually be any expression that can be sent to open() so as to
248 return a readable filehandle. The "file" will be opened, read, and then
249 closed:
250  
251 ### Create a new header by parsing in a file:
252 my $head = MIME::Head->from_file("/tmp/test.hdr");
253  
254 Since this method can function as either a class constructor I<or>
255 an instance initializer, the above is exactly equivalent to:
256  
257 ### Create a new header by parsing in a file:
258 my $head = MIME::Head->new->from_file("/tmp/test.hdr");
259  
260 On success, the object will be returned; on failure, the undefined value.
261  
262 The OPTIONS are the same as in new(), and are passed into new()
263 if this is invoked as a class method.
264  
265 B<Note:> This is really just a convenience front-end onto C<read()>,
266 provided mostly for backwards-compatibility with MIME-parser 1.0.
267  
268 =cut
269  
270 6 ▶sub from_file {
Subroutine from_file: called CC=5 SCAR=16.2
Pod documented
271 6 my ($self, $file, @opts) = @_; ### at this point, $self is inst. or class!
272 6 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 2 4 6
273  
274 ### Parse:
275 6 ▶ my $fh = IO::File->new($file, '<') or return error("open $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file, "<") or return error("open ${file}: $!") 5 1 6
276 5 ▶ $fh->binmode() or return error("binmode $file: $!"); # we expect to have \r\n at line ends, and want to keep 'em.
Branch: 1 branch
BranchTrueFalseTotal
$fh->binmode or return error("binmode ${file}: $!") 5 0 5
277 5 $self = $class->new($fh, @opts); ### now, $self is instance or undef
278 5 ▶ $fh->close or return error("close $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or return error("close ${file}: $!") 5 0 5
279 5 $self;
280 }
281  
282 #------------------------------
283  
284 =item read FILEHANDLE
285  
286 I<Instance (or class) method.>
287 This initializes a header object by reading it in from a FILEHANDLE,
288 until the terminating blank line is encountered.
289 A syntax error or end-of-stream will also halt processing.
290  
291 Supply this routine with a reference to a filehandle glob; e.g., C<\*STDIN>:
292  
293 ### Create a new header by parsing in STDIN:
294 $head->read(\*STDIN);
295  
296 On success, the self object will be returned; on failure, a false value.
297  
298 B<Note:> in the MIME world, it is perfectly legal for a header to be
299 empty, consisting of nothing but the terminating blank line. Thus,
300 we can't just use the formula that "no tags equals error".
301  
302 B<Warning:> as of the time of this writing, Mail::Header::read did not flag
303 either syntax errors or unexpected end-of-file conditions (an EOF
304 before the terminating blank line). MIME::ParserBase takes this
305 into account.
306  
307 =cut
308  
309 1724 ▶sub read {
Subroutine read: called CC=2 SCAR=7.1
Pod documented
310 1724 my $self = shift; ### either instance or class!
311 1724 ▶ ref($self) or $self = $self->new; ### if used as class method, make new
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->new 1724 0 1724
312 1724 $self->SUPER::read(@_);
313 }
314  
315  
316  
317 #------------------------------
318  
319 =back
320  
321 =head2 Getting/setting fields
322  
323 The following are methods related to retrieving and modifying the header
324 fields. Some are inherited from Mail::Header, but I've kept the
325 documentation around for convenience.
326  
327 =over 4
328  
329 =cut
330  
331 #------------------------------
332  
333  
334 #------------------------------
335  
336 =item add TAG,TEXT,[INDEX]
337  
338 I<Instance method, inherited.>
339 Add a new occurrence of the field named TAG, given by TEXT:
340  
341 ### Add the trace information:
342 $head->add('Received',
343 'from eryq.pr.mcs.net by gonzo.net with smtp');
344  
345 Normally, the new occurrence will be I<appended> to the existing
346 occurrences. However, if the optional INDEX argument is 0, then the
347 new occurrence will be I<prepended>. If you want to be I<explicit>
348 about appending, specify an INDEX of -1.
349  
350 B<Warning>: this method always adds new occurrences; it doesn't overwrite
351 any existing occurrences... so if you just want to I<change> the value
352 of a field (creating it if necessary), then you probably B<don't> want to use
353 this method: consider using C<replace()> instead.
354  
355 =cut
356  
357 ### Inherited.
358  
359 #------------------------------
360 #
361 # copy
362 #
363 # Instance method, DEPRECATED.
364 # Duplicate the object.
365 #
366 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod undocumented
367 0 usage "deprecated: use dup() instead.";
368 0 shift->dup(@_);
369 }
370  
371 #------------------------------
372  
373 =item count TAG
374  
375 I<Instance method, inherited.>
376 Returns the number of occurrences of a field; in a boolean context, this
377 tells you whether a given field exists:
378  
379 ### Was a "Subject:" field given?
380 $subject_was_given = $head->count('subject');
381  
382 The TAG is treated in a case-insensitive manner.
383 This method returns some false value if the field doesn't exist,
384 and some true value if it does.
385  
386 =cut
387  
388 ### Inherited.
389  
390  
391 #------------------------------
392  
393 =item decode [FORCE]
394  
395 I<Instance method, DEPRECATED.>
396 Go through all the header fields, looking for RFC 1522 / RFC 2047 style
397 "Q" (quoted-printable, sort of) or "B" (base64) encoding, and decode
398 them in-place. Fellow Americans, you probably don't know what the hell
399 I'm talking about. Europeans, Russians, et al, you probably do.
400 C<:-)>.
401  
402 B<This method has been deprecated.>
403 See L<MIME::Parser/decode_headers> for the full reasons.
404 If you absolutely must use it and don't like the warning, then
405 provide a FORCE:
406  
407 "I_NEED_TO_FIX_THIS"
408 Just shut up and do it. Not recommended.
409 Provided only for those who need to keep old scripts functioning.
410  
411 "I_KNOW_WHAT_I_AM_DOING"
412 Just shut up and do it. Not recommended.
413 Provided for those who REALLY know what they are doing.
414  
415 B<What this method does.>
416 For an example, let's consider a valid email header you might get:
417  
418 From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>
419 To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>
420 CC: =?ISO-8859-1?Q?Andr=E9_?= Pirard <PIRARD@vm1.ulg.ac.be>
421 Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
422 =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=
423 =?US-ASCII?Q?.._cool!?=
424  
425 That basically decodes to (sorry, I can only approximate the
426 Latin characters with 7 bit sequences /o and 'e):
427  
428 From: Keith Moore <moore@cs.utk.edu>
429 To: Keld J/orn Simonsen <keld@dkuug.dk>
430 CC: Andr'e Pirard <PIRARD@vm1.ulg.ac.be>
431 Subject: If you can read this you understand the example... cool!
432  
433 B<Note:> currently, the decodings are done without regard to the
434 character set: thus, the Q-encoding C<=F8> is simply translated to the
435 octet (hexadecimal C<F8>), period. For piece-by-piece decoding
436 of a given field, you want the array context of
437 C<MIME::Words::decode_mimewords()>.
438  
439 B<Warning:> the CRLF+SPACE separator that splits up long encoded words
440 into shorter sequences (see the Subject: example above) gets lost
441 when the field is unfolded, and so decoding after unfolding causes
442 a spurious space to be left in the field.
443 I<THEREFORE: if you're going to decode, do so BEFORE unfolding!>
444  
445 This method returns the self object.
446  
447 I<Thanks to Kent Boortz for providing the idea, and the baseline
448 RFC-1522-decoding code.>
449  
450 =cut
451  
452 2 ▶sub decode {
Subroutine decode: called CC=6 SCAR=18.3
Pod documented
453 2 my $self = shift;
454  
455 ### Warn if necessary:
456 2 ▶ my $force = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
457 2 ▶ unless (($force eq "I_NEED_TO_FIX_THIS") ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($force eq "I_NEED_TO_FIX_THIS" or $force eq "I_KNOW_WHAT_I_AM_DOING") 2 0 2
Condition33%1/3
$force eq "I_NEED_TO_FIX_THIS" or $force eq "I_KNOW_WHAT_I_AM_DOING"
l!l&&r!l&&!r
002
MC/DC0%0/2
$force eq "I_NEED_TO_FIX_THIS" or $force eq "I_KNOW_WHAT_I_AM_DOING"
$force eq "I_NEED_TO_FIX_THIS" $force eq "I_KNOW_WHAT_I_AM_DOING"
Truth table33%1/3
A or B
A= $force eq "I_NEED_TO_FIX_THIS"B= $force eq "I_KNOW_WHAT_I_AM_DOING"
ABresult
1X1
011
000
458 ($force eq "I_KNOW_WHAT_I_AM_DOING")) {
459 2 usage "decode is deprecated for safety";
460 }
461  
462 2 my ($tag, $i, @decoded);
463 2 foreach $tag ($self->tags) {
464 20 @decoded = map { scalar(decode_mimewords($_, Field=>$tag))
465 } $self->get_all($tag);
466 20 ▶ for ($i = 0; $i < @decoded; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @decoded) 20 20 40
467 20 $self->replace($tag, $decoded[$i], $i);
468 }
469 }
470 2 $self->{MH_Decoded} = 1;
471 2 $self;
472 }
473  
474 #------------------------------
475  
476 =item delete TAG,[INDEX]
477  
478 I<Instance method, inherited.>
479 Delete all occurrences of the field named TAG.
480  
481 ### Remove some MIME information:
482 $head->delete('MIME-Version');
483 $head->delete('Content-type');
484  
485 =cut
486  
487 ### Inherited
488  
489  
490 #------------------------------
491 #
492 # exists
493 #
494 0 ▶sub exists {
Subroutine exists: not called CC=1 SCAR=6.9
Pod undocumented
495 0 usage "deprecated; use count() instead";
496 0 shift->count(@_);
497 }
498  
499 #------------------------------
500 #
501 # fields
502 #
503 0 ▶sub fields {
Subroutine fields: not called CC=1 SCAR=6.9
Pod documented
504 0 usage "deprecated: use tags() instead",
505 shift->tags(@_);
506 }
507  
508 #------------------------------
509  
510 =item get TAG,[INDEX]
511  
512 I<Instance method, inherited.>
513 Get the contents of field TAG.
514  
515 If a B<numeric INDEX> is given, returns the occurrence at that index,
516 or undef if not present:
517  
518 ### Print the first and last 'Received:' entries (explicitly):
519 print "First, or most recent: ", $head->get('received', 0);
520 print "Last, or least recent: ", $head->get('received',-1);
521  
522 If B<no INDEX> is given, but invoked in a B<scalar> context, then
523 INDEX simply defaults to 0:
524  
525 ### Get the first 'Received:' entry (implicitly):
526 my $most_recent = $head->get('received');
527  
528 If B<no INDEX> is given, and invoked in an B<array> context, then
529 I<all> occurrences of the field are returned:
530  
531 ### Get all 'Received:' entries:
532 my @all_received = $head->get('received');
533  
534 B<NOTE>: The header(s) returned may end with a newline. If you don't
535 want this, then B<chomp> the return value.
536  
537 =cut
538  
539 ### Inherited.
540  
541  
542 #------------------------------
543  
544 =item get_all FIELD
545  
546 I<Instance method.>
547 Returns the list of I<all> occurrences of the field, or the
548 empty list if the field is not present:
549  
550 ### How did it get here?
551 @history = $head->get_all('Received');
552  
553 B<Note:> I had originally experimented with having C<get()> return all
554 occurrences when invoked in an array context... but that causes a lot of
555 accidents when you get careless and do stuff like this:
556  
557 print "\u$field: ", $head->get($field);
558  
559 It also made the intuitive behaviour unclear if the INDEX argument
560 was given in an array context. So I opted for an explicit approach
561 to asking for all occurrences.
562  
563 =cut
564  
565 21 ▶sub get_all {
Subroutine get_all: called CC=2 SCAR=7.1
Pod documented
566 21 my ($self, $tag) = @_;
567 21 ▶ $self->count($tag) or return (); ### empty if doesn't exist
Branch: 1 branch
BranchTrueFalseTotal
$self->count($tag) or return () 21 0 21
568 21 ($self->get($tag));
569 }
570  
571 #------------------------------
572 #
573 # original_text
574 #
575 # Instance method, DEPRECATED.
576 # Return an approximation of the original text.
577 #
578 0 ▶sub original_text {
Subroutine original_text: not called CC=1 SCAR=6.9
Pod undocumented
579 0 usage "deprecated: use stringify() instead";
580 0 shift->stringify(@_);
581 }
582  
583 #------------------------------
584  
585 =item print [OUTSTREAM]
586  
587 I<Instance method, override.>
588 Print the header out to the given OUTSTREAM, or the currently-selected
589 filehandle if none. The OUTSTREAM may be a filehandle, or any object
590 that responds to a print() message.
591  
592 The override actually lets you print to any object that responds to
593 a print() method. This is vital for outputting MIME entities to scalars.
594  
595 Also, it defaults to the I<currently-selected> filehandle if none is given
596 (not STDOUT!), so I<please> supply a filehandle to prevent confusion.
597  
598 =cut
599  
600 81 ▶sub print {
Subroutine print: called CC=2 SCAR=7.6
Pod documented
601 81 my ($self, $fh) = @_;
602 81 ▶ $fh ||= select;
Condition33%1/3
$fh ||= select
l!l&&r!l&&!r
8100
MC/DC0%0/2
$fh ||= select
$fh select
Truth table33%1/3
A ||= B
A= $fhB= select
ABresult
1X1
011
000
603 81 $fh->print($self->as_string);
604 }
605  
606 #------------------------------
607 #
608 # set TAG,TEXT
609 #
610 # Instance method, DEPRECATED.
611 # Set the field named TAG to [the single occurrence given by the TEXT.
612 #
613 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
614 0 my $self = shift;
615 0 usage "deprecated: use the replace() method instead.";
616 0 $self->replace(@_);
617 }
618  
619 #------------------------------
620  
621 =item stringify
622  
623 I<Instance method.>
624 Return the header as a string. You can also invoke it as C<as_string>.
625  
626 If you set the variable $MIME::Entity::BOUNDARY_DELIMITER to a string,
627 that string will be used as line-end delimiter. If it is not set,
628 the line ending will be a newline character (\n)
629  
630 =cut
631  
632 84 ▶sub stringify {
Subroutine stringify: called CC=4 SCAR=14.0
Pod documented
633 84 my $self = shift; ### build clean header, and output...
634 84 ▶ my @header = grep {defined($_) ? $_ : ()} @{$self->header};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 424 0 424
635 84 ▶ my $header_delimiter = $MIME::Entity::BOUNDARY_DELIMITER || "\n";
Condition100%2/2
$MIME::Entity::BOUNDARY_DELIMITER || "\n"
l!l
777
MC/DC100%1/1
$MIME::Entity::BOUNDARY_DELIMITER || "\n"
$MIME::Entity::BOUNDARY_DELIMITER
Truth table100%2/2
A
A= $MIME::Entity::BOUNDARY_DELIMITER
Aresult
11
00
636 84 ▶ join "", map { /\n$/ ? substr($_, 0, -1) . $header_delimiter : $_ . $header_delimiter } @header;
Branch: 1 branch
BranchTrueFalseTotal
/\n$/ ? : 424 0 424
637 }
638 81 ▶sub as_string { shift->stringify(@_) }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
639  
640 #------------------------------
641  
642 =item unfold [FIELD]
643  
644 I<Instance method, inherited.>
645 Unfold (remove newlines in) the text of all occurrences of the given FIELD.
646 If the FIELD is omitted, I<all> fields are unfolded.
647 Returns the "self" object.
648  
649 =cut
650  
651 ### Inherited
652  
653  
654 #------------------------------
655  
656 =back
657  
658 =head2 MIME-specific methods
659  
660 All of the following methods extract information from the following fields:
661  
662 Content-type
663 Content-transfer-encoding
664 Content-disposition
665  
666 Be aware that they do not just return the raw contents of those fields,
667 and in some cases they will fill in sensible (I hope) default values.
668 Use C<get()> or C<mime_attr()> if you need to grab and process the
669 raw field text.
670  
671 B<Note:> some of these methods are provided both as a convenience and
672 for backwards-compatibility only, while others (like
673 recommended_filename()) I<really do have to be in MIME::Head to work
674 properly,> since they look for their value in more than one field.
675 However, if you know that a value is restricted to a single
676 field, you should really use the Mail::Field interface to get it.
677  
678 =over 4
679  
680 =cut
681  
682 #------------------------------
683  
684  
685 #------------------------------
686 #
687 # params TAG
688 #
689 # Instance method, DEPRECATED.
690 # Extract parameter info from a structured field, and return
691 # it as a hash reference. Provided for 1.0 compatibility only!
692 # Use the new MIME::Field interface classes (subclasses of Mail::Field).
693  
694 1 ▶sub params {
Subroutine params: called CC=1 SCAR=0.0
Pod undocumented
695 1 my ($self, $tag) = @_;
696 1 usage "deprecated: use the MIME::Field interface classes from now on!";
697 1 return MIME::Field::ParamVal->parse_params($self->get($tag,0));
698 }
699  
700 #------------------------------
701  
702 =item mime_attr ATTR,[VALUE]
703  
704 A quick-and-easy interface to set/get the attributes in structured
705 MIME fields:
706  
707 $head->mime_attr("content-type" => "text/html");
708 $head->mime_attr("content-type.charset" => "US-ASCII");
709 $head->mime_attr("content-type.name" => "homepage.html");
710  
711 This would cause the final output to look something like this:
712  
713 Content-type: text/html; charset=US-ASCII; name="homepage.html"
714  
715 Note that the special empty sub-field tag indicates the anonymous
716 first sub-field.
717  
718 B<Giving VALUE as undefined> will cause the contents of the named subfield
719 to be deleted:
720  
721 $head->mime_attr("content-type.charset" => undef);
722  
723 B<Supplying no VALUE argument> just returns the attribute's value,
724 or undefined if it isn't there:
725  
726 $type = $head->mime_attr("content-type"); ### text/html
727 $name = $head->mime_attr("content-type.name"); ### homepage.html
728  
729 In all cases, the new/current value is returned.
730  
731 The special sub-field tag C<@duplicate_parameters> (which can never be
732 a real tag) returns an arrayref of tags that were duplicated in the header,
733 or C<undef> if no such tags were found. For example, given the header:
734  
735 Content-Type: multipart/mixed; boundary="foo"; boundary="bar"
736  
737 Then:
738  
739 $head->mime_attr('content-type.@duplicate_parameters')
740  
741 would return:
742  
743 [ 'boundary' ]
744  
745 Similarly he special sub-field tag C<@empty_parameters> (which can never be
746 a real tag) returns an arrayref of tags that had an empty parameter value
747 in the header, or C<undef> if no such tags were found. For example, given
748 the header:
749  
750 Content-Type: multipart/mixed; boundary=;
751  
752 Then:
753  
754 $head->mime_attr('content-type.@emtpy_parameters')
755  
756 would return:
757  
758 [ 'boundary' ]
759  
760 A duplicate or empty "boundary" tag should be treated as a security risk, as
761 should duplicate Content-Type headers in a message. Since such messages
762 cannot be parsed unambiguously, we strongly recommend that they never be
763 delivered to end-users.
764  
765 Note also that a parameter explicitly set to blank via something like:
766  
767 Content-Type: application/octet-stream; boundary=""
768  
769 I<will> populate C<@empty_parameters>
770  
771 =cut
772  
773 15042 ▶sub mime_attr {
Subroutine mime_attr: called CC=3 SCAR=11.0
Pod documented
774 15042 my ($self, $attr, $value) = @_;
775  
776 ### Break attribute name up:
777 15042 my ($tag, $subtag) = split /\./, $attr;
778 15042 ▶ $subtag ||= '_';
Condition100%2/2
$subtag ||= "_"
l!l
106104432
MC/DC100%1/1
$subtag ||= "_"
$subtag
Truth table100%2/2
A
A= $subtag
Aresult
11
00
779  
780 ### Set or get?
781 15042 my $field = MIME::Field::ParamVal->parse($self->get($tag, 0));
782 15042 ▶ if (@_ > 2) { ### set it:
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) { } 12 15030 15042
783 12 $field->param($subtag, $value); ### set subfield
784 12 $self->replace($tag, $field->stringify); ### replace!
785 12 return $value;
786 }
787 else { ### get it:
788 15030 return $field->param($subtag);
789 }
790 }
791  
792 #------------------------------
793  
794 =item mime_encoding
795  
796 I<Instance method.>
797 Try I<real hard> to determine the content transfer encoding
798 (e.g., C<"base64">, C<"binary">), which is returned in all-lowercase.
799  
800 If no encoding could be found, the default of C<"7bit"> is returned
801 I quote from RFC 2045 section 6.1:
802  
803 This is the default value -- that is, "Content-Transfer-Encoding: 7BIT"
804 is assumed if the Content-Transfer-Encoding header field is not present.
805  
806 I do one other form of fixup: "7_bit", "7-bit", and "7 bit" are
807 corrected to "7bit"; likewise for "8bit".
808  
809 =cut
810  
811 332 ▶sub mime_encoding {
Subroutine mime_encoding: called CC=2 SCAR=6.9
Pod documented
812 332 my $self = shift;
813 332 ▶ my $enc = lc($self->mime_attr('content-transfer-encoding') || '7bit');
Condition100%2/2
$self->mime_attr('content-transfer-encoding') || '7bit'
l!l
174158
MC/DC100%1/1
$self->mime_attr('content-transfer-encoding') || '7bit'
$self->mime_attr('content-transfer-encoding')
Truth table100%2/2
A
A= $self->mime_attr('content-transfer-encoding')
Aresult
11
00
814 332 $enc =~ s{^([78])[ _-]bit\Z}{$1bit};
815 332 $enc;
816 }
817  
818 #------------------------------
819  
820 =item mime_type [DEFAULT]
821  
822 I<Instance method.>
823 Try C<real hard> to determine the content type (e.g., C<"text/plain">,
824 C<"image/gif">, C<"x-weird-type">, which is returned in all-lowercase.
825 "Real hard" means that if no content type could be found, the default
826 (usually C<"text/plain">) is returned. From RFC 2045 section 5.2:
827  
828 Default RFC 822 messages without a MIME Content-Type header are
829 taken by this protocol to be plain text in the US-ASCII character
830 set, which can be explicitly specified as:
831  
832 Content-type: text/plain; charset=us-ascii
833  
834 This default is assumed if no Content-Type header field is specified.
835  
836 Unless this is a part of a "multipart/digest", in which case
837 "message/rfc822" is the default. Note that you can also I<set> the
838 default, but you shouldn't: normally only the MIME parser uses this
839 feature.
840  
841 =cut
842  
843 4065 ▶sub mime_type {
Subroutine mime_type: called CC=6 SCAR=19.1
Pod documented
844 4065 my ($self, $default) = @_;
845 4065 ▶ $self->{MIH_DefaultType} = $default if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 4 4061 4065
846 my $s = $self->mime_attr('content-type') ||
847 $self->{MIH_DefaultType} ||
848 4065 ▶ 'text/plain';
Condition100%5/5
$self->mime_attr('content-type') || $self->{'MIH_DefaultType'} || 'text/plain'
l!l
3911154
$self->mime_attr("content-type") or $self->{"MIH_DefaultType"}
l!l&&r!l&&!r
39029154
MC/DC100%2/2
$self->mime_attr('content-type') || $self->{'MIH_DefaultType'} || 'text/plain'
$self->mime_attr("content-type") $self->{"MIH_DefaultType"}
Truth table100%3/3
A or B
A= $self->mime_attr("content-type")B= $self->{"MIH_DefaultType"}
ABresult
1X1
011
000
849 # avoid [perl #87336] bug, lc laundering tainted data
850 4065 ▶ return lc($s) if $] <= 5.008 || $] >= 5.014;
Branch: 1 branch
BranchTrueFalseTotal
if $] <= "5.008" or $] >= "5.014" 4065 0 4065
Condition33%1/3
$] <= "5.008" or $] >= "5.014"
l!l&&r!l&&!r
040650
MC/DC0%0/2
$] <= "5.008" or $] >= "5.014"
$] <= "5.008" $] >= "5.014"
Truth table33%1/3
A or B
A= $] <= "5.008"B= $] >= "5.014"
ABresult
1X1
011
000
851 0 $s =~ tr/A-Z/a-z/;
852 0 $s;
853 }
854  
855 #------------------------------
856  
857 =item multipart_boundary
858  
859 I<Instance method.>
860 If this is a header for a multipart message, return the
861 "encapsulation boundary" used to separate the parts. The boundary
862 is returned exactly as given in the C<Content-type:> field; that
863 is, the leading double-hyphen (C<-->) is I<not> prepended.
864  
865 Well, I<almost> exactly... this passage from RFC 2046 dictates
866 that we remove any trailing spaces:
867  
868 If a boundary appears to end with white space, the white space
869 must be presumed to have been added by a gateway, and must be deleted.
870  
871 Returns undef (B<not> the empty string) if either the message is not
872 multipart or if there is no specified boundary.
873  
874 =cut
875  
876 1602 ▶sub multipart_boundary {
Subroutine multipart_boundary: called CC=2 SCAR=6.9
Pod documented
877 1602 my $self = shift;
878 1602 my $value = $self->mime_attr('content-type.boundary');
879 1602 ▶ return undef unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1601 1602
880 1601 $value = decode_mimewords($value);
881 1601 $value =~ s/\s+$//; # kill trailing whitespace, per RFC 2046
882 1601 return $value;
883 }
884  
885 #------------------------------
886  
887 =item recommended_filename
888  
889 I<Instance method.>
890 Return the recommended external filename. This is used when
891 extracting the data from the MIME stream. The filename is always
892 returned as a string in Perl's internal format (the UTF8 flag may be on!)
893  
894 Returns undef if no filename could be suggested.
895  
896 =cut
897  
898 sub recommended_filename
899 190 ▶{
Subroutine recommended_filename: called CC=5 SCAR=16.1
Pod documented
900 190 my $self = shift;
901  
902 # Try these headers in order, taking the first defined,
903 # non-blank one we find.
904 190 my $wd = supported MIME::WordDecoder 'UTF-8';
905 190 foreach my $attr_name ( qw( content-disposition.filename content-type.name ) ) {
906 327 my $value = $self->mime_attr( $attr_name );
907 327 ▶ if ( defined $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne "" and $value =~ /\S/) 77 250 327
Condition83%5/6
defined $value and $value ne "" and $value =~ /\S/
!ll&&!rl&&r
247377
defined $value and $value ne ""
!ll&&!rl&&r
247080
MC/DC66%2/3
defined $value and $value ne "" and $value =~ /\S/
defined $value $value ne "" $value =~ /\S/
Truth table75%3/4
A and B and C
A= defined $valueB= $value ne ""C= $value =~ /\S/
ABCresult
0XX0
10X0
1100
1111
908 && $value ne ''
909 && $value =~ /\S/ ) {
910 77 return $wd->decode($value);
911 }
912 }
913  
914 113 return undef;
915 }
916  
917 #------------------------------
918  
919 =back
920  
921 =cut
922  
923  
924 #------------------------------
925 #
926 # tweak_FROM_parsing
927 #
928 # DEPRECATED. Use the inherited mail_from() class method now.
929  
930 0 ▶sub tweak_FROM_parsing {
Subroutine tweak_FROM_parsing: not called CC=1 SCAR=6.9
Pod undocumented
931 0 my $self = shift;
932 0 usage "deprecated. Use mail_from() instead.";
933 0 $self->mail_from(@_);
934 }
935  
936  
937 __END__
« Decoder/QuotedPrint.pm ↑ Summary Decoder/Gzip64.pm »