Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Prevent folding of non-ascii message-id headers.
Also, remove empty lines from classes that don't have any methods.
  • Loading branch information
maxking committed May 18, 2019
commit aca404a87094ea8b5452960b432aee91c9f46769
33 changes: 20 additions & 13 deletions Doc/library/email.headerregistry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,26 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1.

The default mappings are:

:subject: UniqueUnstructuredHeader
:date: UniqueDateHeader
:resent-date: DateHeader
:orig-date: UniqueDateHeader
:sender: UniqueSingleAddressHeader
:resent-sender: SingleAddressHeader
:to: UniqueAddressHeader
:resent-to: AddressHeader
:cc: UniqueAddressHeader
:resent-cc: AddressHeader
:from: UniqueAddressHeader
:resent-from: AddressHeader
:reply-to: UniqueAddressHeader
:subject: UniqueUnstructuredHeader
:date: UniqueDateHeader
:resent-date: DateHeader
:orig-date: UniqueDateHeader
:sender: UniqueSingleAddressHeader
:resent-sender: SingleAddressHeader
:to: UniqueAddressHeader
:resent-to: AddressHeader
:cc: UniqueAddressHeader
:resent-cc: AddressHeader
:bcc: UniqueAddressHeader
:resent-bcc: AddressHeader
:from: UniqueAddressHeader
:resent-from: AddressHeader
:reply-to: UniqueAddressHeader
:mime-version: MIMEVersionHeader
:content-type: ContentTypeHeader
:content-disposition: ContentDispositionHeader
:content-transfer-encoding: ContentTransferEncodingHeader
:message-id: MessageIDHeader

``HeaderRegistry`` has the following methods:

Expand Down
22 changes: 5 additions & 17 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,37 +179,30 @@ def comments(self):


class UnstructuredTokenList(TokenList):

token_type = 'unstructured'


class Phrase(TokenList):

token_type = 'phrase'

class Word(TokenList):

token_type = 'word'


class CFWSList(WhiteSpaceTokenList):

token_type = 'cfws'


class Atom(TokenList):

token_type = 'atom'


class Token(TokenList):

token_type = 'token'
encode_as_ew = False


class EncodedWord(TokenList):

token_type = 'encoded-word'
cte = None
charset = None
Expand Down Expand Up @@ -496,18 +489,15 @@ def domain(self):


class DotAtom(TokenList):

token_type = 'dot-atom'


class DotAtomText(TokenList):

token_type = 'dot-atom-text'
as_ew_allowed = True


class NoFoldLiteral(TokenList):

token_type = 'no-fold-literal'
as_ew_allowed = False

Expand Down Expand Up @@ -815,46 +805,40 @@ def params(self):


class ContentType(ParameterizedHeaderValue):

token_type = 'content-type'
as_ew_allowed = False
maintype = 'text'
subtype = 'plain'


class ContentDisposition(ParameterizedHeaderValue):

token_type = 'content-disposition'
as_ew_allowed = False
content_disposition = None


class ContentTransferEncoding(TokenList):

token_type = 'content-transfer-encoding'
as_ew_allowed = False
cte = '7bit'


class HeaderLabel(TokenList):

token_type = 'header-label'
as_ew_allowed = False


class MsgID(TokenList):

token_type = 'msg-id'
as_ew_allowed = False
fold_subparts = False


class MessageID(MsgID):

token_type = 'message-id'


class Header(TokenList):

token_type = 'header'


Expand Down Expand Up @@ -2763,6 +2747,10 @@ def _refold_parse_tree(parse_tree, *, policy):
# to unpacking the subparts and wrapping them.
if not hasattr(part, 'encode'):
# It's not a Terminal, do each piece individually.
if not getattr(part, 'fold_subparts', True):
# This part can't be folded and also doesn't allow folding
# of subparts.
wrap_as_ew_blocked += 1
parts = list(part) + parts
else:
# It's a terminal, wrap it as an encoded word, possibly
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_email/test_headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,5 +1656,26 @@ def test_message_id_header_is_not_folded(self):
h.fold(policy=policy.default.clone(max_line_length=20)),
'Message-ID: <[email protected]>\n')

# Test message-id isn't folded when id-right is no-fold-literal.
h = self.make_header(
'Message-ID',
'<somemessageidlongerthan@[127.0.0.0.0.0.0.0.0.1]>')
self.assertEqual(
h.fold(policy=policy.default.clone(max_line_length=20)),
'Message-ID: <somemessageidlongerthan@[127.0.0.0.0.0.0.0.0.1]>\n')

# Test message-id isn't folded when id-right is non-ascii characters.
h = self.make_header('Message-ID', '<ईमेल@wők.com>')
self.assertEqual(
h.fold(policy=policy.default.clone(max_line_length=30)),
'Message-ID: <ईमेल@wők.com>\n')

# Test message-id is folded without breaking the msg-id token into
# encoded words, *even* if they don't fit into max_line_length.
h = self.make_header('Message-ID', '<ईमेलfromMessage@wők.com>')
self.assertEqual(
h.fold(policy=policy.default.clone(max_line_length=20)),
'Message-ID:\n <ईमेलfromMessage@wők.com>\n')

if __name__ == '__main__':
unittest.main()