Django

Code

Changeset 2639

Show
Ignore:
Timestamp:
04/09/06 18:54:34 (3 years ago)
Author:
adrian
Message:

Fixed #1569 -- HttpResponse now accepts iterators. Thanks, Maniac

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r2548 r2639  
    7474    limodou 
    7575    Martin Maney <http://www.chipy.org/Martin_Maney> 
    76     Maniac <http://www.softwaremaniacs.org/> 
    7776    Manuzhai 
    7877    Petar Marić 
     
    9695    Brian Ray <http://brianray.chipy.org/> 
    9796    Oliver Rutherfurd <http://rutherfurd.net/> 
     97    Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/> 
    9898    David Schein 
    9999    sopel 
  • django/trunk/django/core/handlers/modpython.py

    r2364 r2639  
    163163        mod_python_req.headers_out.add('Set-Cookie', c.output(header='')) 
    164164    mod_python_req.status = http_response.status_code 
    165     mod_python_req.write(http_response.get_content_as_string(settings.DEFAULT_CHARSET)) 
     165    for chunk in http_response.iterator: 
     166        mod_python_req.write(chunk) 
    166167 
    167168def handler(req): 
  • django/trunk/django/core/handlers/wsgi.py

    r2364 r2639  
    173173        for c in response.cookies.values(): 
    174174            response_headers.append(('Set-Cookie', c.output(header=''))) 
    175         output = [response.get_content_as_string(settings.DEFAULT_CHARSET)] 
    176175        start_response(status, response_headers) 
    177         return output 
     176        return response.iterator 
  • django/trunk/django/middleware/common.py

    r1816 r2639  
    6868        # Use ETags, if requested. 
    6969        if settings.USE_ETAGS: 
    70             etag = md5.new(response.get_content_as_string(settings.DEFAULT_CHARSET)).hexdigest() 
     70            etag = md5.new(response.content).hexdigest() 
    7171            if request.META.get('HTTP_IF_NONE_MATCH') == etag: 
    7272                response = httpwrappers.HttpResponseNotModified() 
  • django/trunk/django/utils/cache.py

    r2528 r2639  
    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') 
  • django/trunk/django/utils/httpwrappers.py

    r2578 r2639  
    144144    return cookiedict 
    145145 
    146 class HttpResponse
     146class HttpResponse(object)
    147147    "A basic HTTP response, with content and dictionary-accessed headers" 
    148148    def __init__(self, content='', mimetype=None): 
     149        from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET 
     150        self._charset = DEFAULT_CHARSET 
    149151        if not mimetype: 
    150             from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET 
    151152            mimetype = "%s; charset=%s" % (DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET) 
    152         self.content = content 
    153         self.headers = {'Content-Type':mimetype} 
     153        if hasattr(content, '__iter__'): 
     154            self.iterator = content 
     155            self._is_string = False 
     156        else: 
     157            self.iterator = [content] 
     158            self._is_string = True 
     159        self.headers = {'Content-Type': mimetype} 
    154160        self.cookies = SimpleCookie() 
    155161        self.status_code = 200 
     
    194200            pass 
    195201 
    196     def get_content_as_string(self, encoding): 
    197         """ 
    198         Returns the content as a string, encoding it from a Unicode object if 
    199         necessary. 
    200         """ 
    201         if isinstance(self.content, unicode): 
    202             return self.content.encode(encoding) 
    203         return self.content 
     202    def _get_content(self): 
     203        content = ''.join(self.iterator) 
     204        if isinstance(content, unicode): 
     205            content = content.encode(self._charset) 
     206        return content 
     207 
     208    def _set_content(self, value): 
     209        self.iterator = [value] 
     210        self._is_string = True 
     211 
     212    content = property(_get_content, _set_content) 
    204213 
    205214    # The remaining methods partially implement the file-like object interface. 
    206215    # See http://docs.python.org/lib/bltin-file-objects.html 
    207216    def write(self, content): 
    208         self.content += content 
     217        if not self._is_string: 
     218            raise Exception, "This %s instance is not writable" % self.__class__ 
     219        self.iterator.append(content) 
    209220 
    210221    def flush(self): 
     
    212223 
    213224    def tell(self): 
    214         return len(self.content) 
     225        if not self._is_string: 
     226            raise Exception, "This %s instance cannot tell its position" % self.__class__ 
     227        return sum(len(chunk) for chunk in self.iterator) 
    215228 
    216229class HttpResponseRedirect(HttpResponse): 
  • django/trunk/docs/request_response.txt

    r1898 r2639  
    305305    string) and MIME type. The ``DEFAULT_MIME_TYPE`` is ``"text/html"``. 
    306306 
     307    **New in Django development version:** ``content`` can be an iterator 
     308    instead of a string. This iterator should return strings, and those strings 
     309    will be joined together to form the content of the response. 
     310 
    307311``__setitem__(header, value)`` 
    308312    Sets the given header name to the given value. Both ``header`` and 
     
    342346``get_content_as_string(encoding)`` 
    343347    Returns the content as a Python string, encoding it from a Unicode object 
    344     if necessary. 
     348    if necessary. **Removed in Django development version.** 
     349 
     350``content`` 
     351    **New in Django development version.** Returns the content as a Python 
     352    string, encoding it from a Unicode object if necessary. Note this is a 
     353    property, not a method, so use ``r.content`` instead of ``r.content()``. 
    345354 
    346355``write(content)``, ``flush()`` and ``tell()``