situation:
apache / mod_wsgi

reproduce:
in routes.py

routes_onerror = [('appname/*','/appname/error/index')]


controllers/error.py
response.status = request.vars.code
return dict()

setting response.status is here to make sure that I keep the same status 
code..

This results in
mod_wsgi (pid=1125): Exception occurred processing WSGI script 
'/var/www/web2py/wsgihandler.py'.
Traceback (most recent call 
last):                                                              
  File "/var/www/web2py/gluon/main.py", line 604, in 
wsgibase                                   
    return 
wsgibase(new_environ,responder)                                                 
     

  File "/var/www/web2py/gluon/main.py", line 607, in 
wsgibase                                   
    return 
http_response.to(responder)                                                     
     

  File "/var/www/web2py/gluon/http.py", line 94, in 
to                                          
    responder(status, 
headers)                                                                  
ValueError: status message was not 
supplied                                                     


To avoid this, we can do a few things
first of all, try to cast the status to an int (in the test it's a string 
so it's not matching any item in defined_status)
second, make sure there always is a message supplied (which means 3 digit 
status code, space and than at least 1 character.. The original code where 
there is a space added doesn't work / results in the exception.)

diff:
diff --git a/gluon/http.py b/gluon/http.py
index 69356b8..36fef89 100644
--- a/gluon/http.py
+++ b/gluon/http.py
@@ -55,6 +55,8 @@ try:
     BaseException
 except NameError:
     BaseException = Exception
+
+import re


 class HTTP(BaseException):
@@ -80,10 +82,20 @@ class HTTP(BaseException):
         env = env or {}
         status = self.status
         headers = self.headers
+
+        # Cast status to int
+        try:
+            status = int(status)
+        except ValueError:
+            pass
+
         if status in defined_status:
             status = '%d %s' % (status, defined_status[status])
-        else:
-            status = str(status) + ' '
+
+        # Make sure we have a status + message (3 ints then space followed 
by anything..)
+        if re.match("\d{3} [^ ]", status) == None:
+            status = str(status) + ' UNKNOWN ERROR'
+
         if not 'Content-Type' in headers:
             headers['Content-Type'] = 'text/html; charset=UTF-8'
         body = self.body



-- 



Reply via email to