Ticket #7894: file_wrapper_response2.diff
File file_wrapper_response2.diff, 6.0 KB (added by , 16 years ago) |
---|
-
django/http/__init__.py
424 424 def __init__(self, *args, **kwargs): 425 425 HttpResponse.__init__(self, *args, **kwargs) 426 426 427 428 class FileWrapper(object): 429 def __init__(self, file_path, block_size, byte_count, offset): 430 self.__dict__.update(locals()) 431 432 def __iter__(self): 433 self.file = open(self.file_path, 'rb') 434 self.close = self.file.close 435 if self.offset: 436 self.file.seek(self.offset) 437 self.bytes_left = self.byte_count 438 return self 439 440 def next(self): 441 if self.bytes_left > 0: 442 data_size = min(self.block_size, self.bytes_left) 443 data = self.file.read(data_size) 444 if data: 445 self.bytes_left -= data_size 446 return data 447 raise StopIteration 448 449 450 class HttpResponseFileWrapper(HttpResponse): 451 def __init__(self, file_path, block_size=8192, length=None, offset=0, **kwargs): 452 content_length = length or os.path.getsize(file_path) 453 HttpResponse.__init__(self, content=FileWrapper(file_path, block_size, content_length, offset), **kwargs) 454 self.file_path = file_path 455 self.block_size = block_size 456 self.length = length 457 self.offset = offset 458 self['Content-Length'] = content_length 459 460 427 461 # A backwards compatible alias for HttpRequest.get_host. 428 462 def get_host(request): 429 463 return request.get_host() -
django/core/servers/basehttp.py
312 312 in the event loop to iterate over the data, and to call 313 313 'self.close()' once the response is finished. 314 314 """ 315 if not self.result_is_file() andnot self.sendfile():315 if not self.result_is_file() or not self.sendfile(): 316 316 for data in self.result: 317 317 self.write(data) 318 318 self.finish_content() -
django/core/handlers/wsgi.py
1 1 from threading import Lock 2 import os 2 3 from pprint import pformat 3 4 try: 4 5 from cStringIO import StringIO … … 194 195 initLock = Lock() 195 196 request_class = WSGIRequest 196 197 198 def wsgi_adaptor_honours_content_length(self, environ): 199 return 'mod_wsgi.callable_object' in environ 200 197 201 def __call__(self, environ, start_response): 198 202 from django.conf import settings 199 203 … … 232 236 for c in response.cookies.values(): 233 237 response_headers.append(('Set-Cookie', str(c.output(header='')))) 234 238 start_response(status, response_headers) 235 return response 239 if (isinstance(response, http.HttpResponseFileWrapper) and 240 'wsgi.file_wrapper'in environ and 241 (response.length is None or 242 self.wsgi_adaptor_honours_content_length(environ) or 243 response.length == os.path.getsize(response.file_path))): 244 f = open(response.file_path, 'rb') 245 if response.offset: 246 f.seek(response.offset) 247 return environ['wsgi.file_wrapper'](f, response.block_size) 248 else: 249 return response 236 250 -
django/core/handlers/modpython.py
199 199 req.headers_out.add('Set-Cookie', c.output(header='')) 200 200 req.status = response.status_code 201 201 try: 202 for chunk in response: 203 req.write(chunk) 202 if isinstance(response, http.HttpResponseFileWrapper): 203 if response.length is None: 204 length = -1 205 else: 206 length = response.length 207 req.sendfile(response.file_path, response.offset, length) 208 else: 209 for chunk in response: 210 req.write(chunk) 204 211 finally: 205 212 response.close() 206 207 213 return 0 # mod_python.apache.OK 208 214 209 215 def handler(req): -
tests/regressiontests/httpwrappers/tests.py
426 426 Traceback (most recent call last): 427 427 ... 428 428 UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format 429 429 430 431 ########################### 432 # HttpResponseFileWrapper # 433 ########################### 434 435 Get the path of the test file 436 437 >>> import os 438 >>> test_file_path = os.path.join(os.path.dirname(__file__), 'test_file.txt') 439 440 Create a response with the file path 441 442 >>> r = HttpResponseFileWrapper(test_file_path, mimetype='text/plain') 443 444 The response's content defaults to an iterator over the file data 445 446 >>> ''.join(list(r)) 447 '123456789 Heres some data!' 448 449 An offset and length can be provided limit the data read from the file 450 451 >>> r = HttpResponseFileWrapper(test_file_path, offset=2, length=5) 452 >>> ''.join(list(r)) 453 '34567' 454 455 A block_size can also be passed in as a suggestion for the block size to read 456 457 >>> r = HttpResponseFileWrapper(test_file_path, offset=2, length=5, block_size=4024) 458 459 Handler can choose to deliver the file's data using alternative means by using the response's properties rather than treating it as an iterator 460 461 >>> r.file_path == test_file_path 462 True 463 464 >>> r.offset 465 2 466 467 >>> r.length 468 5 469 470 >>> r.block_size 471 4024 472 430 473 """ 431 474 432 from django.http import QueryDict, HttpResponse 475 from django.http import QueryDict, HttpResponse, HttpResponseFileWrapper 433 476 434 477 if __name__ == "__main__": 435 478 import doctest