Ticket #1569: 1569.m-r.diff

File 1569.m-r.diff, 5.1 KB (added by Maniac <Maniac@…>, 9 years ago)

Patch for magic-removal branch

  • django/http/__init__.py

     
    22from pprint import pformat
    33from urllib import urlencode
    44from django.utils.datastructures import MultiValueDict
     5from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET
    56
    67try:
    78    # The mod_python version is more efficient, so try importing it first.
     
    146147        cookiedict[key] = c.get(key).value
    147148    return cookiedict
    148149
    149 class HttpResponse:
     150class HttpResponse(object):
    150151    "A basic HTTP response, with content and dictionary-accessed headers"
    151152    def __init__(self, content='', mimetype=None):
    152153        if not mimetype:
    153             from django.conf import settings
    154             mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET)
    155         self.content = content
     154            mimetype = "%s; charset=%s" % (DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET)
     155        if hasattr(content,'__iter__'):
     156            self.iterator = content
     157            self._is_string = False
     158        else:
     159            self.iterator = [content]
     160            self._is_string = True
    156161        self.headers = {'Content-Type':mimetype}
    157162        self.cookies = SimpleCookie()
    158163        self.status_code = 200
     
    196201        except KeyError:
    197202            pass
    198203
    199     def get_content_as_string(self, encoding):
    200         """
    201         Returns the content as a string, encoding it from a Unicode object if
    202         necessary.
    203         """
    204         if isinstance(self.content, unicode):
    205             return self.content.encode(encoding)
    206         return self.content
     204    def _get_content(self):
     205        content = ''.join(self.iterator)
     206        if isinstance(content, unicode):
     207            content = content.encode(DEFAULT_CHARSET)
     208        return content
    207209
     210    def _set_content(self, value):
     211        self.iterator = [value]
     212        self._is_string = True
     213
     214    content = property(_get_content,_set_content)
     215
    208216    # The remaining methods partially implement the file-like object interface.
    209217    # See http://docs.python.org/lib/bltin-file-objects.html
    210218    def write(self, content):
    211         self.content += content
     219        if not self._is_string:
     220            raise Exception, "This %s instance is not writable"%self.__class__
     221        self.iterator.append(content)
    212222
    213223    def flush(self):
    214224        pass
    215225
    216226    def tell(self):
    217         return len(self.content)
     227        if not self._is_string:
     228            raise Exception, "This %s instance cannot tell its position"%self.__class__
     229        return sum(len(chunk) for chunk in self.iterator)
    218230
    219231class HttpResponseRedirect(HttpResponse):
    220232    def __init__(self, redirect_to):
  • django/core/handlers/wsgi.py

     
    159159        response_headers = response.headers.items()
    160160        for c in response.cookies.values():
    161161            response_headers.append(('Set-Cookie', c.output(header='')))
    162         output = [response.get_content_as_string(settings.DEFAULT_CHARSET)]
    163162        start_response(status, response_headers)
    164         return output
     163        return response.iterator
  • django/core/handlers/modpython.py

     
    149149    for c in http_response.cookies.values():
    150150        mod_python_req.headers_out.add('Set-Cookie', c.output(header=''))
    151151    mod_python_req.status = http_response.status_code
    152     mod_python_req.write(http_response.get_content_as_string(settings.DEFAULT_CHARSET))
     152    for chunk in http_response.iterator:
     153        mod_python_req.write(chunk)
    153154
    154155def handler(req):
    155156    # mod_python hooks into this function.
  • django/utils/cache.py

     
    7474        cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
    7575    now = datetime.datetime.utcnow()
    7676    if not response.has_header('ETag'):
    77         response['ETag'] = md5.new(response.get_content_as_string('utf8')).hexdigest()
     77        response['ETag'] = md5.new(response.content).hexdigest()
    7878    if not response.has_header('Last-Modified'):
    7979        response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
    8080    if not response.has_header('Expires'):
  • django/middleware/common.py

     
    6868
    6969        # Use ETags, if requested.
    7070        if settings.USE_ETAGS:
    71             etag = md5.new(response.get_content_as_string(settings.DEFAULT_CHARSET)).hexdigest()
     71            etag = md5.new(response.content).hexdigest()
    7272            if request.META.get('HTTP_IF_NONE_MATCH') == etag:
    7373                response = http.HttpResponseNotModified()
    7474            else:
Back to Top