paulb@4 | 1 | #!/usr/bin/env python |
paulb@4 | 2 | |
paulb@4 | 3 | """ |
paulb@4 | 4 | Twisted adapter. |
paulb@4 | 5 | """ |
paulb@4 | 6 | |
paulb@4 | 7 | import WebStack.Twisted |
paulb@297 | 8 | from WebStack.Generic import EndOfResponse |
paulb@6 | 9 | import twisted.web.resource |
paulb@321 | 10 | import twisted.web.server |
paulb@321 | 11 | import twisted.internet.reactor |
paulb@4 | 12 | |
paulb@6 | 13 | class Dispatcher(twisted.web.resource.Resource): |
paulb@6 | 14 | |
paulb@6 | 15 | "A class dispatching requests to WebStack resources." |
paulb@6 | 16 | |
paulb@6 | 17 | isLeaf = 1 |
paulb@6 | 18 | |
paulb@310 | 19 | def __init__(self, resource, authenticator=None, handle_errors=1): |
paulb@6 | 20 | |
paulb@76 | 21 | """ |
paulb@76 | 22 | Initialise the root application-specific 'resource' and optional |
paulb@310 | 23 | 'authenticator'. The optional 'handle_errors' parameter (if true) causes |
paulb@310 | 24 | handlers to deal with uncaught exceptions cleanly. |
paulb@76 | 25 | """ |
paulb@6 | 26 | |
paulb@6 | 27 | twisted.web.resource.Resource.__init__(self) |
paulb@6 | 28 | self.webstack_resource = resource |
paulb@76 | 29 | self.webstack_authenticator = authenticator |
paulb@310 | 30 | self.handle_errors = handle_errors |
paulb@6 | 31 | |
paulb@6 | 32 | def getChild(self, path, request): |
paulb@6 | 33 | raise NotImplementedError, "getChild" |
paulb@6 | 34 | |
paulb@6 | 35 | def render(self, request): |
paulb@6 | 36 | |
paulb@6 | 37 | "Dispatch the given 'request' to the root application-specific resource." |
paulb@6 | 38 | |
paulb@6 | 39 | trans = WebStack.Twisted.Transaction(request) |
paulb@76 | 40 | if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans): |
paulb@297 | 41 | try: |
paulb@297 | 42 | self.webstack_resource.respond(trans) |
paulb@297 | 43 | except EndOfResponse: |
paulb@297 | 44 | pass |
paulb@310 | 45 | except: |
paulb@310 | 46 | if self.handle_errors: |
paulb@310 | 47 | trans.set_response_code(500) # Internal error |
paulb@310 | 48 | else: |
paulb@310 | 49 | raise |
paulb@76 | 50 | else: |
paulb@76 | 51 | trans.set_response_code(401) # Unauthorized |
paulb@76 | 52 | trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@76 | 53 | self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm())) |
paulb@76 | 54 | |
paulb@29 | 55 | trans.commit() |
paulb@6 | 56 | request.finish() |
paulb@6 | 57 | return twisted.web.server.NOT_DONE_YET |
paulb@4 | 58 | |
paulb@321 | 59 | default_address = ("", 8080) |
paulb@321 | 60 | |
paulb@339 | 61 | def deploy(resource, authenticator=None, address=None, handle_errors=1): |
paulb@321 | 62 | |
paulb@321 | 63 | """ |
paulb@321 | 64 | Deploy the given 'resource', with the given optional 'authenticator', at the |
paulb@321 | 65 | given optional 'address', where 'address' is a 2-tuple of the form |
paulb@321 | 66 | (host_string, port_integer). |
paulb@321 | 67 | |
paulb@321 | 68 | NOTE: Twisted only makes use of the port number provided in the 'address'. |
paulb@339 | 69 | |
paulb@339 | 70 | The optional 'handle_errors' flag (true by default) specifies whether error |
paulb@339 | 71 | conditions are handled gracefully. |
paulb@321 | 72 | """ |
paulb@321 | 73 | |
paulb@321 | 74 | address = address or default_address |
paulb@321 | 75 | |
paulb@339 | 76 | top_level = Dispatcher(resource, authenticator, handle_errors) |
paulb@321 | 77 | site = twisted.web.server.Site(top_level) |
paulb@321 | 78 | twisted.internet.reactor.listenTCP(address[1], site) |
paulb@321 | 79 | twisted.internet.reactor.run() |
paulb@321 | 80 | |
paulb@4 | 81 | # vim: tabstop=4 expandtab shiftwidth=4 |