def test_multipart_newline_boundary(self): p = parser.Parser() msg = '''MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="ALTERNATIVE_BOUNDARY" --ALTERNATIVE_BOUNDARY Content-Type: text/plain hello --ALTERNATIVE_BOUNDARY-- ''' msgobj = p.parsestr(msg) assert msgobj.get_payload(0).get_payload() == 'hello'
def parse_headers(fp, _class=HTTPMessage): """Parses only RFC2822 headers from a file pointer. email Parser wants to see strings rather than bytes. But a TextIOWrapper around self.rfile would buffer too many bytes from the stream, bytes which we later need to read as bytes. So we read the correct bytes here, as bytes, for email Parser to parse. """ headers = [] while True: line = fp.readline(_MAXLINE + 1) if len(line) > _MAXLINE: raise LineTooLong("header line") headers.append(line) if len(headers) > _MAXHEADERS: raise HTTPException("got more than %d headers" % _MAXHEADERS) if line in (b'\r\n', b'\n', b''): break hstring = bytes(b'').join(headers).decode('iso-8859-1') return email_parser.Parser(_class=_class).parsestr(hstring)