utils.rfc6266: Test for invalid ISO-8859-1 and duplicate params.

This commit is contained in:
Florian Bruhin 2014-08-14 12:00:29 +02:00
parent 03ea837211
commit 62b00e5ff6
2 changed files with 21 additions and 6 deletions

View File

@ -389,8 +389,6 @@ class AttachmentTests(AttachmentTestCase):
"""
self._check_filename('attachment; filename ="foo.html"', 'foo.html')
# This should be fixed in the rfc6266 library
@unittest.expectedFailure
def test_attwith2filenames(self):
"""'attachment', specifying two filename parameters.
@ -666,8 +664,6 @@ class CharacterSetTests(AttachmentTestCase):
self._check_filename("attachment; filename*=UTF-8''foo-a%cc%88.html",
'foo-ä.html')
# This should be fixed in the rfc6266 library
@unittest.expectedFailure
def test_attwithfn2231utf8_bad(self):
"""'attachment', specifying a filename of foo-ä-€.html.

View File

@ -10,6 +10,13 @@ import re
LangTagged = namedtuple('LangTagged', 'string langtag')
class DuplicateParamError(Exception):
pass
class InvalidISO8859Error(Exception):
pass
class ContentDisposition:
"""
Records various indications and hints about content disposition.
@ -128,7 +135,7 @@ def parse_headers(content_disposition, location=None, relaxed=False):
try:
parsed = peg.parse(content_disposition, ContentDispositionValue)
except SyntaxError:
except (SyntaxError, DuplicateParamError, InvalidISO8859Error):
return ContentDisposition(location=location)
return ContentDisposition(
disposition=parsed.dtype, assocs=parsed.params, location=location)
@ -141,6 +148,11 @@ def parse_ext_value(val):
charset, coded = val
langtag = None
decoded = urllib.parse.unquote(coded, charset, errors='strict')
if charset == 'iso-8859-1':
# Fail if the filename contains an invalid ISO-8859-1 char
for c in decoded:
if 0x7F <= ord(c) <= 0x9F:
raise InvalidISO8859Error(c)
return LangTagged(decoded, langtag)
# RFC 2616
@ -238,7 +250,14 @@ class ExtDispositionParm:
class DispositionType(peg.List):
grammar = [re.compile('(inline|attachment)', re.I), Token]
class DispositionParmList(peg.Namespace):
class UniqueNamespace(peg.Namespace):
def __setitem__(self, key, value):
if key in self:
raise DuplicateParamError(key)
super().__setitem__(key, value)
class DispositionParmList(UniqueNamespace):
grammar = peg.maybe_some(';', [ExtDispositionParm, DispositionParm])
class ContentDispositionValue: