Author: ccahoon
Date: 2009-07-17 21:02:57 -0500 (Fri, 17 Jul 2009)
New Revision: 11268

Modified:
   django/branches/soc2009/http-wsgi-improvements/django/conf/global_settings.py
   
django/branches/soc2009/http-wsgi-improvements/django/core/handlers/modpython.py
   django/branches/soc2009/http-wsgi-improvements/django/core/handlers/wsgi.py
   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] Establish the priorities and fallbacks for 
HttpResponseSendFile methods.

Change the setting to settings.HTTPRESPONSE_SENDFILE_METHOD. If this is set to 
None, we use handler methods,
but otherwise the header gets set, and we do not send any content. If neither 
of these are available,
use the FileWrapper fallback in HttpResponseSendFile.

This passes the test suite, but is untested on mod_python. I am still trying to 
figure out how to
view the headers of a response with the Content-Disposition "attachment."

Modified: 
django/branches/soc2009/http-wsgi-improvements/django/conf/global_settings.py
===================================================================
--- 
django/branches/soc2009/http-wsgi-improvements/django/conf/global_settings.py   
    2009-07-17 20:04:03 UTC (rev 11267)
+++ 
django/branches/soc2009/http-wsgi-improvements/django/conf/global_settings.py   
    2009-07-18 02:02:57 UTC (rev 11268)
@@ -236,9 +236,13 @@
 # Example: "http://media.lawrence.com";
 MEDIA_URL = ''
 
-# Header to use in HttpResponseSendFile to inform the handler to serve the 
-# file with efficient handler-specific routines. 
-HTTPRESPONSE_SENDFILE_HEADER = 'X-Sendfile' 
+# Header to use in HttpResponseSendFile to inform the handler to serve the
+# file with efficient handler-specific routines. None causes 
HttpResponseSendFile
+# to fall back to, first, mechanisms in the handler (wsgi.filewrapper and
+# req.sendfile.
+# Examples: 'X-Sendfile' (FastCGI, lighttpd, Apache with mod_xsendfile),
+#           'X-Accel-Redirect' (nginx)
+HTTPRESPONSE_SENDFILE_METHOD = None
 
 # List of upload handler classes to be applied in order.
 FILE_UPLOAD_HANDLERS = (

Modified: 
django/branches/soc2009/http-wsgi-improvements/django/core/handlers/modpython.py
===================================================================
--- 
django/branches/soc2009/http-wsgi-improvements/django/core/handlers/modpython.py
    2009-07-17 20:04:03 UTC (rev 11267)
+++ 
django/branches/soc2009/http-wsgi-improvements/django/core/handlers/modpython.py
    2009-07-18 02:02:57 UTC (rev 11268)
@@ -200,15 +200,15 @@
         for c in response.cookies.values():
             req.headers_out.add('Set-Cookie', c.output(header=''))
         req.status = response.status_code
-        if isinstance(response, http.HttpResponseSendFile): 
-            req.sendfile(response.sendfile_filename) 
+        if isinstance(response, http.HttpResponseSendFile):
+            req.sendfile(response.sendfile_filename)
         else:
-            try:
-                for chunk in response:
-                    req.write(chunk)
-            finally:
-                response.close()
-
+            # If we are using a header to do sendfile, set the header and send 
empty content
+            if settings.RESPONSE_SENDFILE_METHOD:
+                response.set_empty_content()
+                response[settings.HTTPRESPONSE_SENDFILE_METHOD] = 
response.sendfile_filename
+            for chunk in response:
+                req.write(chunk)
         return 0 # mod_python.apache.OK
 
 def handler(req):

Modified: 
django/branches/soc2009/http-wsgi-improvements/django/core/handlers/wsgi.py
===================================================================
--- django/branches/soc2009/http-wsgi-improvements/django/core/handlers/wsgi.py 
2009-07-17 20:04:03 UTC (rev 11267)
+++ django/branches/soc2009/http-wsgi-improvements/django/core/handlers/wsgi.py 
2009-07-18 02:02:57 UTC (rev 11268)
@@ -243,14 +243,11 @@
         start_response(status, response_headers)
         
         if isinstance(response, http.HttpResponseSendFile): 
-            filelike = open(response.sendfile_filename, 'rb') 
-            if 'wsgi.file_wrapper' in environ: 
+            if settings.HTTPRESPONSE_SENDFILE_METHOD:
+                response[settings.HTTPRESPONSE_SENDFILE_METHOD] = 
response.sendfile_filename
+            elif 'wsgi.file_wrapper' in environ:
+                filelike = open(response.sendfile_filename, 'rb')
                 return environ['wsgi.file_wrapper'](filelike, 
-                        response.block_size) 
-            else: 
-                # wraps close() as well 
-                from django.core.servers.basehttp import FileWrapper 
-                return FileWrapper(filelike, response.block_size) 
-                
+                        response.block_size)
         return response
 

Modified: django/branches/soc2009/http-wsgi-improvements/django/http/__init__.py
===================================================================
--- django/branches/soc2009/http-wsgi-improvements/django/http/__init__.py      
2009-07-17 20:04:03 UTC (rev 11267)
+++ django/branches/soc2009/http-wsgi-improvements/django/http/__init__.py      
2009-07-18 02:02:57 UTC (rev 11268)
@@ -447,9 +447,14 @@
         self['Content-Length'] = os.path.getsize(path_to_file)
         self['Content-Disposition'] = ('attachment; filename=%s' %
              os.path.basename(path_to_file))
-        self[settings.HTTPRESPONSE_SENDFILE_HEADER] = path_to_file
+        self._empty_content = False
 
+    def set_empty_content(self):
+        self._empty_content = True
+
     def __iter__(self):
+        if self._empty_content:
+            return iter([''])
         from django.core.servers.basehttp import FileWrapper
         return FileWrapper(self.get_file_handler(), self.block_size)
 

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-17 20:04:03 UTC (rev 11267)
+++ 
django/branches/soc2009/http-wsgi-improvements/tests/regressiontests/sendfile/tests.py
      2009-07-18 02:02:57 UTC (rev 11268)
@@ -18,8 +18,8 @@
                 urllib.quote(file1.name))
 
         self.assertEqual(response.status_code, 200)
-        self.assertEqual(response[settings.HTTPRESPONSE_SENDFILE_HEADER],
-                file1.name)
+        #self.assertEqual(response[settings.HTTPRESPONSE_SENDFILE_METHOD],
+        #        file1.name)
         self.assertEqual(response['Content-Disposition'],
                 'attachment; filename=%s' % os.path.basename(file1.name))
         self.assertEqual(response['Content-Length'], str(FILE_SIZE))


--~--~---------~--~----~------------~-------~--~----~
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