diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py index 9b9697f77346a60..539f41935ed49c6 100644 --- a/Lib/email/_header_value_parser.py +++ b/Lib/email/_header_value_parser.py @@ -141,7 +141,7 @@ def append_if_fits(self, token, stoken=None): self.stickyspace = None self.firstline = False return True - if token.has_fws: + if token.has_leading_fws: ws = token.pop_leading_fws() if ws is not None: self.stickyspace += str(ws) @@ -267,7 +267,7 @@ def startswith_fws(self): return self[0].startswith_fws() def pop_leading_fws(self): - if self[0].token_type == 'fws': + if self[0].token_type == 'fws' or self[0].token_type == 'cfws': return self.pop(0) return self[0].pop_leading_fws() @@ -283,6 +283,14 @@ def has_fws(self): return True return False + @property + def has_leading_fws(self): + for part in self: + if part.has_fws: + return True + break + return False + def has_leading_comment(self): return self[0].has_leading_comment() @@ -1292,6 +1300,8 @@ def startswith_fws(self): has_fws = True + has_leading_fws = True + class ValueTerminal(Terminal): @@ -1304,6 +1314,8 @@ def startswith_fws(self): has_fws = False + has_leading_fws = False + def as_encoded_word(self, charset): return _ew.encode(str(self), charset) @@ -1323,6 +1335,8 @@ def __str__(self): has_fws = True + has_leading_fws = True + # XXX these need to become classes and used as instances so # that a program can't change them in a parse tree and screw diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py index e0ec87d2080185d..5d4a714f845531b 100644 --- a/Lib/test/test_email/test__header_value_parser.py +++ b/Lib/test/test_email/test__header_value_parser.py @@ -2723,5 +2723,14 @@ def test_long_filename_attachment(self): folded ) + def test_long_filename_attachment_with_space(self): + folded = self.policy.fold('Content-Disposition', 'attachment; filename="1234 67890123456789012345678901234567890123456789012345678901234567890"') + self.assertEqual( + 'Content-Disposition: attachment;\n filename="1234 67890123456789012345678901234567890123456789012345678901234567890"\n', + folded + ) + + + if __name__ == '__main__': unittest.main()