1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import re
23
24 __all__ = [
25 'CanonicalizationPolicy',
26 'InvalidCanonicalizationPolicyError',
27 ]
31 """The c= value could not be parsed."""
32 pass
33
36 return re.sub(b"[\t ]+\r\n", b"\r\n", content)
37
40 return re.sub(b"[\t ]+", b" ", content)
41
44 return re.sub(b"(\r\n)*$", b"\r\n", content)
45
48 return re.sub(b"\r\n", b"", content)
49
50
51 -def correct_empty_body(content):
52 if content == b"\r\n":
53 return b""
54 else:
55 return content
56
59 """Class that represents the "simple" canonicalization algorithm."""
60
61 name = b"simple"
62
63 @staticmethod
67
68 @staticmethod
72
75 """Class that represents the "relaxed" canonicalization algorithm."""
76
77 name = b"relaxed"
78
79 @staticmethod
89
90 @staticmethod
97
100
101 - def __init__(self, header_algorithm, body_algorithm):
102 self.header_algorithm = header_algorithm
103 self.body_algorithm = body_algorithm
104
105 @classmethod
107 """Construct the canonicalization policy described by a c= value.
108
109 May raise an C{InvalidCanonicalizationPolicyError} if the given
110 value is invalid
111
112 @param c: c= value from a DKIM-Signature header field
113 @return: a C{CanonicalizationPolicy}
114 """
115 if c is None:
116 c = b'simple/simple'
117 m = c.split(b'/')
118 if len(m) not in (1, 2):
119 raise InvalidCanonicalizationPolicyError(c)
120 if len(m) == 1:
121 m.append(b'simple')
122 can_headers, can_body = m
123 try:
124 header_algorithm = ALGORITHMS[can_headers]
125 body_algorithm = ALGORITHMS[can_body]
126 except KeyError as e:
127 raise InvalidCanonicalizationPolicyError(e.args[0])
128 return cls(header_algorithm, body_algorithm)
129
131 return b'/'.join(
132 (self.header_algorithm.name, self.body_algorithm.name))
133
136
137 - def canonicalize_body(self, body):
138 return self.body_algorithm.canonicalize_body(body)
139
140
141 ALGORITHMS = dict((c.name, c) for c in (Simple, Relaxed))
142