annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/email/feedparser.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 # Copyright (C) 2004-2006 Python Software Foundation
jpayne@68 2 # Authors: Baxter, Wouters and Warsaw
jpayne@68 3 # Contact: email-sig@python.org
jpayne@68 4
jpayne@68 5 """FeedParser - An email feed parser.
jpayne@68 6
jpayne@68 7 The feed parser implements an interface for incrementally parsing an email
jpayne@68 8 message, line by line. This has advantages for certain applications, such as
jpayne@68 9 those reading email messages off a socket.
jpayne@68 10
jpayne@68 11 FeedParser.feed() is the primary interface for pushing new data into the
jpayne@68 12 parser. It returns when there's nothing more it can do with the available
jpayne@68 13 data. When you have no more data to push into the parser, call .close().
jpayne@68 14 This completes the parsing and returns the root message object.
jpayne@68 15
jpayne@68 16 The other advantage of this parser is that it will never raise a parsing
jpayne@68 17 exception. Instead, when it finds something unexpected, it adds a 'defect' to
jpayne@68 18 the current message. Defects are just instances that live on the message
jpayne@68 19 object's .defects attribute.
jpayne@68 20 """
jpayne@68 21
jpayne@68 22 __all__ = ['FeedParser', 'BytesFeedParser']
jpayne@68 23
jpayne@68 24 import re
jpayne@68 25
jpayne@68 26 from email import errors
jpayne@68 27 from email._policybase import compat32
jpayne@68 28 from collections import deque
jpayne@68 29 from io import StringIO
jpayne@68 30
jpayne@68 31 NLCRE = re.compile(r'\r\n|\r|\n')
jpayne@68 32 NLCRE_bol = re.compile(r'(\r\n|\r|\n)')
jpayne@68 33 NLCRE_eol = re.compile(r'(\r\n|\r|\n)\Z')
jpayne@68 34 NLCRE_crack = re.compile(r'(\r\n|\r|\n)')
jpayne@68 35 # RFC 2822 $3.6.8 Optional fields. ftext is %d33-57 / %d59-126, Any character
jpayne@68 36 # except controls, SP, and ":".
jpayne@68 37 headerRE = re.compile(r'^(From |[\041-\071\073-\176]*:|[\t ])')
jpayne@68 38 EMPTYSTRING = ''
jpayne@68 39 NL = '\n'
jpayne@68 40
jpayne@68 41 NeedMoreData = object()
jpayne@68 42
jpayne@68 43
jpayne@68 44
jpayne@68 45 class BufferedSubFile(object):
jpayne@68 46 """A file-ish object that can have new data loaded into it.
jpayne@68 47
jpayne@68 48 You can also push and pop line-matching predicates onto a stack. When the
jpayne@68 49 current predicate matches the current line, a false EOF response
jpayne@68 50 (i.e. empty string) is returned instead. This lets the parser adhere to a
jpayne@68 51 simple abstraction -- it parses until EOF closes the current message.
jpayne@68 52 """
jpayne@68 53 def __init__(self):
jpayne@68 54 # Text stream of the last partial line pushed into this object.
jpayne@68 55 # See issue 22233 for why this is a text stream and not a list.
jpayne@68 56 self._partial = StringIO(newline='')
jpayne@68 57 # A deque of full, pushed lines
jpayne@68 58 self._lines = deque()
jpayne@68 59 # The stack of false-EOF checking predicates.
jpayne@68 60 self._eofstack = []
jpayne@68 61 # A flag indicating whether the file has been closed or not.
jpayne@68 62 self._closed = False
jpayne@68 63
jpayne@68 64 def push_eof_matcher(self, pred):
jpayne@68 65 self._eofstack.append(pred)
jpayne@68 66
jpayne@68 67 def pop_eof_matcher(self):
jpayne@68 68 return self._eofstack.pop()
jpayne@68 69
jpayne@68 70 def close(self):
jpayne@68 71 # Don't forget any trailing partial line.
jpayne@68 72 self._partial.seek(0)
jpayne@68 73 self.pushlines(self._partial.readlines())
jpayne@68 74 self._partial.seek(0)
jpayne@68 75 self._partial.truncate()
jpayne@68 76 self._closed = True
jpayne@68 77
jpayne@68 78 def readline(self):
jpayne@68 79 if not self._lines:
jpayne@68 80 if self._closed:
jpayne@68 81 return ''
jpayne@68 82 return NeedMoreData
jpayne@68 83 # Pop the line off the stack and see if it matches the current
jpayne@68 84 # false-EOF predicate.
jpayne@68 85 line = self._lines.popleft()
jpayne@68 86 # RFC 2046, section 5.1.2 requires us to recognize outer level
jpayne@68 87 # boundaries at any level of inner nesting. Do this, but be sure it's
jpayne@68 88 # in the order of most to least nested.
jpayne@68 89 for ateof in reversed(self._eofstack):
jpayne@68 90 if ateof(line):
jpayne@68 91 # We're at the false EOF. But push the last line back first.
jpayne@68 92 self._lines.appendleft(line)
jpayne@68 93 return ''
jpayne@68 94 return line
jpayne@68 95
jpayne@68 96 def unreadline(self, line):
jpayne@68 97 # Let the consumer push a line back into the buffer.
jpayne@68 98 assert line is not NeedMoreData
jpayne@68 99 self._lines.appendleft(line)
jpayne@68 100
jpayne@68 101 def push(self, data):
jpayne@68 102 """Push some new data into this object."""
jpayne@68 103 self._partial.write(data)
jpayne@68 104 if '\n' not in data and '\r' not in data:
jpayne@68 105 # No new complete lines, wait for more.
jpayne@68 106 return
jpayne@68 107
jpayne@68 108 # Crack into lines, preserving the linesep characters.
jpayne@68 109 self._partial.seek(0)
jpayne@68 110 parts = self._partial.readlines()
jpayne@68 111 self._partial.seek(0)
jpayne@68 112 self._partial.truncate()
jpayne@68 113
jpayne@68 114 # If the last element of the list does not end in a newline, then treat
jpayne@68 115 # it as a partial line. We only check for '\n' here because a line
jpayne@68 116 # ending with '\r' might be a line that was split in the middle of a
jpayne@68 117 # '\r\n' sequence (see bugs 1555570 and 1721862).
jpayne@68 118 if not parts[-1].endswith('\n'):
jpayne@68 119 self._partial.write(parts.pop())
jpayne@68 120 self.pushlines(parts)
jpayne@68 121
jpayne@68 122 def pushlines(self, lines):
jpayne@68 123 self._lines.extend(lines)
jpayne@68 124
jpayne@68 125 def __iter__(self):
jpayne@68 126 return self
jpayne@68 127
jpayne@68 128 def __next__(self):
jpayne@68 129 line = self.readline()
jpayne@68 130 if line == '':
jpayne@68 131 raise StopIteration
jpayne@68 132 return line
jpayne@68 133
jpayne@68 134
jpayne@68 135
jpayne@68 136 class FeedParser:
jpayne@68 137 """A feed-style parser of email."""
jpayne@68 138
jpayne@68 139 def __init__(self, _factory=None, *, policy=compat32):
jpayne@68 140 """_factory is called with no arguments to create a new message obj
jpayne@68 141
jpayne@68 142 The policy keyword specifies a policy object that controls a number of
jpayne@68 143 aspects of the parser's operation. The default policy maintains
jpayne@68 144 backward compatibility.
jpayne@68 145
jpayne@68 146 """
jpayne@68 147 self.policy = policy
jpayne@68 148 self._old_style_factory = False
jpayne@68 149 if _factory is None:
jpayne@68 150 if policy.message_factory is None:
jpayne@68 151 from email.message import Message
jpayne@68 152 self._factory = Message
jpayne@68 153 else:
jpayne@68 154 self._factory = policy.message_factory
jpayne@68 155 else:
jpayne@68 156 self._factory = _factory
jpayne@68 157 try:
jpayne@68 158 _factory(policy=self.policy)
jpayne@68 159 except TypeError:
jpayne@68 160 # Assume this is an old-style factory
jpayne@68 161 self._old_style_factory = True
jpayne@68 162 self._input = BufferedSubFile()
jpayne@68 163 self._msgstack = []
jpayne@68 164 self._parse = self._parsegen().__next__
jpayne@68 165 self._cur = None
jpayne@68 166 self._last = None
jpayne@68 167 self._headersonly = False
jpayne@68 168
jpayne@68 169 # Non-public interface for supporting Parser's headersonly flag
jpayne@68 170 def _set_headersonly(self):
jpayne@68 171 self._headersonly = True
jpayne@68 172
jpayne@68 173 def feed(self, data):
jpayne@68 174 """Push more data into the parser."""
jpayne@68 175 self._input.push(data)
jpayne@68 176 self._call_parse()
jpayne@68 177
jpayne@68 178 def _call_parse(self):
jpayne@68 179 try:
jpayne@68 180 self._parse()
jpayne@68 181 except StopIteration:
jpayne@68 182 pass
jpayne@68 183
jpayne@68 184 def close(self):
jpayne@68 185 """Parse all remaining data and return the root message object."""
jpayne@68 186 self._input.close()
jpayne@68 187 self._call_parse()
jpayne@68 188 root = self._pop_message()
jpayne@68 189 assert not self._msgstack
jpayne@68 190 # Look for final set of defects
jpayne@68 191 if root.get_content_maintype() == 'multipart' \
jpayne@68 192 and not root.is_multipart():
jpayne@68 193 defect = errors.MultipartInvariantViolationDefect()
jpayne@68 194 self.policy.handle_defect(root, defect)
jpayne@68 195 return root
jpayne@68 196
jpayne@68 197 def _new_message(self):
jpayne@68 198 if self._old_style_factory:
jpayne@68 199 msg = self._factory()
jpayne@68 200 else:
jpayne@68 201 msg = self._factory(policy=self.policy)
jpayne@68 202 if self._cur and self._cur.get_content_type() == 'multipart/digest':
jpayne@68 203 msg.set_default_type('message/rfc822')
jpayne@68 204 if self._msgstack:
jpayne@68 205 self._msgstack[-1].attach(msg)
jpayne@68 206 self._msgstack.append(msg)
jpayne@68 207 self._cur = msg
jpayne@68 208 self._last = msg
jpayne@68 209
jpayne@68 210 def _pop_message(self):
jpayne@68 211 retval = self._msgstack.pop()
jpayne@68 212 if self._msgstack:
jpayne@68 213 self._cur = self._msgstack[-1]
jpayne@68 214 else:
jpayne@68 215 self._cur = None
jpayne@68 216 return retval
jpayne@68 217
jpayne@68 218 def _parsegen(self):
jpayne@68 219 # Create a new message and start by parsing headers.
jpayne@68 220 self._new_message()
jpayne@68 221 headers = []
jpayne@68 222 # Collect the headers, searching for a line that doesn't match the RFC
jpayne@68 223 # 2822 header or continuation pattern (including an empty line).
jpayne@68 224 for line in self._input:
jpayne@68 225 if line is NeedMoreData:
jpayne@68 226 yield NeedMoreData
jpayne@68 227 continue
jpayne@68 228 if not headerRE.match(line):
jpayne@68 229 # If we saw the RFC defined header/body separator
jpayne@68 230 # (i.e. newline), just throw it away. Otherwise the line is
jpayne@68 231 # part of the body so push it back.
jpayne@68 232 if not NLCRE.match(line):
jpayne@68 233 defect = errors.MissingHeaderBodySeparatorDefect()
jpayne@68 234 self.policy.handle_defect(self._cur, defect)
jpayne@68 235 self._input.unreadline(line)
jpayne@68 236 break
jpayne@68 237 headers.append(line)
jpayne@68 238 # Done with the headers, so parse them and figure out what we're
jpayne@68 239 # supposed to see in the body of the message.
jpayne@68 240 self._parse_headers(headers)
jpayne@68 241 # Headers-only parsing is a backwards compatibility hack, which was
jpayne@68 242 # necessary in the older parser, which could raise errors. All
jpayne@68 243 # remaining lines in the input are thrown into the message body.
jpayne@68 244 if self._headersonly:
jpayne@68 245 lines = []
jpayne@68 246 while True:
jpayne@68 247 line = self._input.readline()
jpayne@68 248 if line is NeedMoreData:
jpayne@68 249 yield NeedMoreData
jpayne@68 250 continue
jpayne@68 251 if line == '':
jpayne@68 252 break
jpayne@68 253 lines.append(line)
jpayne@68 254 self._cur.set_payload(EMPTYSTRING.join(lines))
jpayne@68 255 return
jpayne@68 256 if self._cur.get_content_type() == 'message/delivery-status':
jpayne@68 257 # message/delivery-status contains blocks of headers separated by
jpayne@68 258 # a blank line. We'll represent each header block as a separate
jpayne@68 259 # nested message object, but the processing is a bit different
jpayne@68 260 # than standard message/* types because there is no body for the
jpayne@68 261 # nested messages. A blank line separates the subparts.
jpayne@68 262 while True:
jpayne@68 263 self._input.push_eof_matcher(NLCRE.match)
jpayne@68 264 for retval in self._parsegen():
jpayne@68 265 if retval is NeedMoreData:
jpayne@68 266 yield NeedMoreData
jpayne@68 267 continue
jpayne@68 268 break
jpayne@68 269 msg = self._pop_message()
jpayne@68 270 # We need to pop the EOF matcher in order to tell if we're at
jpayne@68 271 # the end of the current file, not the end of the last block
jpayne@68 272 # of message headers.
jpayne@68 273 self._input.pop_eof_matcher()
jpayne@68 274 # The input stream must be sitting at the newline or at the
jpayne@68 275 # EOF. We want to see if we're at the end of this subpart, so
jpayne@68 276 # first consume the blank line, then test the next line to see
jpayne@68 277 # if we're at this subpart's EOF.
jpayne@68 278 while True:
jpayne@68 279 line = self._input.readline()
jpayne@68 280 if line is NeedMoreData:
jpayne@68 281 yield NeedMoreData
jpayne@68 282 continue
jpayne@68 283 break
jpayne@68 284 while True:
jpayne@68 285 line = self._input.readline()
jpayne@68 286 if line is NeedMoreData:
jpayne@68 287 yield NeedMoreData
jpayne@68 288 continue
jpayne@68 289 break
jpayne@68 290 if line == '':
jpayne@68 291 break
jpayne@68 292 # Not at EOF so this is a line we're going to need.
jpayne@68 293 self._input.unreadline(line)
jpayne@68 294 return
jpayne@68 295 if self._cur.get_content_maintype() == 'message':
jpayne@68 296 # The message claims to be a message/* type, then what follows is
jpayne@68 297 # another RFC 2822 message.
jpayne@68 298 for retval in self._parsegen():
jpayne@68 299 if retval is NeedMoreData:
jpayne@68 300 yield NeedMoreData
jpayne@68 301 continue
jpayne@68 302 break
jpayne@68 303 self._pop_message()
jpayne@68 304 return
jpayne@68 305 if self._cur.get_content_maintype() == 'multipart':
jpayne@68 306 boundary = self._cur.get_boundary()
jpayne@68 307 if boundary is None:
jpayne@68 308 # The message /claims/ to be a multipart but it has not
jpayne@68 309 # defined a boundary. That's a problem which we'll handle by
jpayne@68 310 # reading everything until the EOF and marking the message as
jpayne@68 311 # defective.
jpayne@68 312 defect = errors.NoBoundaryInMultipartDefect()
jpayne@68 313 self.policy.handle_defect(self._cur, defect)
jpayne@68 314 lines = []
jpayne@68 315 for line in self._input:
jpayne@68 316 if line is NeedMoreData:
jpayne@68 317 yield NeedMoreData
jpayne@68 318 continue
jpayne@68 319 lines.append(line)
jpayne@68 320 self._cur.set_payload(EMPTYSTRING.join(lines))
jpayne@68 321 return
jpayne@68 322 # Make sure a valid content type was specified per RFC 2045:6.4.
jpayne@68 323 if (str(self._cur.get('content-transfer-encoding', '8bit')).lower()
jpayne@68 324 not in ('7bit', '8bit', 'binary')):
jpayne@68 325 defect = errors.InvalidMultipartContentTransferEncodingDefect()
jpayne@68 326 self.policy.handle_defect(self._cur, defect)
jpayne@68 327 # Create a line match predicate which matches the inter-part
jpayne@68 328 # boundary as well as the end-of-multipart boundary. Don't push
jpayne@68 329 # this onto the input stream until we've scanned past the
jpayne@68 330 # preamble.
jpayne@68 331 separator = '--' + boundary
jpayne@68 332 boundaryre = re.compile(
jpayne@68 333 '(?P<sep>' + re.escape(separator) +
jpayne@68 334 r')(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$')
jpayne@68 335 capturing_preamble = True
jpayne@68 336 preamble = []
jpayne@68 337 linesep = False
jpayne@68 338 close_boundary_seen = False
jpayne@68 339 while True:
jpayne@68 340 line = self._input.readline()
jpayne@68 341 if line is NeedMoreData:
jpayne@68 342 yield NeedMoreData
jpayne@68 343 continue
jpayne@68 344 if line == '':
jpayne@68 345 break
jpayne@68 346 mo = boundaryre.match(line)
jpayne@68 347 if mo:
jpayne@68 348 # If we're looking at the end boundary, we're done with
jpayne@68 349 # this multipart. If there was a newline at the end of
jpayne@68 350 # the closing boundary, then we need to initialize the
jpayne@68 351 # epilogue with the empty string (see below).
jpayne@68 352 if mo.group('end'):
jpayne@68 353 close_boundary_seen = True
jpayne@68 354 linesep = mo.group('linesep')
jpayne@68 355 break
jpayne@68 356 # We saw an inter-part boundary. Were we in the preamble?
jpayne@68 357 if capturing_preamble:
jpayne@68 358 if preamble:
jpayne@68 359 # According to RFC 2046, the last newline belongs
jpayne@68 360 # to the boundary.
jpayne@68 361 lastline = preamble[-1]
jpayne@68 362 eolmo = NLCRE_eol.search(lastline)
jpayne@68 363 if eolmo:
jpayne@68 364 preamble[-1] = lastline[:-len(eolmo.group(0))]
jpayne@68 365 self._cur.preamble = EMPTYSTRING.join(preamble)
jpayne@68 366 capturing_preamble = False
jpayne@68 367 self._input.unreadline(line)
jpayne@68 368 continue
jpayne@68 369 # We saw a boundary separating two parts. Consume any
jpayne@68 370 # multiple boundary lines that may be following. Our
jpayne@68 371 # interpretation of RFC 2046 BNF grammar does not produce
jpayne@68 372 # body parts within such double boundaries.
jpayne@68 373 while True:
jpayne@68 374 line = self._input.readline()
jpayne@68 375 if line is NeedMoreData:
jpayne@68 376 yield NeedMoreData
jpayne@68 377 continue
jpayne@68 378 mo = boundaryre.match(line)
jpayne@68 379 if not mo:
jpayne@68 380 self._input.unreadline(line)
jpayne@68 381 break
jpayne@68 382 # Recurse to parse this subpart; the input stream points
jpayne@68 383 # at the subpart's first line.
jpayne@68 384 self._input.push_eof_matcher(boundaryre.match)
jpayne@68 385 for retval in self._parsegen():
jpayne@68 386 if retval is NeedMoreData:
jpayne@68 387 yield NeedMoreData
jpayne@68 388 continue
jpayne@68 389 break
jpayne@68 390 # Because of RFC 2046, the newline preceding the boundary
jpayne@68 391 # separator actually belongs to the boundary, not the
jpayne@68 392 # previous subpart's payload (or epilogue if the previous
jpayne@68 393 # part is a multipart).
jpayne@68 394 if self._last.get_content_maintype() == 'multipart':
jpayne@68 395 epilogue = self._last.epilogue
jpayne@68 396 if epilogue == '':
jpayne@68 397 self._last.epilogue = None
jpayne@68 398 elif epilogue is not None:
jpayne@68 399 mo = NLCRE_eol.search(epilogue)
jpayne@68 400 if mo:
jpayne@68 401 end = len(mo.group(0))
jpayne@68 402 self._last.epilogue = epilogue[:-end]
jpayne@68 403 else:
jpayne@68 404 payload = self._last._payload
jpayne@68 405 if isinstance(payload, str):
jpayne@68 406 mo = NLCRE_eol.search(payload)
jpayne@68 407 if mo:
jpayne@68 408 payload = payload[:-len(mo.group(0))]
jpayne@68 409 self._last._payload = payload
jpayne@68 410 self._input.pop_eof_matcher()
jpayne@68 411 self._pop_message()
jpayne@68 412 # Set the multipart up for newline cleansing, which will
jpayne@68 413 # happen if we're in a nested multipart.
jpayne@68 414 self._last = self._cur
jpayne@68 415 else:
jpayne@68 416 # I think we must be in the preamble
jpayne@68 417 assert capturing_preamble
jpayne@68 418 preamble.append(line)
jpayne@68 419 # We've seen either the EOF or the end boundary. If we're still
jpayne@68 420 # capturing the preamble, we never saw the start boundary. Note
jpayne@68 421 # that as a defect and store the captured text as the payload.
jpayne@68 422 if capturing_preamble:
jpayne@68 423 defect = errors.StartBoundaryNotFoundDefect()
jpayne@68 424 self.policy.handle_defect(self._cur, defect)
jpayne@68 425 self._cur.set_payload(EMPTYSTRING.join(preamble))
jpayne@68 426 epilogue = []
jpayne@68 427 for line in self._input:
jpayne@68 428 if line is NeedMoreData:
jpayne@68 429 yield NeedMoreData
jpayne@68 430 continue
jpayne@68 431 self._cur.epilogue = EMPTYSTRING.join(epilogue)
jpayne@68 432 return
jpayne@68 433 # If we're not processing the preamble, then we might have seen
jpayne@68 434 # EOF without seeing that end boundary...that is also a defect.
jpayne@68 435 if not close_boundary_seen:
jpayne@68 436 defect = errors.CloseBoundaryNotFoundDefect()
jpayne@68 437 self.policy.handle_defect(self._cur, defect)
jpayne@68 438 return
jpayne@68 439 # Everything from here to the EOF is epilogue. If the end boundary
jpayne@68 440 # ended in a newline, we'll need to make sure the epilogue isn't
jpayne@68 441 # None
jpayne@68 442 if linesep:
jpayne@68 443 epilogue = ['']
jpayne@68 444 else:
jpayne@68 445 epilogue = []
jpayne@68 446 for line in self._input:
jpayne@68 447 if line is NeedMoreData:
jpayne@68 448 yield NeedMoreData
jpayne@68 449 continue
jpayne@68 450 epilogue.append(line)
jpayne@68 451 # Any CRLF at the front of the epilogue is not technically part of
jpayne@68 452 # the epilogue. Also, watch out for an empty string epilogue,
jpayne@68 453 # which means a single newline.
jpayne@68 454 if epilogue:
jpayne@68 455 firstline = epilogue[0]
jpayne@68 456 bolmo = NLCRE_bol.match(firstline)
jpayne@68 457 if bolmo:
jpayne@68 458 epilogue[0] = firstline[len(bolmo.group(0)):]
jpayne@68 459 self._cur.epilogue = EMPTYSTRING.join(epilogue)
jpayne@68 460 return
jpayne@68 461 # Otherwise, it's some non-multipart type, so the entire rest of the
jpayne@68 462 # file contents becomes the payload.
jpayne@68 463 lines = []
jpayne@68 464 for line in self._input:
jpayne@68 465 if line is NeedMoreData:
jpayne@68 466 yield NeedMoreData
jpayne@68 467 continue
jpayne@68 468 lines.append(line)
jpayne@68 469 self._cur.set_payload(EMPTYSTRING.join(lines))
jpayne@68 470
jpayne@68 471 def _parse_headers(self, lines):
jpayne@68 472 # Passed a list of lines that make up the headers for the current msg
jpayne@68 473 lastheader = ''
jpayne@68 474 lastvalue = []
jpayne@68 475 for lineno, line in enumerate(lines):
jpayne@68 476 # Check for continuation
jpayne@68 477 if line[0] in ' \t':
jpayne@68 478 if not lastheader:
jpayne@68 479 # The first line of the headers was a continuation. This
jpayne@68 480 # is illegal, so let's note the defect, store the illegal
jpayne@68 481 # line, and ignore it for purposes of headers.
jpayne@68 482 defect = errors.FirstHeaderLineIsContinuationDefect(line)
jpayne@68 483 self.policy.handle_defect(self._cur, defect)
jpayne@68 484 continue
jpayne@68 485 lastvalue.append(line)
jpayne@68 486 continue
jpayne@68 487 if lastheader:
jpayne@68 488 self._cur.set_raw(*self.policy.header_source_parse(lastvalue))
jpayne@68 489 lastheader, lastvalue = '', []
jpayne@68 490 # Check for envelope header, i.e. unix-from
jpayne@68 491 if line.startswith('From '):
jpayne@68 492 if lineno == 0:
jpayne@68 493 # Strip off the trailing newline
jpayne@68 494 mo = NLCRE_eol.search(line)
jpayne@68 495 if mo:
jpayne@68 496 line = line[:-len(mo.group(0))]
jpayne@68 497 self._cur.set_unixfrom(line)
jpayne@68 498 continue
jpayne@68 499 elif lineno == len(lines) - 1:
jpayne@68 500 # Something looking like a unix-from at the end - it's
jpayne@68 501 # probably the first line of the body, so push back the
jpayne@68 502 # line and stop.
jpayne@68 503 self._input.unreadline(line)
jpayne@68 504 return
jpayne@68 505 else:
jpayne@68 506 # Weirdly placed unix-from line. Note this as a defect
jpayne@68 507 # and ignore it.
jpayne@68 508 defect = errors.MisplacedEnvelopeHeaderDefect(line)
jpayne@68 509 self._cur.defects.append(defect)
jpayne@68 510 continue
jpayne@68 511 # Split the line on the colon separating field name from value.
jpayne@68 512 # There will always be a colon, because if there wasn't the part of
jpayne@68 513 # the parser that calls us would have started parsing the body.
jpayne@68 514 i = line.find(':')
jpayne@68 515
jpayne@68 516 # If the colon is on the start of the line the header is clearly
jpayne@68 517 # malformed, but we might be able to salvage the rest of the
jpayne@68 518 # message. Track the error but keep going.
jpayne@68 519 if i == 0:
jpayne@68 520 defect = errors.InvalidHeaderDefect("Missing header name.")
jpayne@68 521 self._cur.defects.append(defect)
jpayne@68 522 continue
jpayne@68 523
jpayne@68 524 assert i>0, "_parse_headers fed line with no : and no leading WS"
jpayne@68 525 lastheader = line[:i]
jpayne@68 526 lastvalue = [line]
jpayne@68 527 # Done with all the lines, so handle the last header.
jpayne@68 528 if lastheader:
jpayne@68 529 self._cur.set_raw(*self.policy.header_source_parse(lastvalue))
jpayne@68 530
jpayne@68 531
jpayne@68 532 class BytesFeedParser(FeedParser):
jpayne@68 533 """Like FeedParser, but feed accepts bytes."""
jpayne@68 534
jpayne@68 535 def feed(self, data):
jpayne@68 536 super().feed(data.decode('ascii', 'surrogateescape'))