Ticket #6256: 6256-2.diff

File 6256-2.diff, 2.0 KB (added by Karen Tracey <kmtracey@…>, 7 years ago)

Better patch that doesn't corrupt binary data.

  • django/http/__init__.py

     
    99except ImportError:
    1010    from cgi import parse_qsl
    1111
     12from email.parser import Parser
     13try:
     14    from email.feedparser import FeedParser
     15    class ParserClass(Parser):
     16        # Copy of Python's Parser's parse patched to properly deal with CRLF
     17        # sequences that happen to fall at an 8192-byte boundary (see
     18        # http://bugs.python.org/issue1555570)
     19        def parse(self, fp, headersonly=False):
     20            """Create a message structure from the data in a file.
     21   
     22            Reads all the data from the file and returns the root of the message
     23            structure.  Optional headersonly is a flag specifying whether to stop
     24            parsing after reading the headers or not.  The default is False,
     25            meaning it parses the entire contents of the file.
     26            """
     27            feedparser = FeedParser(self._class)
     28            if headersonly:
     29                feedparser._set_headersonly()
     30            while True:
     31                data = fp.read(8192)
     32                if not data:
     33                    break
     34                if data[-1] == '\r':
     35                    # Don't split a CRLF.
     36                    data += fp.read(1)
     37                feedparser.feed(data)
     38            return feedparser.close()
     39except ImportError:
     40    ParserClass = Parser
     41
    1242from django.utils.datastructures import MultiValueDict, FileDict
    1343from django.utils.encoding import smart_str, iri_to_uri, force_unicode
    1444
     
    108138    from cgi import parse_header
    109139    raw_message = '\r\n'.join(['%s:%s' % pair for pair in header_dict.items()])
    110140    raw_message += '\r\n\r\n' + post_data
    111     msg = email.message_from_string(raw_message)
     141    msg = ParserClass().parsestr(raw_message)
    112142    POST = QueryDict('', mutable=True)
    113143    FILES = MultiValueDict()
    114144    for submessage in msg.get_payload():
Back to Top