Commits

Ian Bicking committed 4d5ce47

Fix problem with CGI apps that don't give a status message, and which mix \n and \r\n in their headers

Comments (0)

Files changed (1)

 """
 import os
 import subprocess
+import urllib
 try:
     import select
 except ImportError:
     a path, then ``$PATH`` will be used.
     """
 
-    def __init__(self, 
+    def __init__(self,
                  global_conf,
                  script,
                  path=None,
     def __call__(self, environ, start_response):
         if 'REQUEST_URI' not in environ:
             environ['REQUEST_URI'] = (
-                environ.get('SCRIPT_NAME', '')
-                + environ.get('PATH_INFO', ''))
+                urllib.quote(environ.get('SCRIPT_NAME', ''))
+                + urllib.quote(environ.get('PATH_INFO', '')))
         if self.include_os_environ:
             cgi_environ = os.environ.copy()
         else:
             return
         self.buffer += data
         while '\n' in self.buffer:
-            if '\r\n' in self.buffer:
+            if '\r\n' in self.buffer and self.buffer.find('\r\n') < self.buffer.find('\n'):
                 line1, self.buffer = self.buffer.split('\r\n', 1)
             else:
                 line1, self.buffer = self.buffer.split('\n', 1)
                 value = value.lstrip()
                 name = name.strip()
                 if name.lower() == 'status':
+                    if ' ' not in value:
+                        # WSGI requires this space, sometimes CGI scripts don't set it:
+                        value = '%s General' % value
                     self.status = value
                 else:
                     self.headers.append((name, value))
     except OSError, e:
         if e.errno != 10:
             raise
-    
+
 def make_cgi_application(global_conf, script, path=None, include_os_environ=None,
                          query_string=None):
     """
     Paste Deploy interface for :class:`CGIApplication`
-    
+
     This object acts as a proxy to a CGI application.  You pass in the
     script path (``script``), an optional path to search for the
     script (if the name isn't absolute) (``path``).  If you don't give