1.1 --- a/WebStack/Adapters/BaseHTTPRequestHandler.py Thu Nov 16 23:40:10 2006 +0000
1.2 +++ b/WebStack/Adapters/BaseHTTPRequestHandler.py Thu Nov 16 23:40:31 2006 +0000
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 BaseHTTPRequestHandler adapter.
1.6
1.7 -Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This library is free software; you can redistribute it and/or
1.11 modify it under the terms of the GNU Lesser General Public
1.12 @@ -23,46 +23,51 @@
1.13 import WebStack.BaseHTTPRequestHandler
1.14 import BaseHTTPServer
1.15 from WebStack.Generic import EndOfResponse
1.16 +from WebStack.Adapters.Helpers.Error import ErrorResource
1.17
1.18 class HandlerFactory:
1.19
1.20 "A factory class creating WebStack dispatcher objects."
1.21
1.22 - def __init__(self, resource, authenticator=None, handle_errors=1):
1.23 + def __init__(self, resource, authenticator=None, handle_errors=1, error_resource=None):
1.24
1.25 """
1.26 Initialise the root application-specific 'resource' and optional
1.27 'authenticator'. The optional 'handle_errors' parameter (if true) causes
1.28 - handlers to deal with uncaught exceptions cleanly.
1.29 + handlers to deal with uncaught exceptions cleanly, and the optional
1.30 + 'error_resource' specifies an alternative error message generation
1.31 + resource.
1.32 """
1.33
1.34 self.webstack_resource = resource
1.35 self.webstack_authenticator = authenticator
1.36 self.handle_errors = handle_errors
1.37 + self.error_resource = error_resource or ErrorResource()
1.38
1.39 def __call__(self, request, client_address, server):
1.40
1.41 "Act as a factory for the server objects."
1.42
1.43 handler = Handler(request, client_address, server, self.webstack_resource,
1.44 - self.webstack_authenticator, self.handle_errors)
1.45 + self.webstack_authenticator, self.handle_errors, self.error_resource)
1.46 return handler
1.47
1.48 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
1.49
1.50 "A class dispatching requests to WebStack resources."
1.51
1.52 - def __init__(self, request, client_address, server, resource, authenticator, handle_errors):
1.53 + def __init__(self, request, client_address, server, resource, authenticator, handle_errors, error_resource):
1.54
1.55 """
1.56 Initialise the root application-specific 'resource' and 'authenticator'.
1.57 Where 'handle_errors' is true, uncaught exceptions are dealt with by the
1.58 - handler.
1.59 + handler and reported using the 'error_resource' provided.
1.60 """
1.61
1.62 self.webstack_resource = resource
1.63 self.webstack_authenticator = authenticator
1.64 self.handle_errors = handle_errors
1.65 + self.error_resource = error_resource
1.66 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server)
1.67
1.68 def handle(self):
1.69 @@ -84,7 +89,9 @@
1.70 pass
1.71 except:
1.72 if self.handle_errors:
1.73 + trans.rollback()
1.74 trans.set_response_code(500) # Internal error
1.75 + self.error_resource.respond(trans)
1.76 else:
1.77 raise
1.78 else:
1.79 @@ -97,7 +104,7 @@
1.80
1.81 default_address = ("", 8080)
1.82
1.83 -def deploy(resource, authenticator=None, address=None, handle_errors=1):
1.84 +def deploy(resource, authenticator=None, address=None, handle_errors=1, error_resource=None):
1.85
1.86 """
1.87 Deploy the given 'resource', with the given optional 'authenticator', at the
1.88 @@ -105,10 +112,11 @@
1.89 (host_string, port_integer).
1.90
1.91 The optional 'handle_errors' flag (true by default) specifies whether error
1.92 - conditions are handled gracefully.
1.93 + conditions are handled gracefully, and the optional 'error_resource'
1.94 + specifies an alternative error message generation resource, if desired.
1.95 """
1.96
1.97 - handler = HandlerFactory(resource, authenticator, handle_errors)
1.98 + handler = HandlerFactory(resource, authenticator, handle_errors, error_resource)
1.99 server = BaseHTTPServer.HTTPServer(address or default_address, handler)
1.100 server.serve_forever()
1.101
2.1 --- a/WebStack/Adapters/CGI.py Thu Nov 16 23:40:10 2006 +0000
2.2 +++ b/WebStack/Adapters/CGI.py Thu Nov 16 23:40:31 2006 +0000
2.3 @@ -23,9 +23,10 @@
2.4 import WebStack.CGI
2.5 from WebStack.Generic import EndOfResponse
2.6 import sys, os
2.7 +from WebStack.Adapters.Helpers.Error import ErrorResource
2.8
2.9 def deploy(resource, authenticator=None, input=None, output=None, env=None,
2.10 - address=None, handle_errors=1):
2.11 + address=None, handle_errors=1, error_resource=None):
2.12
2.13 """
2.14 Dispatch to the root application-specific 'resource'. Employ the optional
2.15 @@ -34,11 +35,15 @@
2.16 of the defaults: standard input, standard output and the operating system
2.17 environment respectively. Note that 'env' must evaluate to true for it to
2.18 replace the default. The optional 'handle_errors' parameter (if true) causes
2.19 - handlers to deal with uncaught exceptions cleanly.
2.20 + handlers to deal with uncaught exceptions cleanly, and the optional
2.21 + 'error_resource' specifies an alternative error message generation resource,
2.22 + if desired.
2.23
2.24 The optional 'address' parameter is deliberately ignored.
2.25 """
2.26
2.27 + error_resource = error_resource or ErrorResource()
2.28 +
2.29 trans = WebStack.CGI.Transaction(input or sys.stdin, output or sys.stdout,
2.30 env or os.environ)
2.31
2.32 @@ -50,7 +55,9 @@
2.33 pass
2.34 except:
2.35 if handle_errors:
2.36 + trans.rollback()
2.37 trans.set_response_code(500) # Internal error
2.38 + error_resource.respond(trans)
2.39 else:
2.40 raise
2.41 else:
3.1 --- a/WebStack/Adapters/Django.py Thu Nov 16 23:40:10 2006 +0000
3.2 +++ b/WebStack/Adapters/Django.py Thu Nov 16 23:40:31 2006 +0000
3.3 @@ -22,23 +22,27 @@
3.4
3.5 import WebStack.Django
3.6 from WebStack.Generic import EndOfResponse
3.7 +from WebStack.Adapters.Helpers.Error import ErrorResource
3.8
3.9 -def deploy(resource, authenticator=None, address=None, handle_errors=1):
3.10 +def deploy(resource, authenticator=None, address=None, handle_errors=1, error_resource=None):
3.11
3.12 """
3.13 Deploy the given 'resource', employing the optional 'authenticator' to
3.14 protect it. The optional 'handle_errors' parameter (if true) causes handlers
3.15 - to deal with uncaught exceptions cleanly.
3.16 + to deal with uncaught exceptions cleanly, and the optional 'error_resource'
3.17 + specifies an alternative error message generation resource, if desired.
3.18
3.19 The optional 'address' parameter is deliberately ignored.
3.20 """
3.21
3.22 def _deploy(request, *args, **kw):
3.23 return respond(request, resource, authenticator=authenticator,
3.24 - virtual_path_info=kw.get("vp"), handle_errors=handle_errors)
3.25 + virtual_path_info=kw.get("vp"), handle_errors=handle_errors,
3.26 + error_resource=error_resource)
3.27 return _deploy
3.28
3.29 -def respond(request, resource, authenticator=None, virtual_path_info=None, handle_errors=1):
3.30 +def respond(request, resource, authenticator=None, virtual_path_info=None, handle_errors=1,
3.31 + error_resource=None):
3.32
3.33 """
3.34 Dispatch to the root application-specific 'resource'. Employ the optional
3.35 @@ -47,9 +51,12 @@
3.36 given 'resource'.
3.37
3.38 The optional 'handle_errors' parameter (if true) causes handlers to deal
3.39 - with uncaught exceptions cleanly.
3.40 + with uncaught exceptions cleanly, and the optional 'error_resource'
3.41 + specifies an alternative error message generation resource.
3.42 """
3.43
3.44 + error_resource = error_resource or ErrorResource()
3.45 +
3.46 trans = WebStack.Django.Transaction(request)
3.47 if virtual_path_info:
3.48 trans.set_virtual_path_info(virtual_path_info)
3.49 @@ -62,7 +69,9 @@
3.50 pass
3.51 except Exception, exc:
3.52 if handle_errors:
3.53 + trans.rollback()
3.54 trans.set_response_code(500) # Internal error
3.55 + error_resource.respond(trans)
3.56 else:
3.57 raise
3.58 else:
4.1 --- a/WebStack/Adapters/WSGI.py Thu Nov 16 23:40:10 2006 +0000
4.2 +++ b/WebStack/Adapters/WSGI.py Thu Nov 16 23:40:31 2006 +0000
4.3 @@ -3,7 +3,7 @@
4.4 """
4.5 WSGI adapter.
4.6
4.7 -Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk>
4.8 +Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk>
4.9
4.10 This library is free software; you can redistribute it and/or
4.11 modify it under the terms of the GNU Lesser General Public
4.12 @@ -23,22 +23,26 @@
4.13 import WebStack.WSGI
4.14 from WebStack.Generic import EndOfResponse
4.15 from WebStack.Adapters.Helpers.wsgi_cgi import run_with_cgi
4.16 +from WebStack.Adapters.Helpers.Error import ErrorResource
4.17
4.18 class WSGIAdapter:
4.19
4.20 "A WSGI adapter class."
4.21
4.22 - def __init__(self, resource, authenticator=None, handle_errors=1):
4.23 + def __init__(self, resource, authenticator=None, handle_errors=1, error_resource=None):
4.24
4.25 """
4.26 Initialise the adapter with the given WebStack 'resource' and the
4.27 optional 'authenticator'. The optional 'handle_errors' parameter (if
4.28 - true) causes handlers to deal with uncaught exceptions cleanly.
4.29 + true) causes handlers to deal with uncaught exceptions cleanly, and the
4.30 + optional 'error_resource' specifies an alternative error message
4.31 + generation resource.
4.32 """
4.33
4.34 self.resource = resource
4.35 self.authenticator = authenticator
4.36 self.handle_errors = handle_errors
4.37 + self.error_resource = error_resource or ErrorResource()
4.38
4.39 def __call__(self, environ, start_response):
4.40
4.41 @@ -64,7 +68,9 @@
4.42 pass
4.43 except:
4.44 if self.handle_errors:
4.45 + trans.rollback()
4.46 trans.set_response_code(500) # Internal error
4.47 + self.error_resource.respond(trans)
4.48 else:
4.49 raise
4.50 else:
4.51 @@ -83,7 +89,7 @@
4.52 )
4.53 return [trans.get_wsgi_content()]
4.54
4.55 -def deploy(resource, authenticator=None, address=None, handle_errors=1):
4.56 +def deploy(resource, authenticator=None, address=None, handle_errors=1, error_resource=None):
4.57
4.58 """
4.59 Deploy the given 'resource', with the given optional 'authenticator', at the
4.60 @@ -93,10 +99,11 @@
4.61 NOTE: The 'address' is ignored with the current WSGI implementation.
4.62
4.63 The optional 'handle_errors' flag (true by default) specifies whether error
4.64 - conditions are handled gracefully.
4.65 + conditions are handled gracefully, and the optional 'error_resource'
4.66 + specifies an alternative error message generation resource, if desired.
4.67 """
4.68
4.69 - handler = WSGIAdapter(resource, authenticator, handle_errors)
4.70 + handler = WSGIAdapter(resource, authenticator, handle_errors, error_resource)
4.71 run_with_cgi(handler)
4.72
4.73 # vim: tabstop=4 expandtab shiftwidth=4