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

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

updated for Django 1.0.2

  • django/core/handlers/modpython.py

    diff -u -r django/core/handlers/modpython.py django/core/handlers/modpython.py
     
    209209
    210210        # Convert our custom HttpResponse object back into the mod_python req.
    211211        req.content_type = response['Content-Type']
     212        if hasattr(http_response, 'sendfile_filename'):
     213            mod_python_req.sendfile(http_response.sendfile_filename)
     214            return
    212215        for key, value in response.items():
    213216            if key != 'content-type':
    214217                req.headers_out[str(key)] = str(value)
  • django/core/handlers/wsgi.py

    diff -u -r django/core/handlers/wsgi.py django/core/handlers/wsgi.py
     
    250250        except KeyError:
    251251            status_text = 'UNKNOWN STATUS CODE'
    252252        status = '%s %s' % (response.status_code, status_text)
     253
     254        if hasattr(response, 'sendfile_filename') and response.has_header('Content-Length'):
     255            del response['Content-Length']
    253256        response_headers = [(str(k), str(v)) for k, v in response.items()]
    254257        for c in response.cookies.values():
    255258            response_headers.append(('Set-Cookie', str(c.output(header=''))))
    256259        start_response(status, response_headers)
     260        if hasattr(response, 'sendfile_filename'):
     261            filelike = open(response.sendfile_filename, 'rb')
     262            block_size = response.block_size
     263            if 'wsgi.file_wrapper' in environ:
     264                return environ['wsgi.file_wrapper'](filelike, block_size)
     265            else:
     266                return iter(lambda: filelike.read(block_size), '')
     267
    257268        return response
    258269
  • django/core/servers/basehttp.py

    diff -u -r 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 -r 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
    396409class HttpResponseRedirect(HttpResponse):
    397410    status_code = 302
    398411
Back to Top