blib/lib/Email/MIME/ContentType.pm

filter: Statementstmt 88.8% 231 / 260 Branchbran 70.2% 118 / 168 Conditioncond 66.0% 35 / 53 MC/DCmcdc 30.3% 10 / 33 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 77.5% 414 / 534 SCAR 53.3
CC
109
Cov
80%
_build_attributes
38.9
_extract_attribute_value
22.4
_parse_attributes
15.1
CRAP 206.4
↑ Summary
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::MIME::ContentType 1.027;
4 # ABSTRACT: Parse and build a MIME Content-Type or Content-Disposition Header
5  
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Encode 2.87 qw(encode find_mime_encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Text::Unidecode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # If set, generate both foo*0=x and foo=x versions. -- rjbs, 2022-08-24
12 4 our $PRE_2231_FORM = 1;
13  
14 4 our @EXPORT = qw(parse_content_type parse_content_disposition build_content_type build_content_disposition);
15  
16 #pod =head1 SYNOPSIS
17 #pod
18 #pod use Email::MIME::ContentType;
19 #pod
20 #pod # Content-Type: text/plain; charset="us-ascii"; format=flowed
21 #pod my $ct = 'text/plain; charset="us-ascii"; format=flowed';
22 #pod my $data = parse_content_type($ct);
23 #pod
24 #pod $data = {
25 #pod type => "text",
26 #pod subtype => "plain",
27 #pod attributes => {
28 #pod charset => "us-ascii",
29 #pod format => "flowed"
30 #pod }
31 #pod };
32 #pod
33 #pod my $ct_new = build_content_type($data);
34 #pod # text/plain; charset=us-ascii; format=flowed
35 #pod
36 #pod
37 #pod # Content-Type: application/x-stuff;
38 #pod # title*0*=us-ascii'en'This%20is%20even%20more%20;
39 #pod # title*1*=%2A%2A%2Afun%2A%2A%2A%20;
40 #pod # title*2="isn't it!"
41 #pod my $ct = q(application/x-stuff;
42 #pod title*0*=us-ascii'en'This%20is%20even%20more%20;
43 #pod title*1*=%2A%2A%2Afun%2A%2A%2A%20;
44 #pod title*2="isn't it!");
45 #pod my $data = parse_content_type($ct);
46 #pod
47 #pod $data = {
48 #pod type => "application",
49 #pod subtype => "x-stuff",
50 #pod attributes => {
51 #pod title => "This is even more ***fun*** isn't it!"
52 #pod }
53 #pod };
54 #pod
55 #pod
56 #pod # Content-Disposition: attachment; filename=genome.jpeg;
57 #pod # modification-date="Wed, 12 Feb 1997 16:29:51 -0500"
58 #pod my $cd = q(attachment; filename=genome.jpeg;
59 #pod modification-date="Wed, 12 Feb 1997 16:29:51 -0500");
60 #pod my $data = parse_content_disposition($cd);
61 #pod
62 #pod $data = {
63 #pod type => "attachment",
64 #pod attributes => {
65 #pod filename => "genome.jpeg",
66 #pod "modification-date" => "Wed, 12 Feb 1997 16:29:51 -0500"
67 #pod }
68 #pod };
69 #pod
70 #pod my $cd_new = build_content_disposition($data);
71 #pod # attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"
72 #pod
73 #pod =cut
74  
75 4 our $STRICT_PARAMS = 1;
76  
77 4 my $ct_default = 'text/plain; charset=us-ascii';
78  
79 4 my $re_token = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+/; # US-ASCII except SPACE, CTLs and tspecials ()<>@,;:\\"/[]?=
80 4 my $re_token_non_strict = qr/([\x00-\x08\x0B\x0C\x0E-\x1F\x7E-\xFF]+|$re_token)/; # allow CTLs and above ASCII
81  
82 4 my $re_qtext = qr/[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7E\x7F]/; # US-ASCII except CR, LF, white space, backslash and quote
83 4 my $re_quoted_pair = qr/\\[\x00-\x7F]/;
84 4 my $re_quoted_string = qr/"((?:[ \t]*(?:$re_qtext|$re_quoted_pair))*[ \t]*)"/;
85  
86 4 my $re_qtext_non_strict = qr/[\x80-\xFF]|$re_qtext/;
87 4 my $re_quoted_pair_non_strict = qr/\\[\x00-\xFF]/;
88 4 my $re_quoted_string_non_strict = qr/"((?:[ \t]*(?:$re_qtext_non_strict|$re_quoted_pair_non_strict))*[ \t]*)"/;
89  
90 4 my $re_charset = qr/[!"#\$%&'+\-0-9A-Z\\\^_`a-z\{\|\}~]+/;
91 4 my $re_language = qr/[A-Za-z]{1,8}(?:-[0-9A-Za-z]{1,8})*/;
92 4 my $re_exvalue = qr/($re_charset)?'(?:$re_language)?'(.*)/;
93  
94 sub parse_content_type {
95 100 ▶ my $ct = shift;
Subroutine parse_content_type: called CC=9 SCAR=25.5
Pod documented
96  
97 # If the header isn't there or is empty, give default answer.
98 100 ▶ return parse_content_type($ct_default) unless defined $ct and length $ct;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ct and length $ct 2 98 100
Condition66%2/3
defined $ct and length $ct
!ll&&!rl&&r
0298
MC/DC50%1/2
defined $ct and length $ct
defined $ct length $ct
Truth table66%2/3
A and B
A= defined $ctB= length $ct
ABresult
0X0
100
111
99  
100 98 _unfold_lines($ct);
101 98 _clean_comments($ct);
102  
103 # It is also recommend (sic.) that this default be assumed when a
104 # syntactically invalid Content-Type header field is encountered.
105 98 ▶ unless ($ct =~ s/^($re_token)\/($re_token)//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ct =~ s[^($re_token)/($re_token)][]) 0 98 98
106 0 ▶ unless ($STRICT_PARAMS and $ct =~ s/^($re_token_non_strict)\/($re_token_non_strict)//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Email::MIME::ContentType::STRICT_PARAMS and $ct =~ s[^($re_token_non_strict)/($re_token_non_strict)][]) 0 0 0
Condition0%0/3
$Email::MIME::ContentType::STRICT_PARAMS and $ct =~ s[^($re_token_non_strict)/($re_token_non_strict)][]
!ll&&!rl&&r
000
MC/DC0%0/2
$Email::MIME::ContentType::STRICT_PARAMS and $ct =~ s[^($re_token_non_strict)/($re_token_non_strict)][]
$Email::MIME::ContentType::STRICT_PARAMS $ct =~ s[^($re_token_non_strict)/($re_token_non_strict)][]
Truth table0%0/3
A and B
A= $Email::MIME::ContentType::STRICT_PARAMSB= $ct =~ s[^($re_token_non_strict)/($re_token_non_strict)][]
ABresult
0X0
100
111
107 0 carp "Invalid Content-Type '$ct'";
108 0 return parse_content_type($ct_default);
109 }
110 }
111  
112 98 my ($type, $subtype) = (lc $1, lc $2);
113  
114 98 _clean_comments($ct);
115 98 $ct =~ s/\s+$//;
116  
117 98 my $attributes = {};
118 98 ▶ if ($STRICT_PARAMS and length $ct and $ct !~ /^;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Email::MIME::ContentType::STRICT_PARAMS and length $ct and not $ct =~ /^;/) { } 0 98 98
Condition83%5/6
$Email::MIME::ContentType::STRICT_PARAMS and length $ct and not $ct =~ /^;/
!ll&&!rl&&r
43550
$Email::MIME::ContentType::STRICT_PARAMS and length $ct
!ll&&!rl&&r
37655
MC/DC0%0/3
$Email::MIME::ContentType::STRICT_PARAMS and length $ct and not $ct =~ /^;/
$Email::MIME::ContentType::STRICT_PARAMS length $ct not $ct =~ /^;/
Truth table75%3/4
A and B and not(C)
A= $Email::MIME::ContentType::STRICT_PARAMSB= length $ctC= not $ct =~ /^;/
ABCresult
0XX0
10X0
1100
1111
119 0 carp "Missing semicolon before first Content-Type parameter '$ct'";
120 } else {
121 98 $attributes = _process_rfc2231(_parse_attributes($ct));
122 }
123  
124 return {
125 98 type => $type,
126 subtype => $subtype,
127 attributes => $attributes,
128  
129 # This is dumb. Really really dumb. For backcompat. -- rjbs,
130 # 2013-08-10
131 discrete => $type,
132 composite => $subtype,
133 };
134 }
135  
136 4 my $cd_default = 'attachment';
137  
138 sub parse_content_disposition {
139 47 ▶ my $cd = shift;
Subroutine parse_content_disposition: called CC=9 SCAR=25.5
Pod documented
140  
141 47 ▶ return parse_content_disposition($cd_default) unless defined $cd and length $cd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cd and length $cd 2 45 47
Condition66%2/3
defined $cd and length $cd
!ll&&!rl&&r
0245
MC/DC50%1/2
defined $cd and length $cd
defined $cd length $cd
Truth table66%2/3
A and B
A= defined $cdB= length $cd
ABresult
0X0
100
111
142  
143 45 _unfold_lines($cd);
144 45 _clean_comments($cd);
145  
146 45 ▶ unless ($cd =~ s/^($re_token)//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cd =~ s/^($re_token)//) 0 45 45
147 0 ▶ unless ($STRICT_PARAMS and $cd =~ s/^($re_token_non_strict)//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Email::MIME::ContentType::STRICT_PARAMS and $cd =~ s/^($re_token_non_strict)//) 0 0 0
Condition0%0/3
$Email::MIME::ContentType::STRICT_PARAMS and $cd =~ s/^($re_token_non_strict)//
!ll&&!rl&&r
000
MC/DC0%0/2
$Email::MIME::ContentType::STRICT_PARAMS and $cd =~ s/^($re_token_non_strict)//
$Email::MIME::ContentType::STRICT_PARAMS $cd =~ s/^($re_token_non_strict)//
Truth table0%0/3
A and B
A= $Email::MIME::ContentType::STRICT_PARAMSB= $cd =~ s/^($re_token_non_strict)//
ABresult
0X0
100
111
148 0 carp "Invalid Content-Disposition '$cd'";
149 0 return parse_content_disposition($cd_default);
150 }
151 }
152  
153 45 my $type = lc $1;
154  
155 45 _clean_comments($cd);
156 45 $cd =~ s/\s+$//;
157  
158 45 my $attributes = {};
159 45 ▶ if ($STRICT_PARAMS and length $cd and $cd !~ /^;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Email::MIME::ContentType::STRICT_PARAMS and length $cd and not $cd =~ /^;/) { } 0 45 45
Condition83%5/6
$Email::MIME::ContentType::STRICT_PARAMS and length $cd and not $cd =~ /^;/
!ll&&!rl&&r
17280
$Email::MIME::ContentType::STRICT_PARAMS and length $cd
!ll&&!rl&&r
10728
MC/DC0%0/3
$Email::MIME::ContentType::STRICT_PARAMS and length $cd and not $cd =~ /^;/
$Email::MIME::ContentType::STRICT_PARAMS length $cd not $cd =~ /^;/
Truth table75%3/4
A and B and not(C)
A= $Email::MIME::ContentType::STRICT_PARAMSB= length $cdC= not $cd =~ /^;/
ABCresult
0XX0
10X0
1100
1111
160 0 carp "Missing semicolon before first Content-Disposition parameter '$cd'";
161 } else {
162 45 $attributes = _process_rfc2231(_parse_attributes($cd));
163 }
164  
165 return {
166 45 type => $type,
167 attributes => $attributes,
168 };
169 }
170  
171 4 my $re_invalid_for_quoted_value = qr/[\x00-\x08\x0A-\x1F\x7F-\xFF]/; # non-US-ASCII and CTLs without SPACE and TAB
172 4 my $re_escape_extended_value = qr/[\x00-\x20\x7F-\xFF\*'%()<>@,;:\\"\/\[\]?=]/; # non-US-ASCII, SPACE, CTLs, *'% and tspecials ()<>@,;:\\"/[]?=
173  
174 sub build_content_type {
175 30 ▶ my $ct = shift;
Subroutine build_content_type: called CC=9 SCAR=23.2
Pod documented
176  
177 30 ▶ croak 'Missing Content-Type \'type\' parameter' unless exists $ct->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ct->{"type"} 0 30 30
178 30 ▶ croak 'Missing Content-Type \'subtype\' parameter' unless exists $ct->{subtype};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ct->{"subtype"} 0 30 30
179  
180 30 ▶ croak 'Invalid Content-Type \'type\' parameter' if $ct->{type} !~ /^(?:$re_token)*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ct->{"type"} =~ /^(?:$re_token)*$/ 0 30 30
181 30 ▶ croak 'Invalid Content-Type \'subtype\' parameter' if $ct->{subtype} !~ /^(?:$re_token)*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ct->{"subtype"} =~ /^(?:$re_token)*$/ 0 30 30
182  
183 30 ▶ croak 'Too long Content-Type \'type\' and \'subtype\' parameters' if length($ct->{type}) + length($ct->{subtype}) > 76;
Branch: 1 branch
BranchTrueFalseTotal
if length($ct->{"type"}) + length($ct->{"subtype"}) > 76 0 30 30
184  
185 30 my ($extra) = grep !/(?:type|subtype|attributes)/, sort keys %{$ct};
186 30 ▶ croak "Extra Content-Type '$extra' parameter" if defined $extra;
Branch: 1 branch
BranchTrueFalseTotal
if defined $extra 0 30 30
187  
188 30 my $ret = $ct->{type} . '/' . $ct->{subtype};
189 30 ▶ my $attrs = exists $ct->{attributes} ? _build_attributes($ct->{attributes}) : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $ct->{'attributes'} ? : 30 0 30
190 30 ▶ $ret .= "; $attrs" if length($attrs);
Branch: 1 branch
BranchTrueFalseTotal
if length $attrs 26 4 30
191 30 return $ret;
192 }
193  
194 sub build_content_disposition {
195 26 ▶ my $cd = shift;
Subroutine build_content_disposition: called CC=7 SCAR=20.2
Pod documented
196  
197 26 ▶ croak 'Missing Content-Type \'type\' parameter' unless exists $cd->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cd->{"type"} 0 26 26
198  
199 26 ▶ croak 'Invalid Content-Type \'type\' parameter' if $cd->{type} !~ /^(?:$re_token)*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"type"} =~ /^(?:$re_token)*$/ 0 26 26
200  
201 26 ▶ croak 'Too long Content-Type \'type\' parameter' if length($cd->{type}) > 77;
Branch: 1 branch
BranchTrueFalseTotal
if length $cd->{"type"} > 77 0 26 26
202  
203 26 my ($extra) = grep !/(?:type|attributes)/, sort keys %{$cd};
204 26 ▶ croak "Extra Content-Type '$extra' parameter" if defined $extra;
Branch: 1 branch
BranchTrueFalseTotal
if defined $extra 0 26 26
205  
206 26 my $ret = $cd->{type};
207 26 ▶ my $attrs = exists $cd->{attributes} ? _build_attributes($cd->{attributes}) : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $cd->{'attributes'} ? : 26 0 26
208 26 ▶ $ret .= "; $attrs" if length($attrs);
Branch: 1 branch
BranchTrueFalseTotal
if length $attrs 22 4 26
209 26 return $ret;
210 }
211  
212 sub _build_attributes {
213 56 ▶ my $attributes = shift;
Subroutine _build_attributes: called CC=37 SCAR=36.6
214  
215 56 my $ret = '';
216  
217 56 foreach my $key (sort keys %{$attributes}) {
218 76 my $value = $attributes->{$key};
219 76 my $ascii_value = $value;
220 76 my @continuous_value;
221 my $extended_value_charset;
222  
223 76 ▶ croak "Invalid attribute '$key'" if $key =~ /$re_escape_extended_value/; # complement to attribute-char in 8bit space
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /$re_escape_extended_value/ 0 76 76
224 76 ▶ croak "Undefined attribute '$key'" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 76 76
225  
226 76 ▶ if ($value =~ /\P{ASCII}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\P{ASCII}/) 14 62 76
227 14 $ascii_value = unidecode($value);
228 14 $ascii_value =~ s/\P{ASCII}/_/g;
229 14 @continuous_value = map { encode('UTF-8', $_) } split //, $value;
230 14 $extended_value_charset = 'UTF-8';
231 }
232  
233 76 ▶ if ($ascii_value !~ /^(?:$re_token)*$/ or $ascii_value =~ /'/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ascii_value =~ /^(?:$re_token)*$/ or $ascii_value =~ /'/) 40 36 76
Condition100%3/3
not $ascii_value =~ /^(?:$re_token)*$/ or $ascii_value =~ /'/
l!l&&r!l&&!r
38236
MC/DC100%2/2
not $ascii_value =~ /^(?:$re_token)*$/ or $ascii_value =~ /'/
not $ascii_value =~ /^(?:$re_token)*$/ $ascii_value =~ /'/
Truth table100%3/3
not(A) or B
A= not $ascii_value =~ /^(?:$re_token)*$/B= $ascii_value =~ /'/
ABresult
1X1
011
000
234 40 ▶ if ($ascii_value =~ /$re_invalid_for_quoted_value/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ascii_value =~ /$re_invalid_for_quoted_value/) 0 40 40
235 0 ▶ @continuous_value = split //, $value unless @continuous_value;
Branch: 1 branch
BranchTrueFalseTotal
unless @continuous_value 0 0 0
236 0 $ascii_value =~ s/[\n\r]/ /g;
237 0 $ascii_value =~ s/$re_invalid_for_quoted_value/_/g;
238 }
239 40 $ascii_value =~ s/(["\\])/\\$1/g;
240 40 $ascii_value = "\"$ascii_value\"";
241 }
242  
243 76 ▶ if (length($key) + length($ascii_value) > 75) { # length(" $key=$ascii_value;") > 78
Branch: 1 branch
BranchTrueFalseTotal
if (length($key) + length($ascii_value) > 75) 6 70 76
244 6 ▶ croak "Too long attribute '$key'" if length($key) > 71; # length(" $key=...;") > 78
Branch: 1 branch
BranchTrueFalseTotal
if length $key > 71 0 6 6
245 6 ▶ my $pos = $ascii_value =~ /"$/ ? 71 : 72;
Branch: 1 branch
BranchTrueFalseTotal
$ascii_value =~ /"$/ ? : 4 2 6
246 6 substr($ascii_value, $pos - length($key), length($ascii_value) + length($key) - 72, '...');
247 6 ▶ @continuous_value = split //, $value unless @continuous_value;
Branch: 1 branch
BranchTrueFalseTotal
unless @continuous_value 6 0 6
248 }
249  
250 76 ▶ if (@continuous_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (@continuous_value) 20 56 76
251 20 my $needs_quote;
252 20 ▶ unless (defined $extended_value_charset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $extended_value_charset) 6 14 20
253 6 ▶ $needs_quote = 1 if grep { $_ !~ /^(?:$re_token)*$/ or $_ =~ /'/ } @continuous_value;
Branch: 2 branches
BranchTrueFalseTotal
if grep {not $_ =~ /^(?:$re_token)*$/ or $_ =~ /'/;} @continuous_value 4 2 6
not $_ =~ /^(?:$re_token)*$/ or $_ =~ /'/ 390 18 408
254 6 ▶ $extended_value_charset = 'US-ASCII' if $needs_quote and grep /$re_invalid_for_quoted_value/, @continuous_value;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_quote and grep /$re_invalid_for_quoted_value/, @continuous_value 0 6 6
Condition66%2/3
$needs_quote and grep /$re_invalid_for_quoted_value/, @continuous_value
!ll&&!rl&&r
240
MC/DC0%0/2
$needs_quote and grep /$re_invalid_for_quoted_value/, @continuous_value
$needs_quote grep /$re_invalid_for_quoted_value/, @continuous_value
Truth table66%2/3
A and B
A= $needs_quoteB= grep /$re_invalid_for_quoted_value/, @continuous_value
ABresult
0X0
100
111
255 }
256  
257 20 my $add_param_len = 4; # for '; *='
258 20 ▶ if (defined $extended_value_charset) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $extended_value_charset) { } 14 6 20
elsif ($needs_quote) { } 4 2 6
259 14 $_ =~ s/($re_escape_extended_value)/sprintf('%%%02X', ord($1))/eg foreach @continuous_value;
260 14 substr($continuous_value[0], 0, 0, "$extended_value_charset''");
261 14 $add_param_len += 1; # for '*' - charset
262 } elsif ($needs_quote) {
263 4 $_ =~ s/(["\\])/\\$1/g foreach @continuous_value;
264 4 $add_param_len += 2; # for quotes
265 }
266  
267 20 ▶ if ($value =~ /\P{ASCII}/ and length(my $oneparameter = "; $key*=" . join '', @continuous_value) <= 78) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\P{ASCII}/ and length((my $oneparameter = "; $key*=" . join('', @continuous_value))) <= 78) { } 8 12 20
Condition100%3/3
$value =~ /\P{ASCII}/ and length((my $oneparameter = "; $key*=" . join("", @continuous_value))) <= 78
!ll&&!rl&&r
668
MC/DC100%2/2
$value =~ /\P{ASCII}/ and length((my $oneparameter = "; $key*=" . join("", @continuous_value))) <= 78
$value =~ /\P{ASCII}/ length((my $oneparameter = "; $key*=" . join("", @continuous_value))) <= 78
Truth table100%3/3
A and B
A= $value =~ /\P{ASCII}/B= length((my $oneparameter = "; $key*=" . join("", @continuous_value))) <= 78
ABresult
0X0
100
111
268 8 $ret .= $oneparameter;
269 } else {
270 12 my $buf = '';
271 12 my $count = 0;
272 12 foreach (@continuous_value) {
273 616 ▶ if (length($key) + length($count) + length($buf) + length($_) + $add_param_len > 78) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($key) + length($count) + length($buf) + length($_) + $add_param_len > 78) 12 604 616
274 12 ▶ $buf = "\"$buf\"" if $needs_quote;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_quote 4 8 12
275 12 my $parameter = "; $key*$count";
276 12 ▶ $parameter .= '*' if defined $extended_value_charset;
Branch: 1 branch
BranchTrueFalseTotal
if defined $extended_value_charset 6 6 12
277 12 $parameter .= "=$buf";
278 12 ▶ croak "Too long attribute '$key'" if length($parameter) > 78;
Branch: 1 branch
BranchTrueFalseTotal
if length $parameter > 78 0 12 12
279 12 $ret .= $parameter;
280 12 $buf = '';
281 12 $count++;
282 }
283 616 $buf .= $_;
284 }
285 12 ▶ if (length($buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) 12 0 12
286 12 ▶ $buf = "\"$buf\"" if $needs_quote;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_quote 4 8 12
287 12 my $parameter = "; $key*$count";
288 12 ▶ $parameter .= '*' if defined $extended_value_charset;
Branch: 1 branch
BranchTrueFalseTotal
if defined $extended_value_charset 6 6 12
289 12 $parameter .= "=$buf";
290 12 ▶ croak "Too long attribute '$key'" if length($parameter) > 78;
Branch: 1 branch
BranchTrueFalseTotal
if length $parameter > 78 0 12 12
291 12 $ret .= $parameter;
292 }
293 }
294 }
295  
296 76 ▶ if (! @continuous_value || $PRE_2231_FORM) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @continuous_value or $Email::MIME::ContentType::PRE_2231_FORM) 66 10 76
Condition100%3/3
not @continuous_value or $Email::MIME::ContentType::PRE_2231_FORM
l!l&&r!l&&!r
561010
MC/DC100%2/2
not @continuous_value or $Email::MIME::ContentType::PRE_2231_FORM
not @continuous_value $Email::MIME::ContentType::PRE_2231_FORM
Truth table100%3/3
not(A) or B
A= not @continuous_valueB= $Email::MIME::ContentType::PRE_2231_FORM
ABresult
1X1
011
000
297 66 $ret .= "; $key=$ascii_value";
298 }
299 }
300  
301 56 ▶ substr($ret, 0, 2, '') if length $ret;
Branch: 1 branch
BranchTrueFalseTotal
if length $ret 48 8 56
302 56 return $ret;
303 }
304  
305 sub _unfold_lines {
306 143 ▶ $_[0] =~ s/(?:\r\n|[\r\n])(?=[ \t])//g;
Subroutine _unfold_lines: called CC=1 SCAR=0.0
307 }
308  
309 sub _clean_comments {
310 1194 ▶ my $ret = ($_[0] =~ s/^\s+//);
Subroutine _clean_comments: called CC=10 SCAR=23.2
311 1194 ▶ while (length $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0]) 962 264 1226
312 962 ▶ last unless $_[0] =~ s/^\(//;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ s/^\(// 930 32 962
313 32 my $level = 1;
314 32 ▶ while (length $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0]) 408 0 408
315 408 my $ch = substr $_[0], 0, 1, '';
316 408 ▶ if ($ch eq '(') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ch eq '(') { } 10 398 408
elsif ($ch eq ')') { } 42 356 398
elsif ($ch eq '\\') { } 12 344 356
317 10 $level++;
318 } elsif ($ch eq ')') {
319 42 $level--;
320 42 ▶ last if $level == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 0 32 10 42
321 } elsif ($ch eq '\\') {
322 12 substr $_[0], 0, 1, '';
323 }
324 }
325 32 ▶ carp "Unbalanced comment" if $level != 0 and $STRICT_PARAMS;
Branch: 1 branch
BranchTrueFalseTotal
if $level != 0 and $Email::MIME::ContentType::STRICT_PARAMS 0 32 32
Condition33%1/3
$level != 0 and $Email::MIME::ContentType::STRICT_PARAMS
!ll&&!rl&&r
3200
MC/DC0%0/2
$level != 0 and $Email::MIME::ContentType::STRICT_PARAMS
$level != 0 $Email::MIME::ContentType::STRICT_PARAMS
Truth table33%1/3
A and B
A= $level != 0B= $Email::MIME::ContentType::STRICT_PARAMS
ABresult
0X0
100
111
326 32 $ret |= ($_[0] =~ s/^\s+//);
327 }
328 1194 return $ret;
329 }
330  
331 sub _process_rfc2231 {
332 143 ▶ my ($attribs) = @_;
Subroutine _process_rfc2231: called CC=12 SCAR=25.0
333 143 my %cont;
334 my %encoded;
335  
336 143 foreach (keys %{$attribs}) {
337 225 ▶ next unless $_ =~ m/^(.*)\*([0-9]+)\*?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /^(.*)\*([0-9]+)\*?$/ 147 78 225
338 78 my ($attr, $sec) = ($1, $2);
339 78 $cont{$attr}->{$sec} = $attribs->{$_};
340 78 ▶ $encoded{$attr} = 1 if $_ =~ m/\*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /\*$/ 46 32 78
341 78 delete $attribs->{$_};
342 }
343  
344 143 foreach (keys %cont) {
345 32 my $key = $_;
346 32 ▶ $key .= '*' if $encoded{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $encoded{$_} 24 8 32
347 32 $attribs->{$key} = join '', @{$cont{$_}}{sort { $a <=> $b } keys %{$cont{$_}}};
348 }
349  
350 143 foreach (keys %{$attribs}) {
351 176 ▶ next unless $_ =~ m/^(.*)\*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /^(.*)\*$/ 134 42 176
352 42 my $key = $1;
353 42 ▶ next unless defined $attribs->{$_} and $attribs->{$_} =~ m/^$re_exvalue$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribs->{$_} and $attribs->{$_} =~ /^$re_exvalue$/ 0 42 42
Condition33%1/3
defined $attribs->{$_} and $attribs->{$_} =~ /^$re_exvalue$/
!ll&&!rl&&r
0042
MC/DC0%0/2
defined $attribs->{$_} and $attribs->{$_} =~ /^$re_exvalue$/
defined $attribs->{$_} $attribs->{$_} =~ /^$re_exvalue$/
Truth table33%1/3
A and B
A= defined $attribs->{$_}B= $attribs->{$_} =~ /^$re_exvalue$/
ABresult
0X0
100
111
354 42 my ($charset, $value) = ($1, $2);
355 42 $value =~ s/%([0-9A-Fa-f]{2})/pack('C', hex($1))/eg;
356 42 ▶ if (length $charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $charset) 30 12 42
357 30 my $enc = find_mime_encoding($charset);
358 30 ▶ if (defined $enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $enc) { } 30 0 30
359 30 $value = $enc->decode($value);
360 } else {
361 0 carp "Unknown charset '$charset' in attribute '$key' value";
362 }
363 }
364 42 $attribs->{$key} = $value;
365 42 delete $attribs->{$_};
366 }
367  
368 143 return $attribs;
369 }
370  
371 sub _parse_attributes {
372 143 ▶ local $_ = shift;
Subroutine _parse_attributes: called CC=12 SCAR=27.1
373 143 ▶ substr($_, 0, 0, '; ') if length $_ and $_ !~ /^;/;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ and not $_ =~ /^;/ 0 143 143
Condition66%2/3
length $_ and not $_ =~ /^;/
!ll&&!rl&&r
181250
MC/DC0%0/2
length $_ and not $_ =~ /^;/
length $_ not $_ =~ /^;/
Truth table66%2/3
A and not(B)
A= length $_B= not $_ =~ /^;/
ABresult
0X0
100
111
374 143 my $attribs = {};
375  
376 143 ▶ while (length $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 229 139 368
377 229 ▶ s/^;// or $STRICT_PARAMS and do {
Branch: 1 branch
BranchTrueFalseTotal
s/^;// or $Email::MIME::ContentType::STRICT_PARAMS and do { carp("Missing semicolon before parameter '${_}'"); return $attribs } 2 227 229
Condition60%3/5
s/^;// || $Email::MIME::ContentType::STRICT_PARAMS && do { carp("Missing semicolon before parameter '${_}'"); return $attribs }
l!l
2272
$Email::MIME::ContentType::STRICT_PARAMS and do { carp("Missing semicolon before parameter '${_}'"); return $attribs }
!ll&&!rl&&r
200
MC/DC0%0/3
s/^;// || $Email::MIME::ContentType::STRICT_PARAMS && do { carp("Missing semicolon before parameter '${_}'"); return $attribs }
s/^;// $Email::MIME::ContentType::STRICT_PARAMS do { carp("Missing semicolon before parameter '${_}'"); return $attribs }
Truth table0%0/4
A || B and C
A= s/^;//B= $Email::MIME::ContentType::STRICT_PARAMSC= do { carp("Missing semicolon before parameter '${_}'"); return $attribs }
ABCresult
1XX1
0111
00X0
0100
378 0 carp "Missing semicolon before parameter '$_'";
379 0 return $attribs;
380 };
381  
382 229 _clean_comments($_);
383  
384 229 ▶ unless (length $_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $_) 4 225 229
385 # Some mail software generates a Content-Type like this:
386 # "Content-Type: text/plain;"
387 # RFC 1521 section 3 says a parameter must exist if there is a
388 # semicolon.
389 4 ▶ carp "Extra semicolon after last parameter" if $STRICT_PARAMS;
Branch: 1 branch
BranchTrueFalseTotal
if $Email::MIME::ContentType::STRICT_PARAMS 0 4 4
390 4 return $attribs;
391 }
392  
393 225 my $attribute;
394 225 ▶ if (s/^($re_token)=//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^($re_token)=//) { } 224 1 225
395 224 $attribute = lc $1;
396 } else {
397 1 ▶ if ($STRICT_PARAMS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Email::MIME::ContentType::STRICT_PARAMS) 0 1 1
398 0 carp "Illegal parameter '$_'";
399 0 return $attribs;
400 }
401 1 ▶ if (s/^($re_token_non_strict)=//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^($re_token_non_strict)=//) { } 0 1 1
402 0 $attribute = lc $1;
403 } else {
404 1 ▶ unless (s/^([^;=\s]+)\s*=//) {
Branch: 1 branch
BranchTrueFalseTotal
unless (s/^([^;=\s]+)\s*=//) 0 1 1
405 0 carp "Cannot parse parameter '$_'";
406 0 return $attribs;
407 }
408 1 $attribute = lc $1;
409 }
410 }
411  
412 225 _clean_comments($_);
413 225 my $value = _extract_attribute_value();
414 225 $attribs->{$attribute} = $value;
415 225 _clean_comments($_);
416 }
417  
418 139 return $attribs;
419 }
420  
421 sub _extract_attribute_value { # EXPECTS AND MODIFIES $_
422 225 ▶ my $value;
Subroutine _extract_attribute_value: called CC=12 SCAR=31.1
423 225 ▶ while (length $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 229 0 229
424 229 ▶ if (s/^($re_token)//) {
Branch: 5 branches
BranchTrueFalseTotal
if (s/^($re_token)//) { } 136 93 229
elsif (s/^$re_quoted_string//) { } 93 0 93
elsif ($Email::MIME::ContentType::STRICT_PARAMS) { } 0 0 0
elsif (s/^($re_token_non_strict)//) { } 0 0 0
elsif (s/^$re_quoted_string_non_strict//) { } 0 0 0
425 136 $value .= $1;
426 } elsif (s/^$re_quoted_string//) {
427 93 my $sub = $1;
428 93 $sub =~ s/\\(.)/$1/g;
429 93 $value .= $sub;
430 } elsif ($STRICT_PARAMS) {
431 0 my $char = substr $_, 0, 1;
432 0 carp "Unquoted '$char' not allowed";
433 0 return;
434 } elsif (s/^($re_token_non_strict)//) {
435 0 $value .= $1;
436 } elsif (s/^$re_quoted_string_non_strict//) {
437 0 my $sub = $1;
438 0 $sub =~ s/\\(.)/$1/g;
439 0 $value .= $sub;
440 }
441  
442 229 my $erased = _clean_comments($_);
443 229 ▶ last if !length $_ or /^;/;
Branch: 1 branch
BranchTrueFalseTotal
if not length $_ or /^;/ 223 6 229
Condition100%3/3
not length $_ or /^;/
l!l&&r!l&&!r
1211026
MC/DC100%2/2
not length $_ or /^;/
not length $_ /^;/
Truth table100%3/3
not(A) or B
A= not length $_B= /^;/
ABresult
1X1
011
000
444 6 ▶ if ($STRICT_PARAMS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Email::MIME::ContentType::STRICT_PARAMS) 0 6 6
445 0 my $char = substr $_, 0, 1;
446 0 carp "Extra '$char' found after parameter";
447 0 return;
448 }
449  
450 6 ▶ if ($erased) {
Branch: 1 branch
BranchTrueFalseTotal
if ($erased) 6 0 6
451 # Sometimes semicolon is missing, so check for = char
452 6 ▶ last if m/^$re_token_non_strict=/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$re_token_non_strict=/ 2 4 6
453 4 $value .= ' ';
454 }
455  
456 4 $value .= substr $_, 0, 1, '';
457 }
458 225 return $value;
459 }
460  
461 4 1;
462  
463 #pod =func parse_content_type
464 #pod
465 #pod This routine is exported by default.
466 #pod
467 #pod This routine parses email content type headers according to section 5.1 of RFC
468 #pod 2045 and also RFC 2231 (Character Set and Parameter Continuations). It returns
469 #pod a hash as above, with entries for the C<type>, the C<subtype>, and a hash of
470 #pod C<attributes>.
471 #pod
472 #pod For backward compatibility with a really unfortunate misunderstanding of RFC
473 #pod 2045 by the early implementors of this module, C<discrete> and C<composite> are
474 #pod also present in the returned hashref, with the values of C<type> and C<subtype>
475 #pod respectively.
476 #pod
477 #pod =func parse_content_disposition
478 #pod
479 #pod This routine is exported by default.
480 #pod
481 #pod This routine parses email Content-Disposition headers according to RFC 2183 and
482 #pod RFC 2231. It returns a hash as above, with entries for the C<type>, and a hash
483 #pod of C<attributes>.
484 #pod
485 #pod =func build_content_type
486 #pod
487 #pod This routine is exported by default.
488 #pod
489 #pod This routine builds email Content-Type header according to RFC 2045 and RFC 2231.
490 #pod It takes a hash as above, with entries for the C<type>, the C<subtype>, and
491 #pod optionally also a hash of C<attributes>. It returns a string representing
492 #pod Content-Type header. Non-ASCII attributes are encoded to UTF-8 according to
493 #pod Character Set section of RFC 2231. Attribute which has more then 78 ASCII
494 #pod characters is split into more attributes accorrding to Parameter Continuations
495 #pod of RFC 2231.
496 #pod
497 #pod For compatibility reasons with clients which do not support RFC 2231, output
498 #pod string contains also truncated ASCII version of any too long or non-ASCII
499 #pod attribute. Encoding to ASCII is done via Text::Unidecode module. This
500 #pod behavior can cause confusion by 2231-compatible MIME implementations, and can
501 #pod be disabled by setting C<$Email::MIME::ContentType::STRICT> to true.
502 #pod
503 #pod =func build_content_disposition
504 #pod
505 #pod This routine is exported by default.
506 #pod
507 #pod This routine builds email Content-Disposition header according to RFC 2182 and
508 #pod RFC 2231. It takes a hash as above, with entries for the C<type>, and
509 #pod optionally also a hash of C<attributes>. It returns a string representing
510 #pod Content-Disposition header. Non-ASCII or too long attributes are handled in
511 #pod the same way like in L<build_content_type function|/build_content_type>.
512 #pod
513 #pod =head1 WARNINGS
514 #pod
515 #pod This is not a valid content-type header, according to both RFC 1521 and RFC
516 #pod 2045:
517 #pod
518 #pod Content-Type: type/subtype;
519 #pod
520 #pod If a semicolon appears, a parameter must. C<parse_content_type> will carp if
521 #pod it encounters a header of this type, but you can suppress this by setting
522 #pod C<$Email::MIME::ContentType::STRICT_PARAMS> to a false value. Please consider
523 #pod localizing this assignment!
524 #pod
525 #pod Same applies for C<parse_content_disposition>.
526 #pod
527 #pod =cut
528  
529 __END__
↑ Summary