Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4c7c40

Browse files
committed
RFC 2822 describes the characters allowed in a header field name. Conform to
this, and add test cases.
1 parent e48bad7 commit f4c7c40

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

Lib/email/FeedParser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
NLCRE_bol = re.compile('(\r\n|\r|\n)')
2828
NLCRE_eol = re.compile('(\r\n|\r|\n)$')
2929
NLCRE_crack = re.compile('(\r\n|\r|\n)')
30-
headerRE = re.compile(r'^(From |[-\w]{2,}:|[\t ])')
30+
# RFC 2822 $3.6.8 Optional fields. ftext is %d33-57 / %d59-126, Any character
31+
# except controls, SP, and ":".
32+
headerRE = re.compile(r'^(From |[\041-\071\073-\176]{2,}:|[\t ])')
3133
EMPTYSTRING = ''
3234
NL = '\n'
3335

Lib/email/test/test_email.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,22 @@ def test_strip_line_feed_and_carriage_return_in_headers(self):
24022402
eq(msg.get('Header'), value1)
24032403
eq(msg.get('Next-Header'), value2)
24042404

2405+
def test_rfc2822_header_syntax(self):
2406+
eq = self.assertEqual
2407+
m = '>From: foo\nFrom: bar\n!"#QUX;~: zoo\n\nbody'
2408+
msg = email.message_from_string(m)
2409+
eq(len(msg.keys()), 3)
2410+
keys = msg.keys()
2411+
keys.sort()
2412+
eq(keys, ['!"#QUX;~', '>From', 'From'])
2413+
eq(msg.get_payload(), 'body')
2414+
2415+
def test_rfc2822_space_not_allowed_in_header(self):
2416+
eq = self.assertEqual
2417+
m = '>From [email protected] 11:25:53\nFrom: bar\n!"#QUX;~: zoo\n\nbody'
2418+
msg = email.message_from_string(m)
2419+
eq(len(msg.keys()), 0)
2420+
24052421

24062422

24072423
class TestBase64(unittest.TestCase):

0 commit comments

Comments
 (0)