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"' % (