_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.
This commit is contained in:
parent
beb5945c65
commit
66971fbca5
|
@ -4,14 +4,12 @@
|
||||||
"""A parser of RFC 2822 and MIME email messages.
|
"""A parser of RFC 2822 and MIME email messages.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
|
||||||
# Intrapackage imports
|
# Intrapackage imports
|
||||||
import Errors
|
import Errors
|
||||||
import Message
|
import Message
|
||||||
|
|
||||||
bcre = re.compile('boundary="?([^"]+)"?', re.IGNORECASE)
|
|
||||||
EMPTYSTRING = ''
|
EMPTYSTRING = ''
|
||||||
NL = '\n'
|
NL = '\n'
|
||||||
|
|
||||||
|
@ -92,13 +90,8 @@ class Parser:
|
||||||
def _parsebody(self, container, fp):
|
def _parsebody(self, container, fp):
|
||||||
# Parse the body, but first split the payload on the content-type
|
# Parse the body, but first split the payload on the content-type
|
||||||
# boundary if present.
|
# boundary if present.
|
||||||
boundary = isdigest = None
|
boundary = container.get_boundary()
|
||||||
ctype = container['content-type']
|
isdigest = (container.get_type() == 'multipart/digest')
|
||||||
if ctype:
|
|
||||||
mo = bcre.search(ctype)
|
|
||||||
if mo:
|
|
||||||
boundary = mo.group(1)
|
|
||||||
isdigest = container.get_type() == 'multipart/digest'
|
|
||||||
# If there's a boundary, split the payload text into its constituent
|
# If there's a boundary, split the payload text into its constituent
|
||||||
# parts and parse each separately. Otherwise, just parse the rest of
|
# parts and parse each separately. Otherwise, just parse the rest of
|
||||||
# the body as a single message. Note: any exceptions raised in the
|
# the body as a single message. Note: any exceptions raised in the
|
||||||
|
@ -141,7 +134,20 @@ class Parser:
|
||||||
container.preamble = preamble
|
container.preamble = preamble
|
||||||
container.epilogue = epilogue
|
container.epilogue = epilogue
|
||||||
container.add_payload(msgobj)
|
container.add_payload(msgobj)
|
||||||
elif ctype == 'message/rfc822':
|
elif container.get_type() == 'message/delivery-status':
|
||||||
|
# This special kind of type contains blocks of headers separated
|
||||||
|
# by a blank line. We'll represent each header block as a
|
||||||
|
# separate Message object
|
||||||
|
blocks = []
|
||||||
|
while 1:
|
||||||
|
blockmsg = self._class()
|
||||||
|
self._parseheaders(blockmsg, fp)
|
||||||
|
if not len(blockmsg):
|
||||||
|
# No more header blocks left
|
||||||
|
break
|
||||||
|
blocks.append(blockmsg)
|
||||||
|
container.set_payload(blocks)
|
||||||
|
elif container.get_main_type() == 'message':
|
||||||
# Create a container for the payload, but watch out for there not
|
# Create a container for the payload, but watch out for there not
|
||||||
# being any headers left
|
# being any headers left
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue