]> granicus.if.org Git - python/commitdiff
Resolution of SF bug #1002475 and patch #1003693; Header lines that end in
authorBarry Warsaw <barry@python.org>
Sat, 7 Aug 2004 15:57:52 +0000 (15:57 +0000)
committerBarry Warsaw <barry@python.org>
Sat, 7 Aug 2004 15:57:52 +0000 (15:57 +0000)
\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.

Lib/email/FeedParser.py
Lib/email/test/test_email.py

index af0e177d561f2d5c3e936fa8797e0fa8c9336ed3..dc3027dbda4d21b1dccfc876191f547f64cfca55 100644 (file)
@@ -415,7 +415,8 @@ class FeedParser:
                 continue
             if lastheader:
                 # XXX reconsider the joining of folded lines
-                self._cur[lastheader] = EMPTYSTRING.join(lastvalue)[:-1]
+                lhdr = EMPTYSTRING.join(lastvalue)[:-1].rstrip('\r\n')
+                self._cur[lastheader] = lhdr
                 lastheader, lastvalue = '', []
             # Check for envelope header, i.e. unix-from
             if line.startswith('From '):
@@ -449,4 +450,4 @@ class FeedParser:
         # Done with all the lines, so handle the last header.
         if lastheader:
             # XXX reconsider the joining of folded lines
-            self._cur[lastheader] = EMPTYSTRING.join(lastvalue).rstrip()
+            self._cur[lastheader] = EMPTYSTRING.join(lastvalue).rstrip('\r\n')
index 0d7e79f98c2a711e864fc156b5c6d89d650e5a7a..1749102334c470c9fe5994777960fe5dfd7c6d73 100644 (file)
@@ -2307,7 +2307,7 @@ class TestParsers(TestEmailBase):
         self.failIf(msg.is_multipart())
         self.failUnless(isinstance(msg.get_payload(), str))
 
-    def test_whitespace_continuaton(self):
+    def test_whitespace_continuation(self):
         eq = self.assertEqual
         # This message contains a line after the Subject: header that has only
         # whitespace, but it is not empty!
@@ -2319,6 +2319,24 @@ Subject: the next line has a space on it
 Date: Mon, 8 Apr 2002 15:09:19 -0400
 Message-ID: spam
 
+Here's the message body
+""")
+        eq(msg['subject'], 'the next line has a space on it\n ')
+        eq(msg['message-id'], 'spam')
+        eq(msg.get_payload(), "Here's the message body\n")
+
+    def test_whitespace_continuation_last_header(self):
+        eq = self.assertEqual
+        # Like the previous test, but the subject line is the last
+        # header.
+        msg = email.message_from_string("""\
+From: aperson@dom.ain
+To: bperson@dom.ain
+Date: Mon, 8 Apr 2002 15:09:19 -0400
+Message-ID: spam
+Subject: the next line has a space on it
+\x20
+
 Here's the message body
 """)
         eq(msg['subject'], 'the next line has a space on it\n ')
@@ -2381,6 +2399,17 @@ Here's the message body
         msg = email.message_from_string(NL.join(lines))
         self.assertEqual(msg['date'], 'Tue, 20 Aug 2002 16:43:45 +1000')
 
+    def test_strip_line_feed_and_carriage_return_in_headers(self):
+        eq = self.assertEqual
+        # For [ 1002475 ] email message parser doesn't handle \r\n correctly
+        value1 = 'text'
+        value2 = 'more text'
+        m = 'Header: %s\r\nNext-Header: %s\r\n\r\nBody\r\n\r\n' % (
+            value1, value2)
+        msg = email.message_from_string(m)
+        eq(msg.get('Header'), value1)
+        eq(msg.get('Next-Header'), value2)
+
 
 \f
 class TestBase64(unittest.TestCase):