Courriel/Header.pm

filter: Statementstmt 36.3% 48 / 132 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 61.1% 11 / 18 Podpod 100% 2 / 2 totaltotal 29.6% 61 / 206 CC 23 SCAR 36.6
CC
23
Cov
27%
_encode_string
110.0
_maybe_encoded_address
42.0
_encoded_address_list
18.3
CRAP 38.7
↑ Summary Courriel/Headers.pm »
1 package Courriel::Header;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 our $VERSION = '0.49';
8  
9 9 ▶use Courriel::Helpers qw( fold_header );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Courriel::Types qw( NonEmptyStr Str Streamable );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Email::Address::XS qw( parse_email_groups );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Encode qw( encode find_encoding );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Params::ValidationCompiler qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 with 'Courriel::Role::Streams' => { -exclude => ['stream_to'] };
20  
21 9 has name => (
22 is => 'ro',
23 isa => NonEmptyStr,
24 required => 1,
25 );
26  
27 9 has value => (
28 is => 'ro',
29 isa => Str,
30 required => 1,
31 );
32  
33 {
34 9 my $validator = validation_for(
35 params => [
36 charset => { type => NonEmptyStr, default => 'utf8' },
37 output => { type => Streamable },
38 ],
39 named_to_list => 1,
40 );
41  
42 0 ▶ sub stream_to {
Subroutine stream_to: not called CC=1 SCAR=6.9
Pod documented
43 0 my $self = shift;
44 0 my ( $charset, $output ) = $validator->(@_);
45  
46 0 my $string = $self->name;
47 0 $string .= ': ';
48  
49 0 $string .= $self->_maybe_encoded_value($charset);
50  
51 0 $output->( fold_header($string) );
52  
53 0 return;
54 }
55 }
56  
57 9 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=4.1
Pod documented
58 0 my $self = shift;
59  
60 0 my $string = q{};
61  
62 0 $self->stream_to( output => $self->_string_output( \$string ), @_ );
63  
64 0 return $string;
65 }
66  
67 {
68 # RFC 2047 - An 'encoded-word' MUST NOT be used in a Received header
69 # field.
70 9 my %never_encode = map { lc $_ => 1 } qw( Received );
71 9 my %contains_addresses = map { lc $_ => 1 } qw( CC From To );
72  
73 # XXX - this really isn't very correct. Only certain types of values (per RFC
74 # 2047) can be encoded, not just any random text. I'm not sure how best to
75 # handle this. If we parsed an email that encoded stuff that shouldn't be
76 # encoded, what should we do? At the very least, we should add some checks to
77 # Courriel::Builder to ensure that people don't try to create an email with
78 # non-ASCII in certain parts of fields (like in email addresses).
79 0 ▶ sub _maybe_encoded_value {
Subroutine _maybe_encoded_value: not called CC=3 SCAR=24.8
80 0 my $self = shift;
81 0 my $charset = shift;
82  
83 return $self->value
84 0 ▶ if $never_encode{ lc $self->name };
Branch: 1 branch
BranchTrueFalseTotal
if $never_encode{lc $self->name} 0 0 0
85  
86 return $self->_encoded_address_list($charset)
87 0 ▶ if $contains_addresses{ lc $self->name };
Branch: 1 branch
BranchTrueFalseTotal
if $contains_addresses{lc $self->name} 0 0 0
88  
89 0 return $self->_encode_string( $self->value, $charset );
90 }
91 }
92  
93 9 ▶sub _encoded_address_list {
Subroutine _encoded_address_list: not called CC=4 SCAR=29.1
94 0 my $self = shift;
95 0 my $charset = shift;
96  
97 0 my @parsed = parse_email_groups( $self->value );
98 0 my @list;
99  
100 ## no critic (ControlStructures::ProhibitCStyleForLoops)
101 0 ▶ for ( my $i = 0; $i < @parsed; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @parsed) 0 0 0
102 0 my $group = $parsed[$i];
103 0 my $addresses = $parsed[ $i + 1 ];
104  
105 0 ▶ if ( defined $group ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group) { } 0 0 0
106 0 my $g = "$group:";
107 0 ▶ if ( @{$addresses} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$addresses;}) 0 0 0
108 0 $g .= q{ };
109 $g .= join ', ',
110 0 map { $self->_maybe_encoded_address( $_, $charset ) }
111 0 @{$addresses};
112 }
113 0 $g .= ';';
114 0 push @list, $g;
115 }
116 else {
117 push @list,
118 0 map { $self->_maybe_encoded_address( $_, $charset ) }
119 0 @{$addresses};
120 }
121 }
122  
123 0 return join ', ', @list;
124 }
125  
126 0 ▶sub _maybe_encoded_address {
Subroutine _maybe_encoded_address: not called CC=6 SCAR=37.4
127 0 my $self = shift;
128 0 my $address = shift;
129 0 my $charset = shift;
130  
131 0 my $encoded = q{};
132  
133 0 my $phrase = $address->phrase;
134 0 ▶ if ( defined $phrase && length $phrase ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $phrase and length $phrase) 0 0 0
Condition0%0/3
defined $phrase and length $phrase
!ll&&!rl&&r
000
MC/DC0%0/2
defined $phrase and length $phrase
defined $phrase length $phrase
Truth table0%0/3
A and B
A= defined $phraseB= length $phrase
ABresult
0X0
100
111
135 0 my $enc_phrase = $self->_encode_string( $phrase, $charset );
136  
137 # If the phrase wasn't encoded then we can make it a quoted-word, if
138 # it was encoded then it cannot be wrapped in quotes per RFC 2047.
139 0 ▶ if ( $enc_phrase ne $phrase ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc_phrase ne $phrase) { } 0 0 0
140 0 $encoded .= $enc_phrase;
141 }
142 else {
143 0 $encoded .= q{"} . $phrase . q{"};
144 }
145 0 $encoded .= q{ };
146 }
147  
148 0 $encoded .= '<' . $address->address . '>';
149  
150 0 my $comment = $address->comment;
151 0 ▶ if ( defined $comment && length $comment ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment and length $comment) 0 0 0
Condition0%0/3
defined $comment and length $comment
!ll&&!rl&&r
000
MC/DC0%0/2
defined $comment and length $comment
defined $comment length $comment
Truth table0%0/3
A and B
A= defined $commentB= length $comment
ABresult
0X0
100
111
152 0 $encoded .= '(' . $self->_encode_string( $comment, $charset ) . ')';
153 }
154  
155 0 return $encoded;
156 }
157  
158 {
159 9 my $header_chunk = qr/
160 (?:
161 ^
162 |
163 (?<ascii>[\x21-\x7e]+) # printable ASCII (excluding space, \x20)
164 |
165 (?<non_ascii>\S+) # anything that's not space
166 )
167 (?:
168 (?<ws>\s+)
169 |
170 $
171 )
172 /x;
173  
174 0 ▶ sub _encode_string {
Subroutine _encode_string: not called CC=10 SCAR=47.0
175 0 my $self = shift;
176 0 my $string = shift;
177 0 my $charset = shift;
178  
179 0 my @chunks;
180 0 ▶ while ( $string =~ /\G$header_chunk/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\G$header_chunk/g) 0 0 0
181 0 push @chunks, {%+};
182 }
183  
184 0 my @encoded;
185 0 for my $i ( 0 .. $#chunks ) {
186 0 ▶ if ( defined $chunks[$i]->{non_ascii} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chunks[$i]{'non_ascii'}) { } 0 0 0
187 my $to_encode
188 = $chunks[ $i + 1 ]
189 && defined $chunks[ $i + 1 ]{non_ascii}
190 ? $chunks[$i]{non_ascii} . ( $chunks[$i]{ws} // q{} )
191 0 ▶ : $chunks[$i]{non_ascii};
Branch: 1 branch
BranchTrueFalseTotal
$chunks[$i + 1] && defined $chunks[$i + 1]{'non_ascii'} ? : 0 0 0
Condition0%0/5
$chunks[$i + 1] and defined $chunks[$i + 1]{"non_ascii"}
!ll&&!rl&&r
000
$chunks[$i]{"ws"} // ""
l!l
00
MC/DC0%0/2
$chunks[$i + 1] and defined $chunks[$i + 1]{"non_ascii"}
$chunks[$i + 1] defined $chunks[$i + 1]{"non_ascii"}
MC/DC0%0/1
$chunks[$i]{"ws"} // ""
$chunks[$i]{"ws"}
Truth table0%0/3
A and B
A= $chunks[$i + 1]B= defined $chunks[$i + 1]{"non_ascii"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $chunks[$i]{"ws"}
Aresult
11
00
192  
193 0 push @encoded, $self->_mime_encode( $to_encode, $charset );
194 0 ▶ push @encoded, q{ } if $chunks[ $i + 1 ];
Branch: 1 branch
BranchTrueFalseTotal
if $chunks[$i + 1] 0 0 0
195 }
196 else {
197 push @encoded,
198 ( $chunks[$i]{ascii} // q{} )
199 0 ▶ . ( $chunks[$i]{ws} // q{} );
Condition0%0/4
$chunks[$i]{"ascii"} // ""
l!l
00
$chunks[$i]{"ws"} // ""
l!l
00
MC/DC0%0/1
$chunks[$i]{"ascii"} // ""
$chunks[$i]{"ascii"}
MC/DC0%0/1
$chunks[$i]{"ws"} // ""
$chunks[$i]{"ws"}
Truth table0%0/2
A
A= $chunks[$i]{"ascii"}
Aresult
11
00
Truth table0%0/2
A
A= $chunks[$i]{"ws"}
Aresult
11
00
200 }
201 }
202  
203 0 return join q{}, @encoded;
204 }
205 }
206  
207 9 ▶sub _mime_encode {
Subroutine _mime_encode: not called CC=4 SCAR=28.9
208 0 my $self = shift;
209 0 my $text = shift;
210 0 my $charset = find_encoding(shift)->mime_name;
211  
212 0 my $head = '=?' . $charset . '?B?';
213 0 my $tail = '?=';
214  
215 0 my $base_length = 75 - ( length($head) + length($tail) );
216  
217 # This code is copied from Mail::Message::Field::Full in the Mail-Box
218 # distro.
219 0 my $real_length = int( $base_length / 4 ) * 3;
220  
221 0 my @result;
222 0 my $chunk = q{};
223 0 ▶ while ( length( my $chr = substr( $text, 0, 1, q{} ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $chr = substr($text, 0, 1, ''))) 0 0 0
224 0 my $chr = encode( $charset, $chr, 0 );
225  
226 0 ▶ if ( length($chunk) + length($chr) > $real_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($chunk) + length($chr) > $real_length) 0 0 0
227 0 push @result, $head . encode_base64( $chunk, q{} ) . $tail;
228 0 $chunk = q{};
229 }
230  
231 0 $chunk .= $chr;
232 }
233  
234 0 ▶ push @result, $head . encode_base64( $chunk, q{} ) . $tail
Branch: 1 branch
BranchTrueFalseTotal
if length $chunk 0 0 0
235 if length $chunk;
236  
237 0 return join q{ }, @result;
238 }
239  
240 9 __PACKAGE__->meta->make_immutable;
241  
242 9 1;
243  
244 # ABSTRACT: A single header's name and value
245  
246 __END__
↑ Summary Courriel/Headers.pm »