diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-04-09 23:54:34 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-04-09 23:54:34 +0000 |
| commit | bc4638d722c0e48cfd6bc4d8084b41445f987004 (patch) | |
| tree | 3fa74811a9275d242c8f4808571abd95fe801e74 /django/utils | |
| parent | b0a60c186ee2a5564b5fa897b994e4ff2b7ad5b2 (diff) | |
Fixed #1569 -- HttpResponse now accepts iterators. Thanks, Maniac
git-svn-id: http://code.djangoproject.com/svn/django/trunk@2639 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/cache.py | 2 | ||||
| -rw-r--r-- | django/utils/httpwrappers.py | 41 |
2 files changed, 28 insertions, 15 deletions
diff --git a/django/utils/cache.py b/django/utils/cache.py index 7c7a41bafa..b888cef179 100644 --- a/django/utils/cache.py +++ b/django/utils/cache.py @@ -74,7 +74,7 @@ def patch_response_headers(response, cache_timeout=None): cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS now = datetime.datetime.utcnow() if not response.has_header('ETag'): - response['ETag'] = md5.new(response.get_content_as_string('utf8')).hexdigest() + response['ETag'] = md5.new(response.content).hexdigest() if not response.has_header('Last-Modified'): response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT') if not response.has_header('Expires'): diff --git a/django/utils/httpwrappers.py b/django/utils/httpwrappers.py index 8716505f2e..9d731d23c9 100644 --- a/django/utils/httpwrappers.py +++ b/django/utils/httpwrappers.py @@ -143,14 +143,20 @@ def parse_cookie(cookie): cookiedict[key] = c.get(key).value return cookiedict -class HttpResponse: +class HttpResponse(object): "A basic HTTP response, with content and dictionary-accessed headers" def __init__(self, content='', mimetype=None): + from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET + self._charset = DEFAULT_CHARSET if not mimetype: - from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET mimetype = "%s; charset=%s" % (DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET) - self.content = content - self.headers = {'Content-Type':mimetype} + if hasattr(content, '__iter__'): + self.iterator = content + self._is_string = False + else: + self.iterator = [content] + self._is_string = True + self.headers = {'Content-Type': mimetype} self.cookies = SimpleCookie() self.status_code = 200 @@ -193,25 +199,32 @@ class HttpResponse: except KeyError: pass - def get_content_as_string(self, encoding): - """ - Returns the content as a string, encoding it from a Unicode object if - necessary. - """ - if isinstance(self.content, unicode): - return self.content.encode(encoding) - return self.content + def _get_content(self): + content = ''.join(self.iterator) + if isinstance(content, unicode): + content = content.encode(self._charset) + return content + + def _set_content(self, value): + self.iterator = [value] + self._is_string = True + + content = property(_get_content, _set_content) # The remaining methods partially implement the file-like object interface. # See http://docs.python.org/lib/bltin-file-objects.html def write(self, content): - self.content += content + if not self._is_string: + raise Exception, "This %s instance is not writable" % self.__class__ + self.iterator.append(content) def flush(self): pass def tell(self): - return len(self.content) + if not self._is_string: + raise Exception, "This %s instance cannot tell its position" % self.__class__ + return sum(len(chunk) for chunk in self.iterator) class HttpResponseRedirect(HttpResponse): def __init__(self, redirect_to): |
