Ticket #1569: 1569.m-r.2.diff

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

More correct patch for magic-removal

  • django/http/__init__.py

     
    146146        cookiedict[key] = c.get(key).value
    147147    return cookiedict
    148148
    149 class HttpResponse:
     149class HttpResponse(object):
    150150    "A basic HTTP response, with content and dictionary-accessed headers"
    151151    def __init__(self, content='', mimetype=None):
     152        from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET
     153        self._charset = DEFAULT_CHARSET
    152154        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
     155            mimetype = "%s; charset=%s" % (DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET)
     156        if hasattr(content,'__iter__'):
     157            self.iterator = content
     158            self._is_string = False
     159        else:
     160            self.iterator = [content]
     161            self._is_string = True
    156162        self.headers = {'Content-Type':mimetype}
    157163        self.cookies = SimpleCookie()
    158164        self.status_code = 200
     
    196202        except KeyError:
    197203            pass
    198204
    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
     205    def _get_content(self):
     206        content = ''.join(self.iterator)
     207        if isinstance(content, unicode):
     208            content = content.encode(self._charset)
     209        return content
    207210
     211    def _set_content(self, value):
     212        self.iterator = [value]
     213        self._is_string = True
     214
     215    content = property(_get_content,_set_content)
     216
    208217    # The remaining methods partially implement the file-like object interface.
    209218    # See http://docs.python.org/lib/bltin-file-objects.html
    210219    def write(self, content):
    211         self.content += content
     220        if not self._is_string:
     221            raise Exception, "This %s instance is not writable"%self.__class__
     222        self.iterator.append(content)
    212223
    213224    def flush(self):
    214225        pass
    215226
    216227    def tell(self):
    217         return len(self.content)
     228        if not self._is_string:
     229            raise Exception, "This %s instance cannot tell its position"%self.__class__
     230        return sum(len(chunk) for chunk in self.iterator)
    218231
    219232class HttpResponseRedirect(HttpResponse):
    220233    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