1.1 --- a/WebStack/Adapters/BaseHTTPRequestHandler.py Fri Jun 20 22:27:50 2008 +0200
1.2 +++ b/WebStack/Adapters/BaseHTTPRequestHandler.py Sat Jan 29 23:34:57 2005 +0000
1.3 @@ -12,36 +12,41 @@
1.4
1.5 "A factory class creating WebStack dispatcher objects."
1.6
1.7 - def __init__(self, resource, authenticator=None):
1.8 + def __init__(self, resource, authenticator=None, handle_errors=1):
1.9
1.10 """
1.11 Initialise the root application-specific 'resource' and optional
1.12 - 'authenticator'.
1.13 + 'authenticator'. The optional 'handle_errors' parameter (if true) causes
1.14 + handlers to deal with uncaught exceptions cleanly.
1.15 """
1.16
1.17 self.webstack_resource = resource
1.18 self.webstack_authenticator = authenticator
1.19 + self.handle_errors = handle_errors
1.20
1.21 def __call__(self, request, client_address, server):
1.22
1.23 "Act as a factory for the server objects."
1.24
1.25 handler = Handler(request, client_address, server, self.webstack_resource,
1.26 - self.webstack_authenticator)
1.27 + self.webstack_authenticator, self.handle_errors)
1.28 return handler
1.29
1.30 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
1.31
1.32 "A class dispatching requests to WebStack resources."
1.33
1.34 - def __init__(self, request, client_address, server, resource, authenticator):
1.35 + def __init__(self, request, client_address, server, resource, authenticator, handle_errors):
1.36
1.37 """
1.38 Initialise the root application-specific 'resource' and 'authenticator'.
1.39 + Where 'handle_errors' is true, uncaught exceptions are dealt with by the
1.40 + handler.
1.41 """
1.42
1.43 self.webstack_resource = resource
1.44 self.webstack_authenticator = authenticator
1.45 + self.handle_errors = handle_errors
1.46 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server)
1.47
1.48 def handle(self):
1.49 @@ -60,6 +65,11 @@
1.50 self.webstack_resource.respond(trans)
1.51 except EndOfResponse:
1.52 pass
1.53 + except:
1.54 + if self.handle_errors:
1.55 + trans.set_response_code(500) # Internal error
1.56 + else:
1.57 + raise
1.58 else:
1.59 trans.set_response_code(401) # Unauthorized
1.60 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
2.1 --- a/WebStack/Adapters/CGI.py Fri Jun 20 22:27:50 2008 +0200
2.2 +++ b/WebStack/Adapters/CGI.py Sat Jan 29 23:34:57 2005 +0000
2.3 @@ -8,7 +8,7 @@
2.4 from WebStack.Generic import EndOfResponse
2.5 import sys, os
2.6
2.7 -def respond(resource, authenticator=None, input=None, output=None, env=None):
2.8 +def respond(resource, authenticator=None, input=None, output=None, env=None, handle_errors=1):
2.9
2.10 """
2.11 Dispatch to the root application-specific 'resource'. Employ the optional
2.12 @@ -16,7 +16,8 @@
2.13 stream, 'output' stream or environment 'env' are specified, use them instead
2.14 of the defaults: standard input, standard output and the operating system
2.15 environment respectively. Note that 'env' must evaluate to true for it to
2.16 - replace the default.
2.17 + replace the default. The optional 'handle_errors' parameter (if true) causes
2.18 + handlers to deal with uncaught exceptions cleanly.
2.19 """
2.20
2.21 trans = WebStack.CGI.Transaction(input or sys.stdin, output or sys.stdout,
2.22 @@ -28,6 +29,11 @@
2.23 resource.respond(trans)
2.24 except EndOfResponse:
2.25 pass
2.26 + except:
2.27 + if self.handle_errors:
2.28 + trans.set_response_code(500) # Internal error
2.29 + else:
2.30 + raise
2.31 else:
2.32 trans.set_response_code(401) # Unauthorized
2.33 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
3.1 --- a/WebStack/Adapters/JavaServlet.py Fri Jun 20 22:27:50 2008 +0200
3.2 +++ b/WebStack/Adapters/JavaServlet.py Sat Jan 29 23:34:57 2005 +0000
3.3 @@ -11,15 +11,17 @@
3.4
3.5 "A servlet helper class dispatching requests to WebStack resources."
3.6
3.7 - def __init__(self, resource, authenticator=None):
3.8 + def __init__(self, resource, authenticator=None, handle_errors=1):
3.9
3.10 """
3.11 Initialise the root application-specific 'resource' and optional
3.12 - 'authenticator'.
3.13 + 'authenticator'. The optional 'handle_errors' parameter (if true)
3.14 + causes handlers to deal with uncaught exceptions cleanly.
3.15 """
3.16
3.17 self.webstack_resource = resource
3.18 self.webstack_authenticator = authenticator
3.19 + self.handle_errors = handle_errors
3.20
3.21 def service(self, request, response):
3.22
3.23 @@ -33,6 +35,11 @@
3.24 self.webstack_resource.respond(trans)
3.25 except EndOfResponse:
3.26 pass
3.27 + except:
3.28 + if self.handle_errors:
3.29 + trans.set_response_code(500) # Internal error
3.30 + else:
3.31 + raise
3.32 else:
3.33 trans.set_response_code(401) # Unauthorized
3.34 #trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
4.1 --- a/WebStack/Adapters/ModPython.py Fri Jun 20 22:27:50 2008 +0200
4.2 +++ b/WebStack/Adapters/ModPython.py Sat Jan 29 23:34:57 2005 +0000
4.3 @@ -8,12 +8,12 @@
4.4 from WebStack.Generic import EndOfResponse
4.5 from mod_python import apache
4.6
4.7 -def respond(request, resource, debug=0):
4.8 +def respond(request, resource, handle_errors=1):
4.9
4.10 """
4.11 Dispatch the given 'request' to the root application-specific 'resource'.
4.12 - The optional 'debug' flag, if set to 1, causes tracebacks to be displayed in
4.13 - the browser.
4.14 + The optional 'handle_errors' flag, if set to false, causes tracebacks to be
4.15 + displayed in the browser.
4.16 """
4.17
4.18 trans = WebStack.ModPython.Transaction(request)
4.19 @@ -32,17 +32,17 @@
4.20 # NOTE: Error conditions should be investigated further, along with
4.21 # NOTE: other response states.
4.22
4.23 - if not debug:
4.24 + if handle_errors:
4.25 return apache.HTTP_INTERNAL_SERVER_ERROR
4.26 else:
4.27 raise
4.28
4.29 -def authenticate(request, authenticator, debug=0):
4.30 +def authenticate(request, authenticator, handle_errors=1):
4.31
4.32 """
4.33 Dispatch the given 'request' to the application-specific 'authenticator'.
4.34 - The optional 'debug' flag, if set to 1, causes tracebacks to be displayed in
4.35 - the browser.
4.36 + The optional 'handle_errors' flag, if set to false, causes tracebacks to be
4.37 + displayed in the browser.
4.38 """
4.39
4.40 trans = WebStack.ModPython.Transaction(request)
4.41 @@ -60,7 +60,7 @@
4.42 # NOTE: Error conditions should be investigated further, along with
4.43 # NOTE: other response states.
4.44
4.45 - if not debug:
4.46 + if handle_errors:
4.47 return apache.HTTP_INTERNAL_SERVER_ERROR
4.48 else:
4.49 raise
5.1 --- a/WebStack/Adapters/Twisted.py Fri Jun 20 22:27:50 2008 +0200
5.2 +++ b/WebStack/Adapters/Twisted.py Sat Jan 29 23:34:57 2005 +0000
5.3 @@ -14,16 +14,18 @@
5.4
5.5 isLeaf = 1
5.6
5.7 - def __init__(self, resource, authenticator=None):
5.8 + def __init__(self, resource, authenticator=None, handle_errors=1):
5.9
5.10 """
5.11 Initialise the root application-specific 'resource' and optional
5.12 - 'authenticator'.
5.13 + 'authenticator'. The optional 'handle_errors' parameter (if true) causes
5.14 + handlers to deal with uncaught exceptions cleanly.
5.15 """
5.16
5.17 twisted.web.resource.Resource.__init__(self)
5.18 self.webstack_resource = resource
5.19 self.webstack_authenticator = authenticator
5.20 + self.handle_errors = handle_errors
5.21
5.22 def getChild(self, path, request):
5.23 raise NotImplementedError, "getChild"
5.24 @@ -38,6 +40,11 @@
5.25 self.webstack_resource.respond(trans)
5.26 except EndOfResponse:
5.27 pass
5.28 + except:
5.29 + if self.handle_errors:
5.30 + trans.set_response_code(500) # Internal error
5.31 + else:
5.32 + raise
5.33 else:
5.34 trans.set_response_code(401) # Unauthorized
5.35 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
6.1 --- a/WebStack/Adapters/WSGI.py Fri Jun 20 22:27:50 2008 +0200
6.2 +++ b/WebStack/Adapters/WSGI.py Sat Jan 29 23:34:57 2005 +0000
6.3 @@ -11,15 +11,17 @@
6.4
6.5 "A WSGI adapter class."
6.6
6.7 - def __init__(self, resource, authenticator=None):
6.8 + def __init__(self, resource, authenticator=None, handle_errors=1):
6.9
6.10 """
6.11 Initialise the adapter with the given WebStack 'resource' and the
6.12 - optional 'authenticator'.
6.13 + optional 'authenticator'. The optional 'handle_errors' parameter (if
6.14 + true) causes handlers to deal with uncaught exceptions cleanly.
6.15 """
6.16
6.17 self.resource = resource
6.18 self.authenticator = authenticator
6.19 + self.handle_errors = handle_errors
6.20
6.21 def __call__(self, environ, start_response):
6.22
6.23 @@ -43,6 +45,11 @@
6.24 self.resource.respond(trans)
6.25 except EndOfResponse:
6.26 pass
6.27 + except:
6.28 + if self.handle_errors:
6.29 + trans.set_response_code(500) # Internal error
6.30 + else:
6.31 + raise
6.32 else:
6.33 trans.set_response_code(401) # Unauthorized
6.34 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
7.1 --- a/WebStack/Adapters/Webware.py Fri Jun 20 22:27:50 2008 +0200
7.2 +++ b/WebStack/Adapters/Webware.py Sat Jan 29 23:34:57 2005 +0000
7.3 @@ -29,15 +29,18 @@
7.4 instantiation.
7.5 """
7.6
7.7 - def __init__(self, resource, authenticator=None):
7.8 + def __init__(self, resource, authenticator=None, handle_errors=1):
7.9
7.10 """
7.11 Initialise the parser object with the given root application-specific
7.12 - 'resource' and optional 'authenticator'.
7.13 + 'resource' and optional 'authenticator'. The optional 'handle_errors'
7.14 + parameter (if true) causes handlers to deal with uncaught exceptions
7.15 + cleanly.
7.16 """
7.17
7.18 self.webstack_resource = resource
7.19 self.webstack_authenticator = authenticator
7.20 + self.handle_errors = handle_errors
7.21
7.22 def parse(self, trans, requestPath):
7.23
7.24 @@ -52,7 +55,8 @@
7.25 should be available elsewhere.
7.26 """
7.27
7.28 - return WebStackServlet(self.webstack_resource, self.webstack_authenticator)
7.29 + return WebStackServlet(self.webstack_resource, self.webstack_authenticator,
7.30 + self.handle_errors)
7.31
7.32 # For Webware 0.8.1 and earlier, employ servlet factories and servlets.
7.33
7.34 @@ -66,19 +70,22 @@
7.35 application-specific resources.
7.36 """
7.37
7.38 - def __init__(self, application, resource, file_extensions, authenticator=None):
7.39 + def __init__(self, application, resource, file_extensions, authenticator=None, handle_errors=1):
7.40
7.41 """
7.42 Initialise the servlet factory with the Webware 'application' and the
7.43 WebStack root application-specific 'resource'. The 'file_extensions'
7.44 specified indicate for which files this factory is invoked. An optional
7.45 - 'authenticator' is used to control access to the resource.
7.46 + 'authenticator' is used to control access to the resource. The optional
7.47 + 'handle_errors' parameter (if true) causes handlers to deal with
7.48 + uncaught exceptions cleanly.
7.49 """
7.50
7.51 ServletFactory.__init__(self, application)
7.52 self.webstack_resource = resource
7.53 self.file_extensions = file_extensions
7.54 self.webstack_authenticator = authenticator
7.55 + self.handle_errors = handle_errors
7.56
7.57 def uniqueness(self):
7.58
7.59 @@ -105,7 +112,8 @@
7.60 method is invoked on the servlet.
7.61 """
7.62
7.63 - return WebStackServlet(self.webstack_resource, self.webstack_authenticator)
7.64 + return WebStackServlet(self.webstack_resource, self.webstack_authenticator,
7.65 + self.handle_errors)
7.66
7.67 # Servlets are common to both solutions.
7.68
7.69 @@ -113,16 +121,18 @@
7.70
7.71 "A servlet which dispatches transactions to application-specific resources."
7.72
7.73 - def __init__(self, resource, authenticator):
7.74 + def __init__(self, resource, authenticator, handle_errors):
7.75
7.76 """
7.77 Initialise the servlet with an application-specific 'resource' and
7.78 - 'authenticator'.
7.79 + 'authenticator'. Where 'handle_errors' is true, uncaught exceptions are
7.80 + dealt with by the handler.
7.81 """
7.82
7.83 Servlet.__init__(self)
7.84 self.webstack_resource = resource
7.85 self.webstack_authenticator = authenticator
7.86 + self.handle_errors = handle_errors
7.87
7.88 def respond(self, trans):
7.89
7.90 @@ -137,6 +147,11 @@
7.91 self.webstack_resource.respond(new_trans)
7.92 except EndOfResponse:
7.93 pass
7.94 + except:
7.95 + if self.handle_errors:
7.96 + new_trans.set_response_code(500) # Internal error
7.97 + else:
7.98 + raise
7.99 else:
7.100 new_trans.set_response_code(401) # Unauthorized
7.101 new_trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
8.1 --- a/WebStack/Adapters/Zope.py Fri Jun 20 22:27:50 2008 +0200
8.2 +++ b/WebStack/Adapters/Zope.py Sat Jan 29 23:34:57 2005 +0000
8.3 @@ -21,16 +21,18 @@
8.4 security.declareObjectProtected("View")
8.5 security.declareProtected("View", "index_html")
8.6
8.7 - def __init__(self, id, resource, authenticator=None):
8.8 + def __init__(self, id, resource, authenticator=None, handle_errors=1):
8.9
8.10 """
8.11 Initialise with an 'id', a WebStack 'resource', and an optional
8.12 - 'authenticator'.
8.13 + 'authenticator'. The optional 'handle_errors' parameter (if true) causes
8.14 + handlers to deal with uncaught exceptions cleanly.
8.15 """
8.16
8.17 self.id = id
8.18 self.webstack_resource = resource
8.19 self.webstack_authenticator = authenticator
8.20 + self.handle_errors = handle_errors
8.21
8.22 def __bobo_traverse__(self, request, entry_name):
8.23 if entry_name == "index_html":
8.24 @@ -56,6 +58,11 @@
8.25 self.webstack_resource.respond(trans)
8.26 except EndOfResponse:
8.27 pass
8.28 + except:
8.29 + if self.handle_errors:
8.30 + trans.set_response_code(500) # Internal error
8.31 + else:
8.32 + raise
8.33 else:
8.34 #trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
8.35 # self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm()))