Ticket #2131: httpresponsesendfile-no-default-content.diff

File httpresponsesendfile-no-default-content.diff, 3.7 KB (added by mrts, 6 years ago)

Disallow default HttpResponse behaviour (content etc)

  • django/http/__init__.py

     
    393393            raise Exception("This %s instance cannot tell its position" % self.__class__)
    394394        return sum([len(chunk) for chunk in self._container])
    395395
     396class HttpResponseSendFile(HttpResponse):
     397    def __init__(self, path_to_file, content_type=None, block_size=8192):
     398        if not content_type:
     399            from mimetypes import guess_type
     400            content_type = guess_type(path_to_file)[0]
     401            if content_type is None:
     402                content_type = "application/octet-stream"
     403        super(HttpResponseSendFile, self).__init__(None,
     404                content_type=content_type)
     405        self.sendfile_filename = path_to_file
     406        self.block_size = block_size
     407        self._headers['content-length'] = ('Content-Length',
     408                os.path.getsize(path_to_file))
     409
    396410class HttpResponseRedirect(HttpResponse):
    397411    status_code = 302
    398412
  • django/core/servers/basehttp.py

     
    313313        in the event loop to iterate over the data, and to call
    314314        'self.close()' once the response is finished.
    315315        """
    316         if not self.result_is_file() and not self.sendfile():
    317             for data in self.result:
    318                 self.write(data)
    319             self.finish_content()
     316        for data in self.result:
     317            self.write(data)
     318        self.finish_content()
    320319        self.close()
    321320
    322321    def get_scheme(self):
  • django/core/handlers/wsgi.py

     
    252252        except KeyError:
    253253            status_text = 'UNKNOWN STATUS CODE'
    254254        status = '%s %s' % (response.status_code, status_text)
     255
    255256        response_headers = [(str(k), str(v)) for k, v in response.items()]
    256257        for c in response.cookies.values():
    257258            response_headers.append(('Set-Cookie', str(c.output(header=''))))
     259
    258260        start_response(status, response_headers)
     261
     262        if isinstance(response, http.HttpResponseSendFile):
     263            filelike = open(response.sendfile_filename, 'rb')
     264            if 'wsgi.file_wrapper' in environ:
     265                return environ['wsgi.file_wrapper'](filelike,
     266                        response.block_size)
     267            else:
     268                # wraps close() as well
     269                from django.core.servers.basehttp import FileWrapper
     270                return FileWrapper(filelike, response.block_size)
     271
    259272        return response
    260273
  • django/core/handlers/modpython.py

     
    215215        for c in response.cookies.values():
    216216            req.headers_out.add('Set-Cookie', c.output(header=''))
    217217        req.status = response.status_code
    218         try:
    219             for chunk in response:
    220                 req.write(chunk)
    221         finally:
    222             response.close()
    223218
     219        if isinstance(response, http.HttpResponseSendFile):
     220            req.sendfile(response.sendfile_filename)
     221        else:
     222            try:
     223                for chunk in response:
     224                    req.write(chunk)
     225            finally:
     226                response.close()
     227
    224228        return 0 # mod_python.apache.OK
    225229
    226230def handler(req):
Back to Top