DKIM/Canonicalization/DkimCommon.pm

filter: Statementstmt 85.7% 54 / 63 Branchbran 70.8% 17 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 11 / 13 Podpod 25.0% 2 / 8 totaltotal 77.7% 84 / 108 CC 15 SCAR 14.4
CC
15
Cov
82%
finish_header
8.0
add_body
5.7
_group_headers
3.0
CRAP 4.2
« DKIM/PrivateKey.pm ↑ Summary DKIM/Canonicalization/relaxed.pm »
1 package Mail::DKIM::Canonicalization::DkimCommon;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 our $VERSION = '1.20261105'; # VERSION
5 # ABSTRACT: common canonicalization
6  
7 # Copyright 2005-2007 Messiah College. All rights reserved.
8 # Jason Long <jlong@messiah.edu>
9  
10 # Copyright (c) 2004 Anthony D. Urso. All rights reserved.
11 # This program is free software; you can redistribute it and/or
12 # modify it under the same terms as Perl itself.
13  
14 19 ▶use base 'Mail::DKIM::Canonicalization::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1251 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
18 1251 my $self = shift;
19 1251 $self->SUPER::init;
20  
21 1251 $self->{body_count} = 0;
22 1251 $self->{body_truncated} = 0;
23  
24 # these canonicalization methods require signature to use
25 $self->{Signature}
26 1251 ▶ or croak 'no signature specified';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"Signature"} or croak("no signature specified") 1251 0 1251
27 }
28  
29 # similar to code in DkCommon.pm
30 509 ▶sub add_header {
Subroutine add_header: called CC=1 SCAR=0.0
Pod undocumented
31  
32 #Note: canonicalization of headers is performed
33 #in finish_header()
34 }
35  
36 318 ▶sub finish_header {
Subroutine finish_header: called CC=8 SCAR=20.8
Pod documented
37 318 my $self = shift;
38 318 my %args = @_;
39  
40 # Headers are canonicalized in the order specified by the h= tag.
41 # However, in the case of multiple instances of the same header name,
42 # the headers will be canonicalized in reverse order (i.e. "from
43 # the bottom of the header field block to the top").
44 #
45 # This is described in 5.4 of RFC4871.
46  
47 # presence of a h= tag is mandatory...
48 318 ▶ unless ( defined $self->{Signature}->headerlist ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Signature"}->headerlist) 0 318 318
49 0 die "Error: h= tag is required for this canonicalization\n";
50 }
51  
52 # the message headers grouped by name, bottom-up, so each name in h=
53 # costs one lookup rather than a scan of the header; the grouping is
54 # shared through HeaderIndex by every signature on the message
55 318 ▶ my $heads = $args{HeaderIndex} ? $args{HeaderIndex}{dkim} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$args{'HeaderIndex'} ? : 295 23 318
56 318 ▶ unless ($heads) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($heads) 230 88 318
57 230 $heads = _group_headers( $args{Headers} );
58 230 ▶ $args{HeaderIndex}{dkim} = $heads if $args{HeaderIndex};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"HeaderIndex"} 207 23 230
59 }
60  
61 # iterate through the header field names specified in the signature
62 318 my %used;
63 318 my @sig_headers = $self->{Signature}->headerlist;
64 318 foreach my $hdr_name (@sig_headers) {
65  
66 # find the specified header in the message, counting how many of
67 # that name we've used, so if it occurs more than once, we'll get
68 # the next header in line
69 21456 my $name = lc $hdr_name;
70 21456 ▶ my $list = $heads->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $heads->{$name} or next 1413 20043 21456
71 1413 my $hdr = $list->[ $used{$name}++ ];
72 1413 ▶ next unless defined $hdr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hdr 4 1409 1413
73  
74 1409 $hdr =~ s/\015\012\z//s;
75 1409 $self->output( $self->canonicalize_header($hdr) . "\015\012" );
76 }
77 }
78  
79 # group the headers by lowercased name; since the bottom-most headers
80 # are to get precedence, each group is in reverse order (now the first
81 # header matching a particular name is the header to insert)
82 230 ▶sub _group_headers {
Subroutine _group_headers: called CC=3 SCAR=11.0
83 230 my ($headers) = @_;
84 230 my %heads;
85 230 foreach my $hdr ( reverse @$headers ) {
86 4767 ▶ next unless $hdr =~ /^([^\s:]+)\s*:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr =~ /^([^\s:]+)\s*:/ 4 4763 4767
87 4763 push @{ $heads{ lc $1 } }, $hdr;
88 }
89 230 return \%heads;
90 }
91  
92 345 ▶sub add_body {
Subroutine add_body: called CC=4 SCAR=17.3
Pod documented
93 345 my $self = shift;
94 345 my ($multiline) = @_;
95  
96 345 $multiline = $self->canonicalize_body($multiline);
97 345 ▶ if ( $self->{Signature} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Signature"}) 345 0 345
98 345 ▶ if ( my $limit = $self->{Signature}->body_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit = $self->{"Signature"}->body_count) 0 345 345
99 0 my $remaining = $limit - $self->{body_count};
100 0 ▶ if ( length($multiline) > $remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $multiline > $remaining) 0 0 0
101 0 $self->{body_truncated} += length($multiline) - $remaining;
102 0 $multiline = substr( $multiline, 0, $remaining );
103 }
104 }
105 }
106  
107 345 $self->{body_count} += length($multiline);
108 345 $self->output($multiline);
109 }
110  
111 541 ▶sub finish_body {
Subroutine finish_body: called CC=1 SCAR=0.0
Pod undocumented
112 }
113  
114 1 ▶sub finish_message {
Subroutine finish_message: called CC=2 SCAR=7.0
Pod undocumented
115 1 my $self = shift;
116  
117 1 ▶ if ( $self->{Signature} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Signature"}) 1 0 1
118 1 $self->output("\015\012");
119  
120 # append the DKIM-Signature (without data)
121 1 my $line = $self->{Signature}->as_string_without_data;
122  
123 # signature is subject to same canonicalization as headers
124 1 $self->output( $self->canonicalize_header($line) );
125 }
126 }
127  
128 0 ▶sub body_count {
Subroutine body_count: not called CC=1 SCAR=6.9
Pod undocumented
129 0 my $self = shift;
130 0 return $self->{body_count};
131 }
132  
133 0 ▶sub body_truncated {
Subroutine body_truncated: not called CC=1 SCAR=6.9
Pod undocumented
134 0 my $self = shift;
135 0 return $self->{body_truncated};
136 }
137  
138 19 1;
139  
140 __END__
« DKIM/PrivateKey.pm ↑ Summary DKIM/Canonicalization/relaxed.pm »