Ticket #2131: django-ticket-2131-http-response-send-file.2-django1.0.2final-v1.2_test.diff

File django-ticket-2131-http-response-send-file.2-django1.0.2final-v1.2_test.diff, 1.9 KB (added by mizatservercave, 6 years ago)

PARTIAL patch (FOR TESTING ONLY) to verify HttpResponseSendFile works without specific handler support

  • django/core/servers/basehttp.py

    diff -u django/core/servers/basehttp.py 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/http/__init__.py

    diff -u django/http/__init__.py 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):
     398        HttpResponse.__init__(self)
     399        if not content_type:
     400            from mimetypes import guess_type
     401            content_type = guess_type(path_to_file)[0]
     402            if content_type == None: # probably windows w/o proper mimetype lookup
     403                content_type = "application/octet-stream"
     404        self['Content-Type'] = content_type
     405        self.sendfile_filename = path_to_file
     406        self.block_size = 8192
     407        self.status_code = 200
     408        # compatibility; fake being a regular HttpResponse if needed
     409        self['Content-Length'] = os.path.getsize(path_to_file)
     410        self._container = open(path_to_file)
     411        self._is_string = False
     412
    396413class HttpResponseRedirect(HttpResponse):
    397414    status_code = 302
    398415
Back to Top