1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 from twisted.web import resource
7 class RewriterResource(resource.Resource):
9 def __init__(self, orig, *rewriteRules):
10 resource.Resource.__init__(self)
12 self.rewriteRules = list(rewriteRules)
14 def _rewrite(self, request):
15 for rewriteRule in self.rewriteRules:
18 def getChild(self, path, request):
19 request.postpath.insert(0, path)
21 self._rewrite(request)
22 path = request.postpath.pop(0)
23 request.prepath.append(path)
24 return self.resource.getChildWithDefault(path, request)
26 def render(self, request):
27 self._rewrite(request)
28 return self.resource.render(request)
31 def tildeToUsers(request):
32 if request.postpath and request.postpath[0][:1]=='~':
33 request.postpath[:1] = ['users', request.postpath[0][1:]]
34 request.path = '/'+'/'.join(request.prepath+request.postpath)
36 def alias(aliasPath, sourcePath):
38 I am not a very good aliaser. But I'm the best I can be. If I'm
39 aliasing to a Resource that generates links, and it uses any parts
40 of request.prepath to do so, the links will not be relative to the
41 aliased path, but rather to the aliased-to path. That I can't
42 alias static.File directory listings that nicely. However, I can
43 still be useful, as many resources will play nice.
45 sourcePath = sourcePath.split('/')
46 aliasPath = aliasPath.split('/')
47 def rewriter(request):
48 if request.postpath[:len(aliasPath)] == aliasPath:
49 after = request.postpath[len(aliasPath):]
50 request.postpath = sourcePath + after
51 request.path = '/'+'/'.join(request.prepath+request.postpath)