# -*- test-case-name: twisted.test.test_web -*- # Twisted, the Framework of Your Internet # Copyright (C) 2001 Matthew W. Lefkowitz # # This library is free software; you can redistribute it and/or # modify it under the terms of version 2.1 of the GNU Lesser General Public # License as published by the Free Software Foundation. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from cStringIO import StringIO from twisted.python import failure import html import resource import linecache import string, re import types def redirectTo(URL, request): request.redirect(URL) return """
click here """ % {'url': URL} class Redirect(resource.Resource): isLeaf = 1 def __init__(self, url): resource.Resource.__init__(self) self.url = url def render(self, request): return redirectTo(self.url, request) def getChild(self, name, request): return self class ChildRedirector(Redirect): isLeaf = 0 def __init__(self, url): # XXX is this enough? if ((url.find('://') == -1) and (not url.startswith('..')) and (not url.startswith('/'))): raise ValueError("It seems you've given me a redirect (%s) that is a child of myself! That's not good, it'll cause an infinite redirect." % url) Redirect.__init__(self, url) def getChild(self, name, request): newUrl = self.url if not newUrl.endswith('/'): newUrl += '/' newUrl += name return ChildRedirector(newUrl) from twisted.python import urlpath class ParentRedirect(resource.Resource): """ I redirect to URLPath.here(). """ isLeaf = 1 def render(self, request): return redirectTo(urlpath.URLPath.fromRequest(request).here(), request) def getChild(self, request): return self class DeferredResource(resource.Resource): """ I wrap up a Deferred that will eventually result in a Resource object. """ isLeaf = 1 def __init__(self, d): resource.Resource.__init__(self) self.d = d def getChild(self, name, request): return self def render(self, request): self.d.addCallback(self._cbChild, request).addErrback( self._ebChild,request) from twisted.web.server import NOT_DONE_YET return NOT_DONE_YET def _cbChild(self, child, request): result = resource.getChildForRequest(child, request).render(request) from twisted.web.server import NOT_DONE_YET if result == NOT_DONE_YET: return else: request.write(result) request.finish() def _ebChild(self, reason, request): request.processingFailed(reason) return reason stylesheet = """ """ def htmlrepr(x): return htmlReprTypes.get(type(x), htmlUnknown)(x) def saferepr(x): try: rx = repr(x) except: rx = "'+html.escape(saferepr(x))+'
'
def htmlDict(d):
io = StringIO()
w = io.write
w('%s | %s |
%s: %s
""" frameHTML = """