Author: ccahoon
Date: 2009-07-10 14:02:34 -0500 (Fri, 10 Jul 2009)
New Revision: 11212

Modified:
   django/branches/soc2009/http-wsgi-improvements/django/http/__init__.py
   
django/branches/soc2009/http-wsgi-improvements/tests/regressiontests/sendfile/tests.py
Log:
[soc2009/http-wsgi-improvements] HttpResponseSendFile now uses 
django.core.servers.basehttp.FileWrapper inside __iter__ to provide fallback.

regressiontests.sendfile uses this now, and passes. The fallback was tested
using guppy and apache2 with mod_wsgi for heap issues, and it appears to be
fine. We can go back and look at this again if it becomes an issue.

Modified: django/branches/soc2009/http-wsgi-improvements/django/http/__init__.py
===================================================================
--- django/branches/soc2009/http-wsgi-improvements/django/http/__init__.py      
2009-07-09 19:53:18 UTC (rev 11211)
+++ django/branches/soc2009/http-wsgi-improvements/django/http/__init__.py      
2009-07-10 19:02:34 UTC (rev 11212)
@@ -430,6 +430,8 @@
         return sum([len(chunk) for chunk in self._container])
 
 class HttpResponseSendFile(HttpResponse): 
+    sendfile_fh = None
+
     def __init__(self, path_to_file, content_type=None, block_size=8192): 
            if not content_type: 
                from mimetypes import guess_type 
@@ -445,10 +447,14 @@
                    os.path.basename(path_to_file)) 
            self[settings.HTTPRESPONSE_SENDFILE_HEADER] = path_to_file 
 
-    def _get_content(self):
-        return open(self.sendfile_filename).read()
+    def __iter__(self):
+        from django.core.servers.basehttp import FileWrapper
+        return FileWrapper(self.get_file_handler(), self.block_size)
 
-    content = property(_get_content)
+    def get_file_handler(self):
+        if not self.sendfile_fh:
+            self.sendfile_fh = open(self.sendfile_filename, 'rb')
+        return self.sendfile_fh
 
 class HttpResponseRedirect(HttpResponse):
     _status_code = 302

Modified: 
django/branches/soc2009/http-wsgi-improvements/tests/regressiontests/sendfile/tests.py
===================================================================
--- 
django/branches/soc2009/http-wsgi-improvements/tests/regressiontests/sendfile/tests.py
      2009-07-09 19:53:18 UTC (rev 11211)
+++ 
django/branches/soc2009/http-wsgi-improvements/tests/regressiontests/sendfile/tests.py
      2009-07-10 19:02:34 UTC (rev 11212)
@@ -25,10 +25,11 @@
         self.assertEqual(response['Content-Length'], str(FILE_SIZE))
         self.assertEqual(response['Content-Type'], 'application/pdf')
 
-        # *if* the degraded case is to be supported, add this instead:
-        self.assertEqual(response.content, CONTENT)
+        # Test the fallback file transfer -- we use FileWrapper to iterate 
through
+        # the file, this also wraps close(). This appears to mitigate 
performance
+        # issues.
+        self.assertEqual("".join(iter(response)), CONTENT)
         get_content = lambda: response.content.read()
-        #self.assertRaises(TypeError, get_content)
 
         file1.close()
         # TODO: test middleware bypass etc


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to