Ticket #15281: 15281.patch
File 15281.patch, 3.9 KB (added by , 14 years ago) |
---|
-
django/views/static.py
7 7 import os 8 8 import posixpath 9 9 import re 10 import stat11 10 import urllib 12 11 from email.Utils import parsedate_tz, mktime_tz 13 12 … … 57 56 mimetype, encoding = mimetypes.guess_type(fullpath) 58 57 mimetype = mimetype or 'application/octet-stream' 59 58 if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), 60 statobj [stat.ST_MTIME], statobj[stat.ST_SIZE]):59 statobj.st_mtime, statobj.st_size): 61 60 return HttpResponseNotModified(mimetype=mimetype) 62 contents = open(fullpath, 'rb') .read()61 contents = open(fullpath, 'rb') 63 62 response = HttpResponse(contents, mimetype=mimetype) 64 response["Last-Modified"] = http_date(statobj [stat.ST_MTIME])65 response["Content-Length"] = len(contents)63 response["Last-Modified"] = http_date(statobj.st_mtime) 64 response["Content-Length"] = statobj.st_size 66 65 if encoding: 67 66 response["Content-Encoding"] = encoding 68 67 return response -
tests/regressiontests/views/tests/static.py
26 26 for filename in media_files: 27 27 response = self.client.get('/views/%s/%s' % (self.prefix, filename)) 28 28 file_path = path.join(media_dir, filename) 29 self.assertEquals(open(file_path).read(), response.content) 30 self.assertEquals(len(response.content), int(response['Content-Length'])) 29 content = str(response.content) 30 self.assertEquals(open(file_path).read(), content) 31 self.assertEquals(len(content), int(response['Content-Length'])) 31 32 self.assertEquals(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None)) 32 33 34 def test_serve_does_not_buffer_files(self): 35 "The static view uses an iterator - see #15281" 36 response = self.client.get('/views/%s/file.txt' % self.prefix) 37 # response objects can't be used as file-like objects when they are 38 # initialized with an iterator 39 self.assertRaises(Exception, response.write, 'more text') 40 33 41 def test_unknown_mime_type(self): 34 42 response = self.client.get('/views/%s/file.unknown' % self.prefix) 35 43 self.assertEquals('application/octet-stream', response['Content-Type']) … … 68 76 response = self.client.get('/views/%s/%s' % (self.prefix, file_name), 69 77 HTTP_IF_MODIFIED_SINCE=invalid_date) 70 78 file = open(path.join(media_dir, file_name)) 71 self.assertEquals(file.read(),response.content)72 self.assertEquals( len(response.content),73 79 content = str(response.content) 80 self.assertEquals(file.read(), content) 81 self.assertEquals(len(content), int(response['Content-Length'])) 74 82 75 83 def test_invalid_if_modified_since2(self): 76 84 """Handle even more bogus If-Modified-Since values gracefully … … 82 90 invalid_date = ': 1291108438, Wed, 20 Oct 2010 14:05:00 GMT' 83 91 response = self.client.get('/views/%s/%s' % (self.prefix, file_name), 84 92 HTTP_IF_MODIFIED_SINCE=invalid_date) 93 content = str(response.content) 85 94 file = open(path.join(media_dir, file_name)) 86 self.assertEquals(file.read(), response.content) 87 self.assertEquals(len(response.content), 88 int(response['Content-Length'])) 95 self.assertEquals(file.read(), content) 96 self.assertEquals(len(content), int(response['Content-Length'])) 89 97 90 98 91 99 class StaticHelperTest(StaticTests):