My solution to appending slashes to resources
(I didn't reply to my original message because the mailing list never sent it to me. Yet, I can see it at the archives here http://twistedmatrix.com/pipermail/twisted-web/2009-October/004334.html - is anyone getting these messages?) A bit of metaprogramming to the rescue. For those Resource classes that you want to append slashes automatically, this seems to work. Can anyone see any problems with this? def append_slashes(rsc_class): original_getChild = rsc_class.getChild original_render = rsc_class.render def slash_appending_getChild(self, path, request): if path == '': return self else: return original_getChild(self, path, request) def slash_appending_render(self, request): if request.prepath[-1] != '': request.redirect(request.childLink("")) return "" else: return original_render(self, request) rsc_class.getChild = slash_appending_getChild rsc_class.render = slash_appending_render rsc_class.isLeaf = False return rsc_class # test code # entering http://localhost:8080/hello should redirect to http://localhost:8080/hello/ from twisted.internet import reactor from twisted.python import log from twisted.web import server, resource, static import sys class MyPage(resource.Resource): def render_GET(self, request): return "<html><body><p>Hello World</p></body></html>" append_slashes(MyPage) log.startLogging(sys.stdout) root = resource.Resource() root.putChild("hello", MyPage()) site = server.Site(root) reactor.listenTCP(8080, site) reactor.run() -- Mark Wright markscottwright@gmail.com
Mark Wright wrote:
(I didn't reply to my original message because the mailing list never sent it to me. Yet, I can see it at the archives here http://twistedmatrix.com/pipermail/twisted-web/2009-October/004334.html - is anyone getting these messages?)
Yes. Mailman doesn't send the mail to the sender by default, hence you not getting a copy.
A bit of metaprogramming to the rescue. For those Resource classes that you want to append slashes automatically, this seems to work. Can anyone see any problems with this?
It's... an awful lot of effort, but hey - if trailing slashes are that important to you, knock yourself out ;o)
participants (2)
-
Mark Wright -
Phil Mayers