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

Skip to content

Commit 66971fb

Browse files
committed
_parsebody(): Use get_boundary() and get_type().
Also, add a clause to the big-if to handle message/delivery-status content types. These create a message with subparts that are Message instances, which best represent the header blocks of this content type.
1 parent beb5945 commit 66971fb

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

Lib/email/Parser.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
"""A parser of RFC 2822 and MIME email messages.
55
"""
66

7-
import re
87
from cStringIO import StringIO
98

109
# Intrapackage imports
1110
import Errors
1211
import Message
1312

14-
bcre = re.compile('boundary="?([^"]+)"?', re.IGNORECASE)
1513
EMPTYSTRING = ''
1614
NL = '\n'
1715

@@ -92,13 +90,8 @@ def _parseheaders(self, container, fp):
9290
def _parsebody(self, container, fp):
9391
# Parse the body, but first split the payload on the content-type
9492
# boundary if present.
95-
boundary = isdigest = None
96-
ctype = container['content-type']
97-
if ctype:
98-
mo = bcre.search(ctype)
99-
if mo:
100-
boundary = mo.group(1)
101-
isdigest = container.get_type() == 'multipart/digest'
93+
boundary = container.get_boundary()
94+
isdigest = (container.get_type() == 'multipart/digest')
10295
# If there's a boundary, split the payload text into its constituent
10396
# parts and parse each separately. Otherwise, just parse the rest of
10497
# the body as a single message. Note: any exceptions raised in the
@@ -141,7 +134,20 @@ def _parsebody(self, container, fp):
141134
container.preamble = preamble
142135
container.epilogue = epilogue
143136
container.add_payload(msgobj)
144-
elif ctype == 'message/rfc822':
137+
elif container.get_type() == 'message/delivery-status':
138+
# This special kind of type contains blocks of headers separated
139+
# by a blank line. We'll represent each header block as a
140+
# separate Message object
141+
blocks = []
142+
while 1:
143+
blockmsg = self._class()
144+
self._parseheaders(blockmsg, fp)
145+
if not len(blockmsg):
146+
# No more header blocks left
147+
break
148+
blocks.append(blockmsg)
149+
container.set_payload(blocks)
150+
elif container.get_main_type() == 'message':
145151
# Create a container for the payload, but watch out for there not
146152
# being any headers left
147153
try:

0 commit comments

Comments
 (0)