summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorLisa Junger <ljunger@thoughtworks.com>2015-03-31 17:10:11 +0200
committerLisa Junger <ljunger@thoughtworks.com>2015-04-02 17:54:21 +0200
commitdb5a88affdf696b833619d18cbcfdd408f206150 (patch)
treeed6649a17304ddabe26361a607cbfac7f16e15b4 /service
parentbd4fc909be410548283cb5d6f0d8a7eeaccc4405 (diff)
handle incorrectly encoded headers as ascii.
- Issue #347
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/model/mail.py10
-rw-r--r--service/test/unit/adapter/test_mail.py12
2 files changed, 20 insertions, 2 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py
index f23c2708..e11c4048 100644
--- a/service/pixelated/adapter/model/mail.py
+++ b/service/pixelated/adapter/model/mail.py
@@ -303,13 +303,19 @@ class PixelatedMail(Mail):
return _headers
+ def _decode_header_with_fallback(self, entry):
+ try:
+ return decode_header(entry)[0][0]
+ except Exception, e:
+ return entry.encode('ascii', 'ignore')
+
def _decode_header(self, header):
if not header:
return None
if isinstance(header, list):
- return [decode_header(entry)[0][0] for entry in header]
+ return [self._decode_header_with_fallback(entry) for entry in header]
else:
- return decode_header(header)[0][0]
+ return self._decode_header_with_fallback(header)
def _get_date(self):
date = self.hdoc.content.get('date', None)
diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py
index c7910b7f..68b340a7 100644
--- a/service/test/unit/adapter/test_mail.py
+++ b/service/test/unit/adapter/test_mail.py
@@ -1,3 +1,4 @@
+# -*- coding: UTF-8 -*-
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
@@ -334,6 +335,17 @@ class TestPixelatedMail(unittest.TestCase):
mail.as_dict()
+ def test_parse_UTF8_headers_with_CharsetAscii(self):
+ leap_mail_from = u'"söme ümläuds" <lisa5@dev.pixelated-project.org>'
+ leap_mail_to = u'"söme ümläuds" <lisa5@dev.pixelated-project.org>,\n"söme ümläuds" <lisa5@dev.pixelated-project.org>'
+
+ leap_mail = test_helper.leap_mail(extra_headers={'From': leap_mail_from, 'Subject': "some subject", 'To': leap_mail_to})
+
+ mail = PixelatedMail.from_soledad(*leap_mail, soledad_querier=self.querier)
+
+ mail.headers['From'].encode('ascii')
+ self.assertEqual(mail.headers['To'], ['"sme mluds" <lisa5@dev.pixelated-project.org>', '"sme mluds" <lisa5@dev.pixelated-project.org>'])
+
def simple_mail_dict():
return {