Ticket #2613: wsgi.py-memoryerror.diff

File wsgi.py-memoryerror.diff, 1.4 KB (added by Jeong-Min Lee, 9 years ago)

refactored previous patch.

  • core/handlers/wsgi.py

     
    44from django.utils import datastructures
    55from django import http
    66from pprint import pformat
     7try:
     8    from cStringIO import StringIO
     9except ImportError:
     10    from StringIO import StringIO
    711
     12from shutil import copyfileobj
     13def copyfileobj2(fsrc, fdst, length=16*1024, howmany=0):
     14    """copy data from file-like object fsrc to file-like object fdst"""
     15    if howmany == 0:
     16        return copyfileobj(fsrc, fdst, length)
     17    remain = howmany
     18    while remain > 0:
     19        buf = fsrc.read(min(length, remain))
     20        if not buf:
     21            break
     22        fdst.write(buf)
     23        remain -= len(buf)
     24
    825# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    926STATUS_CODE_TEXT = {
    1027    100: 'CONTINUE',
     
    119136        try:
    120137            return self._raw_post_data
    121138        except AttributeError:
    122             self._raw_post_data = self.environ['wsgi.input'].read(int(self.environ["CONTENT_LENGTH"]))
     139            s = StringIO()
     140            copyfileobj2(self.environ['wsgi.input'], s,
     141                howmany=int(self.environ['CONTENT_LENGTH']))
     142            self._raw_post_data = s.getvalue()
     143            s.close()
    123144            return self._raw_post_data
    124145
    125146    GET = property(_get_get, _set_get)
Back to Top