From d0685d42ffefd87291c3419d9bc3b33a9940554c Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Mon, 19 May 2008 15:19:25 -0700 Subject: [PATCH] web: factor out identical renderHTTP methods --- src/allmydata/web/common.py | 17 +++++++++++++++ src/allmydata/web/directory.py | 20 ++--------------- src/allmydata/web/filenode.py | 39 +++------------------------------- src/allmydata/web/root.py | 19 ++--------------- 4 files changed, 24 insertions(+), 71 deletions(-) diff --git a/src/allmydata/web/common.py b/src/allmydata/web/common.py index 10599269..a2e4b9e8 100644 --- a/src/allmydata/web/common.py +++ b/src/allmydata/web/common.py @@ -125,3 +125,20 @@ class MyExceptionHandler(appserver.DefaultExceptionHandler): http.NOT_IMPLEMENTED) super = appserver.DefaultExceptionHandler return super.renderHTTP_exception(self, ctx, f) + +class RenderMixin: + + def renderHTTP(self, ctx): + request = IRequest(ctx) + + # if we were using regular twisted.web Resources (and the regular + # twisted.web.server.Request object) then we could implement + # render_PUT and render_GET. But Nevow's request handler + # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy + # some code from the Resource.render method that Nevow bypasses, to + # do the same thing. + m = getattr(self, 'render_' + request.method, None) + if not m: + from twisted.web.server import UnsupportedMethod + raise UnsupportedMethod(getattr(self, 'allowedMethods', ())) + return m(ctx) diff --git a/src/allmydata/web/directory.py b/src/allmydata/web/directory.py index a16af78a..8397541b 100644 --- a/src/allmydata/web/directory.py +++ b/src/allmydata/web/directory.py @@ -18,7 +18,7 @@ from allmydata.interfaces import IDirectoryNode, IFileNode, IMutableFileNode, \ ExistingChildError from allmydata.web.common import text_plain, WebError, IClient, \ boolean_of_arg, get_arg, should_create_intermediate_directories, \ - getxmlfile + getxmlfile, RenderMixin from allmydata.web.filenode import ReplaceMeMixin, \ FileNodeHandler, PlaceHolderNodeHandler @@ -38,7 +38,7 @@ def make_handler_for(node, parentnode=None, name=None): return DirectoryNodeHandler(node, parentnode, name) raise WebError("Cannot provide handler for '%s'" % node) -class DirectoryNodeHandler(rend.Page, ReplaceMeMixin): +class DirectoryNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin): addSlash = True def __init__(self, node, parentnode=None, name=None): @@ -111,22 +111,6 @@ class DirectoryNodeHandler(rend.Page, ReplaceMeMixin): if DEBUG: print "good child" return make_handler_for(node, self.node, name) - def renderHTTP(self, ctx): - # This is where all of the ?t=* actions are implemented. - request = IRequest(ctx) - - # if we were using regular twisted.web Resources (and the regular - # twisted.web.server.Request object) then we could implement - # render_PUT and render_GET. But Nevow's request handler - # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy - # some code from the Resource.render method that Nevow bypasses, to - # do the same thing. - m = getattr(self, 'render_' + request.method, None) - if not m: - from twisted.web.server import UnsupportedMethod - raise UnsupportedMethod(getattr(self, 'allowedMethods', ())) - return m(ctx) - def render_DELETE(self, ctx): assert self.parentnode and self.name d = self.parentnode.delete(self.name) diff --git a/src/allmydata/web/filenode.py b/src/allmydata/web/filenode.py index c17bc52f..608dc19e 100644 --- a/src/allmydata/web/filenode.py +++ b/src/allmydata/web/filenode.py @@ -13,7 +13,7 @@ from allmydata.interfaces import IDownloadTarget, ExistingChildError from allmydata.mutable.common import MODE_READ from allmydata.util import log -from allmydata.web.common import text_plain, WebError, IClient, \ +from allmydata.web.common import text_plain, WebError, IClient, RenderMixin, \ boolean_of_arg, get_arg, should_create_intermediate_directories class ReplaceMeMixin: @@ -79,7 +79,7 @@ class ReplaceMeMixin: d.addCallback(lambda newnode: newnode.get_uri()) return d -class PlaceHolderNodeHandler(rend.Page, ReplaceMeMixin): +class PlaceHolderNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin): def __init__(self, parentnode, name): rend.Page.__init__(self) assert parentnode @@ -97,22 +97,6 @@ class PlaceHolderNodeHandler(rend.Page, ReplaceMeMixin): % name, http.CONFLICT) - def renderHTTP(self, ctx): - # This is where all of the ?t=* actions are implemented. - request = IRequest(ctx) - - # if we were using regular twisted.web Resources (and the regular - # twisted.web.server.Request object) then we could implement - # render_PUT and render_GET. But Nevow's request handler - # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy - # some code from the Resource.render method that Nevow bypasses, to - # do the same thing. - m = getattr(self, 'render_' + request.method, None) - if not m: - from twisted.web.server import UnsupportedMethod - raise UnsupportedMethod(getattr(self, 'allowedMethods', ())) - return m(ctx) - def render_PUT(self, ctx): req = IRequest(ctx) t = get_arg(req, "t", "").strip() @@ -149,7 +133,7 @@ class PlaceHolderNodeHandler(rend.Page, ReplaceMeMixin): return d -class FileNodeHandler(rend.Page, ReplaceMeMixin): +class FileNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin): def __init__(self, node, parentnode=None, name=None): rend.Page.__init__(self) assert node @@ -165,23 +149,6 @@ class FileNodeHandler(rend.Page, ReplaceMeMixin): raise WebError("Files have no children, certainly not named '%s'" % name) - - def renderHTTP(self, ctx): - # This is where all of the ?t=* actions are implemented. - request = IRequest(ctx) - - # if we were using regular twisted.web Resources (and the regular - # twisted.web.server.Request object) then we could implement - # render_PUT and render_GET. But Nevow's request handler - # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy - # some code from the Resource.render method that Nevow bypasses, to - # do the same thing. - m = getattr(self, 'render_' + request.method, None) - if not m: - from twisted.web.server import UnsupportedMethod - raise UnsupportedMethod(getattr(self, 'allowedMethods', ())) - return m(ctx) - def render_GET(self, ctx): req = IRequest(ctx) t = get_arg(req, "t", "").strip() diff --git a/src/allmydata/web/root.py b/src/allmydata/web/root.py index 51a96307..9e752b85 100644 --- a/src/allmydata/web/root.py +++ b/src/allmydata/web/root.py @@ -16,29 +16,14 @@ from allmydata.util import idlib from allmydata.interfaces import IFileNode from allmydata.web import filenode, directory, unlinked, status from allmydata.web.common import abbreviate_size, IClient, getxmlfile, \ - WebError, get_arg + WebError, get_arg, RenderMixin -class URIHandler(rend.Page): +class URIHandler(RenderMixin, rend.Page): # I live at /uri . There are several operations defined on /uri itself, # mostly involed with creation of unlinked files and directories. - def renderHTTP(self, ctx): - request = IRequest(ctx) - - # if we were using regular twisted.web Resources (and the regular - # twisted.web.server.Request object) then we could implement - # render_PUT and render_GET. But Nevow's request handler - # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy - # some code from the Resource.render method that Nevow bypasses, to - # do the same thing. - m = getattr(self, 'render_' + request.method, None) - if not m: - from twisted.web.server import UnsupportedMethod - raise UnsupportedMethod(getattr(self, 'allowedMethods', ())) - return m(ctx) - def render_GET(self, ctx): req = IRequest(ctx) uri = get_arg(req, "uri", None) -- 2.45.2