diff -r 1aa6751b298f Lib/email/feedparser.py --- a/Lib/email/feedparser.py Mon Dec 16 09:05:44 2013 -0600 +++ b/Lib/email/feedparser.py Mon Dec 16 20:12:15 2013 +0000 @@ -33,7 +33,7 @@ NLCRE_crack = re.compile('(\r\n|\r|\n)') # RFC 2822 $3.6.8 Optional fields. ftext is %d33-57 / %d59-126, Any character # except controls, SP, and ":". -headerRE = re.compile(r'^(From |[\041-\071\073-\176]{1,}:|[\t ])') +headerRE = re.compile(r'^(From |[\041-\071\073-\176]*:|[\t ])') EMPTYSTRING = '' NL = '\n' @@ -492,6 +492,15 @@ # There will always be a colon, because if there wasn't the part of # the parser that calls us would have started parsing the body. i = line.find(':') + + # If the colon is on the start of the line the header is clearly + # malformed, but we might be able to salvage the rest of the + # message. Track the error but keep going. + if i == 0: + defect = errors.InvalidHeaderDefect("Missing header name.") + self._cur.defects.append(defect) + continue + assert i>0, "_parse_headers fed line with no : and no leading WS" lastheader = line[:i] lastvalue = [line] diff -r 1aa6751b298f Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py Mon Dec 16 09:05:44 2013 -0600 +++ b/Lib/test/test_email/test_email.py Mon Dec 16 20:12:15 2013 +0000 @@ -729,6 +729,13 @@ w4kgdGVzdGFiYwo= """)) + def test_empty_header_name_handled(self): + # Issue 19996 + msg = email.message_from_string("First: val\n: bad\nSecond: val") + self.assertEqual(msg['First'], 'val') + self.assertEqual(msg['Second'], 'val') + + # Test the email.encoders module class TestEncoders(unittest.TestCase): diff -r 1aa6751b298f Lib/test/test_httplib.py --- a/Lib/test/test_httplib.py Mon Dec 16 09:05:44 2013 -0600 +++ b/Lib/test/test_httplib.py Mon Dec 16 20:12:15 2013 +0000 @@ -155,6 +155,18 @@ conn.request('GET', '/foo') self.assertTrue(sock.data.startswith(expected)) + def test_malformed_headers_coped_with(self): + # Issue 19996 + body = "HTTP/1.1 200 OK\r\nFirst: val\r\n: nval\r\nSecond: val\r\n\r\n" + sock = FakeSocket(body) + resp = client.HTTPResponse(sock) + resp.begin() + + self.assertEqual(resp.getheader('First'), 'val') + self.assertEqual(resp.getheader('Second'), 'val') + + + class BasicTest(TestCase): def test_status_lines(self):