Ticket #2131: http-response-send-file.2.diff

File http-response-send-file.2.diff, 2.9 KB (added by ymasuda[at]ethercube.com, 18 years ago)

fixed typos above, sorry

  • http/__init__.py

     
    238238            raise Exception, "This %s instance cannot tell its position" % self.__class__
    239239        return sum([len(chunk) for chunk in self._iterator])
    240240
     241class HttpResponseSendFile(HttpResponse):
     242    def __init__(self, path_to_file, content_type=None):
     243        HttpResponse.__init__(self)
     244        if not content_type:
     245            from mimetypes import guess_type
     246            content_type = guess_type(path_to_file)[0]
     247        self['Content-Type'] = content_type
     248        self.sendfile_filename = path_to_file
     249        self.block_size = 8192
     250        self.status_code = 200
     251
    241252class HttpResponseRedirect(HttpResponse):
    242253    def __init__(self, redirect_to):
    243254        HttpResponse.__init__(self)
  • core/servers/basehttp.py

     
    307307        in the event loop to iterate over the data, and to call
    308308        'self.close()' once the response is finished.
    309309        """
    310         if not self.result_is_file() and not self.sendfile():
    311             for data in self.result:
    312                 self.write(data)
    313             self.finish_content()
     310        for data in self.result:
     311            self.write(data)
     312        self.finish_content()
    314313        self.close()
    315314
    316315    def get_scheme(self):
  • core/handlers/wsgi.py

     
    160160        for c in response.cookies.values():
    161161            response_headers.append(('Set-Cookie', c.output(header='')))
    162162        start_response(status, response_headers)
     163        if hasattr(response, 'sendfile_filename'):
     164            filelike = open(response.sendfile_filename, 'rb')
     165            block_size = response.block_size
     166            if 'wsgi.file_wrapper' in environ:
     167                return environ['wsgi.file_wrapper'](filelike, block_size)
     168            else:
     169                return iter(lambda: filelike.read(block_size), '')
    163170        return response.iterator
  • core/handlers/modpython.py

     
    143143    "Populates the mod_python request object with an HttpResponse"
    144144    from django.conf import settings
    145145    mod_python_req.content_type = http_response['Content-Type']
     146    if hasattr(http_response, 'sendfile_filename'):
     147        mod_python_req.sendfile(http_response.sendfile_filename)
     148        return
    146149    for key, value in http_response.headers.items():
    147150        if key != 'Content-Type':
    148151            mod_python_req.headers_out[key] = value
Back to Top