File Coverage

blib/lib/Mail/DKIM/Canonicalization/DkimCommon.pm
Criterion Covered Total %
statement 46 55 83.6
branch 9 16 56.2
condition n/a
subroutine 10 12 83.3
pod 2 8 25.0
total 67 91 73.6


line stmt bran cond sub pod time code
1             package Mail::DKIM::Canonicalization::DkimCommon;
2 14     14   124 use strict;
  14         46  
  14         413  
3 14     14   74 use warnings;
  14         75  
  14         666  
4             our $VERSION = '1.20230911'; # VERSION
5             # ABSTRACT: common canonicalization
6              
7             # Copyright 2005-2007 Messiah College. All rights reserved.
8             # Jason Long
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 14     14   83 use base 'Mail::DKIM::Canonicalization::Base';
  14         38  
  14         7116  
15 14     14   103 use Carp;
  14         32  
  14         13559  
16              
17             sub init {
18 921     921 0 1505 my $self = shift;
19 921         2339 $self->SUPER::init;
20              
21 921         1726 $self->{body_count} = 0;
22 921         1528 $self->{body_truncated} = 0;
23              
24             # these canonicalization methods require signature to use
25             $self->{Signature}
26 921 50       2177 or croak 'no signature specified';
27             }
28              
29             # similar to code in DkCommon.pm
30       5902 0   sub add_header {
31              
32             #Note: canonicalization of headers is performed
33             #in finish_header()
34             }
35              
36             sub finish_header {
37 261     261 1 488 my $self = shift;
38 261         747 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             # Since the bottom-most headers are to get precedence, we reverse
48             # the headers here... (now the first header matching a particular
49             # name is the header to insert)
50 261         435 my @mess_headers = reverse @{ $args{Headers} };
  261         1154  
51              
52             # presence of a h= tag is mandatory...
53 261 50       871 unless ( defined $self->{Signature}->headerlist ) {
54 0         0 die "Error: h= tag is required for this canonicalization\n";
55             }
56              
57             # iterate through the header field names specified in the signature
58 261         714 my @sig_headers = $self->{Signature}->headerlist;
59 261         629 foreach my $hdr_name (@sig_headers) {
60 1486         2638 $hdr_name = lc $hdr_name;
61              
62             # find the specified header in the message
63             inner_loop:
64 1486         3421 for ( my $i = 0 ; $i < @mess_headers ; $i++ ) {
65 5634         8615 my $hdr = $mess_headers[$i];
66              
67 5634 100       17182 if ( $hdr =~ /^([^\s:]+)\s*:/ ) {
68 5632         11638 my $key = lc $1;
69 5632 100       13678 if ( $key eq $hdr_name ) {
70              
71             # found it
72              
73             # remove it from our list, so if it occurs more than
74             # once, we'll get the next header in line
75 1446         2541 splice @mess_headers, $i, 1;
76              
77 1446         4743 $hdr =~ s/\015\012\z//s;
78 1446         3953 $self->output(
79             $self->canonicalize_header($hdr) . "\015\012" );
80 1446         4486 last inner_loop;
81             }
82             }
83             }
84             }
85             }
86              
87             sub add_body {
88 260     260 1 434 my $self = shift;
89 260         732 my ($multiline) = @_;
90              
91 260         789 $multiline = $self->canonicalize_body($multiline);
92 260 50       711 if ( $self->{Signature} ) {
93 260 50       841 if ( my $limit = $self->{Signature}->body_count ) {
94 0         0 my $remaining = $limit - $self->{body_count};
95 0 0       0 if ( length($multiline) > $remaining ) {
96 0         0 $self->{body_truncated} += length($multiline) - $remaining;
97 0         0 $multiline = substr( $multiline, 0, $remaining );
98             }
99             }
100             }
101              
102 260         542 $self->{body_count} += length($multiline);
103 260         697 $self->output($multiline);
104             }
105              
106       419 0   sub finish_body {
107             }
108              
109             sub finish_message {
110 1     1 0 16 my $self = shift;
111              
112 1 50       4 if ( $self->{Signature} ) {
113 1         5 $self->output("\015\012");
114              
115             # append the DKIM-Signature (without data)
116 1         9 my $line = $self->{Signature}->as_string_without_data;
117              
118             # signature is subject to same canonicalization as headers
119 1         8 $self->output( $self->canonicalize_header($line) );
120             }
121             }
122              
123             sub body_count {
124 0     0 0   my $self = shift;
125 0           return $self->{body_count};
126             }
127              
128             sub body_truncated {
129 0     0 0   my $self = shift;
130 0           return $self->{body_truncated};
131             }
132              
133             1;
134              
135             __END__