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

Skip to content

Commit 8896bf5

Browse files
committed
Resolution of SF bug #1002475 and patch #1003693; Header lines that end in
\r\n only get the \n stripped, not the \r (unless it's the last header which does get the \r stripped). Patch by Tony Meyer. test_whitespace_continuation_last_header(), test_strip_line_feed_and_carriage_return_in_headers(): New tests. _parse_headers(): Be sure to strip \r\n from the right side of header lines.
1 parent 14d535c commit 8896bf5

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

Lib/email/FeedParser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ def _parse_headers(self, lines):
415415
continue
416416
if lastheader:
417417
# XXX reconsider the joining of folded lines
418-
self._cur[lastheader] = EMPTYSTRING.join(lastvalue)[:-1]
418+
lhdr = EMPTYSTRING.join(lastvalue)[:-1].rstrip('\r\n')
419+
self._cur[lastheader] = lhdr
419420
lastheader, lastvalue = '', []
420421
# Check for envelope header, i.e. unix-from
421422
if line.startswith('From '):
@@ -449,4 +450,4 @@ def _parse_headers(self, lines):
449450
# Done with all the lines, so handle the last header.
450451
if lastheader:
451452
# XXX reconsider the joining of folded lines
452-
self._cur[lastheader] = EMPTYSTRING.join(lastvalue).rstrip()
453+
self._cur[lastheader] = EMPTYSTRING.join(lastvalue).rstrip('\r\n')

Lib/email/test/test_email.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,7 @@ def test_header_parser(self):
23072307
self.failIf(msg.is_multipart())
23082308
self.failUnless(isinstance(msg.get_payload(), str))
23092309

2310-
def test_whitespace_continuaton(self):
2310+
def test_whitespace_continuation(self):
23112311
eq = self.assertEqual
23122312
# This message contains a line after the Subject: header that has only
23132313
# whitespace, but it is not empty!
@@ -2319,6 +2319,24 @@ def test_whitespace_continuaton(self):
23192319
Date: Mon, 8 Apr 2002 15:09:19 -0400
23202320
Message-ID: spam
23212321
2322+
Here's the message body
2323+
""")
2324+
eq(msg['subject'], 'the next line has a space on it\n ')
2325+
eq(msg['message-id'], 'spam')
2326+
eq(msg.get_payload(), "Here's the message body\n")
2327+
2328+
def test_whitespace_continuation_last_header(self):
2329+
eq = self.assertEqual
2330+
# Like the previous test, but the subject line is the last
2331+
# header.
2332+
msg = email.message_from_string("""\
2333+
2334+
2335+
Date: Mon, 8 Apr 2002 15:09:19 -0400
2336+
Message-ID: spam
2337+
Subject: the next line has a space on it
2338+
\x20
2339+
23222340
Here's the message body
23232341
""")
23242342
eq(msg['subject'], 'the next line has a space on it\n ')
@@ -2381,6 +2399,17 @@ def test_three_lines(self):
23812399
msg = email.message_from_string(NL.join(lines))
23822400
self.assertEqual(msg['date'], 'Tue, 20 Aug 2002 16:43:45 +1000')
23832401

2402+
def test_strip_line_feed_and_carriage_return_in_headers(self):
2403+
eq = self.assertEqual
2404+
# For [ 1002475 ] email message parser doesn't handle \r\n correctly
2405+
value1 = 'text'
2406+
value2 = 'more text'
2407+
m = 'Header: %s\r\nNext-Header: %s\r\n\r\nBody\r\n\r\n' % (
2408+
value1, value2)
2409+
msg = email.message_from_string(m)
2410+
eq(msg.get('Header'), value1)
2411+
eq(msg.get('Next-Header'), value2)
2412+
23842413

23852414

23862415
class TestBase64(unittest.TestCase):

0 commit comments

Comments
 (0)