Pull c= value parsing out into Canonicalizationpolicy.from_c_value.
This commit is contained in:
@@ -90,6 +90,35 @@ class CanonicalizationPolicy:
|
||||
self.header_algorithm = header_algorithm
|
||||
self.body_algorithm = body_algorithm
|
||||
|
||||
@classmethod
|
||||
def from_c_value(cls, c, logger=None):
|
||||
"""Construct the canonicalization policy described by a c= value.
|
||||
|
||||
@param c: c= value from a DKIM-Signature header field
|
||||
@return: a C{CanonicalizationPolicy}, or C{None} if the value is
|
||||
invalid
|
||||
"""
|
||||
if c is None:
|
||||
c = b'simple/simple'
|
||||
m = c.split(b'/')
|
||||
if len(m) not in (1, 2):
|
||||
if logger:
|
||||
logger.error(
|
||||
"c= value is not in format method/method: %s" % c)
|
||||
return None
|
||||
if len(m) == 1:
|
||||
m.append(b'simple')
|
||||
can_headers, can_body = m
|
||||
try:
|
||||
header_algorithm = algorithms[can_headers]
|
||||
body_algorithm = algorithms[can_body]
|
||||
except KeyError as e:
|
||||
if logger:
|
||||
logger.error(
|
||||
"unknown canonicalization algorithm: %s" % e.message)
|
||||
return None
|
||||
return cls(header_algorithm, body_algorithm)
|
||||
|
||||
def canonicalize_headers(self, headers):
|
||||
return self.header_algorithm.canonicalize_headers(headers)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user