Split out dkim.canonicalization, and test it. Note that test_wsp_strips_headers is correctly failing, as trailing whitespace is not stripped from header names.

This commit is contained in:
William Grant
2011-06-03 21:16:31 +10:00
parent 1721b7fec2
commit 589853aee5
4 changed files with 155 additions and 38 deletions
+4 -38
View File
@@ -25,6 +25,10 @@ import logging
import re
import time
from dkim.canonicalization import (
Relaxed,
Simple,
)
from dkim.crypto import (
DigestTooLargeError,
parse_pem_private_key,
@@ -40,8 +44,6 @@ from dkim.util import (
)
__all__ = [
"Simple",
"Relaxed",
"InternalError",
"KeyFormatError",
"MessageFormatError",
@@ -50,42 +52,6 @@ __all__ = [
"verify",
]
class Simple:
"""Class that represents the "simple" canonicalization algorithm."""
name = b"simple"
@staticmethod
def canonicalize_headers(headers):
# No changes to headers.
return headers
@staticmethod
def canonicalize_body(body):
# Ignore all empty lines at the end of the message body.
return re.sub(b"(\r\n)*$", b"\r\n", body)
class Relaxed:
"""Class that represents the "relaxed" canonicalization algorithm."""
name = b"relaxed"
@staticmethod
def canonicalize_headers(headers):
# Convert all header field names to lowercase.
# Unfold all header lines.
# Compress WSP to single space.
# Remove all WSP at the start or end of the field value (strip).
return [(x[0].lower(), re.sub(br"\s+", b" ", re.sub(b"\r\n", b"", x[1])).strip()+b"\r\n") for x in headers]
@staticmethod
def canonicalize_body(body):
# Remove all trailing WSP at end of lines.
# Compress non-line-ending WSP to single space.
# Ignore all empty lines at the end of the message body.
return re.sub(b"(\r\n)*$", b"\r\n", re.sub(br"[\x09\x20]+", b" ", re.sub(b"[\\x09\\x20]+\r\n", b"\r\n", body)))
class DKIMException(Exception):
"""Base class for DKIM errors."""
pass