summaryrefslogtreecommitdiff
path: root/src/leap/mail/imap/messages.py
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2015-03-23 12:59:12 -0400
committerKali Kaneko <kali@leap.se>2015-03-30 14:48:07 -0400
commitf97104e25fe504993615f194825a757d4c381a24 (patch)
treeff4a91777a7d385e5b986a2c1412e11939b76b4b /src/leap/mail/imap/messages.py
parentefb6028e8d4096d113c565a3953919a5e30d0947 (diff)
[bug] re-add fix for multiple headers
This fix stores as multi-line headers that are repeated, and that were being discarded when storing them in a regular dict. It had been removed during the last refactor. I also store headers now as a case-insensitive dict, which solves other problems with the implementation of the twisted imap. Releases: 0.4.0
Diffstat (limited to 'src/leap/mail/imap/messages.py')
-rw-r--r--src/leap/mail/imap/messages.py25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/leap/mail/imap/messages.py b/src/leap/mail/imap/messages.py
index 02aac2e..13943b1 100644
--- a/src/leap/mail/imap/messages.py
+++ b/src/leap/mail/imap/messages.py
@@ -208,6 +208,18 @@ class IMAPMessagePart(object):
return IMAPMessagePart(subpart)
+class CaseInsensitiveDict(dict):
+ """
+ A dictionary subclass that will allow case-insenstive key lookups.
+ """
+
+ def __setitem__(self, key, value):
+ super(CaseInsensitiveDict, self).__setitem__(key.lower(), value)
+
+ def __getitem__(self, key):
+ return super(CaseInsensitiveDict, self).__getitem__(key.lower())
+
+
def _format_headers(headers, negate, *names):
# current server impl. expects content-type to be present, so if for
# some reason we do not have headers, we have to return at least that
@@ -228,13 +240,13 @@ def _format_headers(headers, negate, *names):
# default to most likely standard
charset = find_charset(headers, "utf-8")
- _headers = dict()
- for key, value in headers.items():
- # twisted imap server expects *some* headers to be lowercase
- # We could use a CaseInsensitiveDict here...
- if key.lower() == "content-type":
- key = key.lower()
+ # We will return a copy of the headers dictionary that
+ # will allow case-insensitive lookups. In some parts of the twisted imap
+ # server code the keys are expected to be in lower case, and in this way
+ # we avoid having to convert them.
+ _headers = CaseInsensitiveDict()
+ for key, value in headers.items():
if not isinstance(key, str):
key = key.encode(charset, 'replace')
if not isinstance(value, str):
@@ -247,4 +259,5 @@ def _format_headers(headers, negate, *names):
# filter original dict by negate-condition
if cond(key):
_headers[key] = value
+
return _headers