paulb@4 | 1 | #!/usr/bin/env python |
paulb@4 | 2 | |
paulb@4 | 3 | """ |
paulb@4 | 4 | Twisted adapter. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@4 | 21 | """ |
paulb@4 | 22 | |
paulb@4 | 23 | import WebStack.Twisted |
paulb@297 | 24 | from WebStack.Generic import EndOfResponse |
paulb@6 | 25 | import twisted.web.resource |
paulb@321 | 26 | import twisted.web.server |
paulb@321 | 27 | import twisted.internet.reactor |
paulb@4 | 28 | |
paulb@6 | 29 | class Dispatcher(twisted.web.resource.Resource): |
paulb@6 | 30 | |
paulb@6 | 31 | "A class dispatching requests to WebStack resources." |
paulb@6 | 32 | |
paulb@6 | 33 | isLeaf = 1 |
paulb@6 | 34 | |
paulb@310 | 35 | def __init__(self, resource, authenticator=None, handle_errors=1): |
paulb@6 | 36 | |
paulb@76 | 37 | """ |
paulb@76 | 38 | Initialise the root application-specific 'resource' and optional |
paulb@310 | 39 | 'authenticator'. The optional 'handle_errors' parameter (if true) causes |
paulb@310 | 40 | handlers to deal with uncaught exceptions cleanly. |
paulb@76 | 41 | """ |
paulb@6 | 42 | |
paulb@6 | 43 | twisted.web.resource.Resource.__init__(self) |
paulb@6 | 44 | self.webstack_resource = resource |
paulb@76 | 45 | self.webstack_authenticator = authenticator |
paulb@310 | 46 | self.handle_errors = handle_errors |
paulb@6 | 47 | |
paulb@6 | 48 | def getChild(self, path, request): |
paulb@6 | 49 | raise NotImplementedError, "getChild" |
paulb@6 | 50 | |
paulb@6 | 51 | def render(self, request): |
paulb@6 | 52 | |
paulb@6 | 53 | "Dispatch the given 'request' to the root application-specific resource." |
paulb@6 | 54 | |
paulb@6 | 55 | trans = WebStack.Twisted.Transaction(request) |
paulb@510 | 56 | try: |
paulb@510 | 57 | if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans): |
paulb@510 | 58 | try: |
paulb@510 | 59 | self.webstack_resource.respond(trans) |
paulb@510 | 60 | except EndOfResponse: |
paulb@510 | 61 | pass |
paulb@510 | 62 | except: |
paulb@510 | 63 | if self.handle_errors: |
paulb@510 | 64 | trans.set_response_code(500) # Internal error |
paulb@510 | 65 | else: |
paulb@510 | 66 | raise |
paulb@510 | 67 | else: |
paulb@510 | 68 | trans.set_response_code(401) # Unauthorized |
paulb@510 | 69 | trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@510 | 70 | self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm())) |
paulb@76 | 71 | |
paulb@510 | 72 | finally: |
paulb@510 | 73 | trans.commit() |
paulb@518 | 74 | |
paulb@6 | 75 | request.finish() |
paulb@6 | 76 | return twisted.web.server.NOT_DONE_YET |
paulb@4 | 77 | |
paulb@321 | 78 | default_address = ("", 8080) |
paulb@321 | 79 | |
paulb@339 | 80 | def deploy(resource, authenticator=None, address=None, handle_errors=1): |
paulb@321 | 81 | |
paulb@321 | 82 | """ |
paulb@321 | 83 | Deploy the given 'resource', with the given optional 'authenticator', at the |
paulb@321 | 84 | given optional 'address', where 'address' is a 2-tuple of the form |
paulb@321 | 85 | (host_string, port_integer). |
paulb@321 | 86 | |
paulb@321 | 87 | NOTE: Twisted only makes use of the port number provided in the 'address'. |
paulb@339 | 88 | |
paulb@339 | 89 | The optional 'handle_errors' flag (true by default) specifies whether error |
paulb@339 | 90 | conditions are handled gracefully. |
paulb@321 | 91 | """ |
paulb@321 | 92 | |
paulb@321 | 93 | address = address or default_address |
paulb@321 | 94 | |
paulb@339 | 95 | top_level = Dispatcher(resource, authenticator, handle_errors) |
paulb@321 | 96 | site = twisted.web.server.Site(top_level) |
paulb@321 | 97 | twisted.internet.reactor.listenTCP(address[1], site) |
paulb@321 | 98 | twisted.internet.reactor.run() |
paulb@321 | 99 | |
paulb@4 | 100 | # vim: tabstop=4 expandtab shiftwidth=4 |