def getPageContextForRequestContext(self, ctx): """Retrieve a resource from this site for a particular request. The resource will be wrapped in a PageContext which keeps track of how the resource was located. """ path = inevow.IRemainingSegments(ctx) res = inevow.IResource(self.resource) pageContext = context.PageContext(tag=res, parent=ctx) return defer.maybeDeferred(res.locateChild, pageContext, path).addCallback(self.handleSegment, ctx.tag, path, pageContext)
def _cbFinishRender(self, html, ctx): if isinstance(html, str): self.write(html) self.finishRequest(True) elif html is errorMarker: ## Error webpage has already been rendered and finish called pass else: res = inevow.IResource(html) pageContext = context.PageContext(tag=res, parent=ctx) return self.gotPageContext(pageContext) return html
def handleSegment(self, result, request, path, pageContext): if result is errorMarker: return errorMarker newres, newpath = result # If the child resource is None then display a 404 page if newres is None: from nevow.rend import FourOhFour return context.PageContext(tag=FourOhFour(), parent=pageContext) # If we got a deferred then we need to call back later, once the # child is actually available. if isinstance(newres, defer.Deferred): return newres.addCallback( lambda actualRes: self.handleSegment( (actualRes, newpath), request, path, pageContext)) # # FIX A GIANT LEAK. Is this code really useful anyway? # newres = inevow.IResource(newres)#, persist=True) if newres is pageContext.tag: assert not newpath is path, "URL traversal cycle detected when attempting to locateChild %r from resource %r." % (path, pageContext.tag) assert len(newpath) < len(path), "Infinite loop impending..." ## We found a Resource... update the request.prepath and postpath for x in xrange(len(path) - len(newpath)): if request.postpath: request.prepath.append(request.postpath.pop(0)) ## Create a context object to represent this new resource ctx = context.PageContext(tag=newres, parent=pageContext) ctx.remember(tuple(request.prepath), inevow.ICurrentSegments) ctx.remember(tuple(request.postpath), inevow.IRemainingSegments) res = newres path = newpath if not path: return ctx return defer.maybeDeferred( res.locateChild, ctx, path ).addErrback( processingFailed, request, ctx ).addCallback( self.handleSegment, request, path, ctx )
def _cbFinishRender(self, html, ctx): if isinstance(html, str): self.write(html) server.Request.finish(self) elif html is errorMarker: ## Error webpage has already been rendered and finish called pass else: res = inevow.IResource(html, None) if res is not None: pageContext = context.PageContext(tag=res, parent=ctx) return self.gotPageContext(pageContext) else: print "html is not a string: %s on %s" % (str(html), ctx.tag) server.Request.finish(self) return html
def locateChild(self, ctx, segments): r = self.children.get(segments[0], None) if r: return r, segments[1:] path = segments[0] self.fp.restat() if not self.fp.isdir(): return rend.NotFound if path: fpath = self.fp.child(path) else: fpath = self.fp.childSearchPreauth(*self.indexNames) if fpath is None: if self.allowListing: return self.directoryListing(), segments[1:] else: return rend.NotFound if not fpath.exists(): fpath = fpath.siblingExtensionSearch(*self.ignoredExts) if fpath is None: return rend.NotFound # Don't run processors on directories - if someone wants their own # customized directory rendering, subclass File instead. if fpath.isfile(): if platformType == "win32": # don't want .RPY to be different than .rpy, since that # would allow source disclosure. processor = InsensitiveDict(self.processors).get( fpath.splitext()[1]) else: processor = self.processors.get(fpath.splitext()[1]) if processor: return (inevow.IResource(processor(fpath.path, self.registry)), segments[1:]) return self.createSimilarFile(fpath.path), segments[1:]
def locateChild(self, ctx, segments): """Locate a child page of this one. ctx is a nevow.context.PageContext representing the parent Page, and segments is a tuple of each element in the URI. An tuple (page, segments) should be returned, where page is an instance of nevow.rend.Page and segments a tuple representing the remaining segments of the URI. If the child is not found, return NotFound instead. locateChild is designed to be easily overridden to perform fancy lookup tricks. However, the default locateChild is useful, and looks for children in three places, in this order: - in a dictionary, self.children - a member of self named child_<childname>. This can be either an attribute or a method. If an attribute, it should be an object which can be adapted to IResource. If a method, it should take the context and return an object which can be adapted to IResource. - by calling self.childFactory(ctx, name). Name is a single string instead of a tuple of strings. This should return an object that can be adapted to IResource. """ if self.children is not None: r = self.children.get(segments[0], None) if r is not None: return r, segments[1:] w = getattr(self, 'child_%s' % segments[0], None) if w is not None: if inevow.IResource(w, None) is not None: return w, segments[1:] r = w(ctx) if r is not None: return r, segments[1:] r = self.childFactory(ctx, segments[0]) if r is not None: return r, segments[1:] return FreeformChildMixin.locateChild(self, ctx, segments)
def _cbFinishRender(self, html, ctx): """ Callback for the page rendering process having completed. @param html: Either the content of the response body (L{bytes}) or a marker that an exception occurred and has already been handled or an object adaptable to L{IResource} to use to render the response. """ if self._lostConnection: # No response can be sent at this point. pass elif isinstance(html, str): self.write(html) self.finishRequest(True) elif html is errorMarker: ## Error webpage has already been rendered and finish called pass else: res = inevow.IResource(html) pageContext = context.PageContext(tag=res, parent=ctx) return self.gotPageContext(pageContext) return html
def renderResource(self, u): request = FakeRequest() ctx = context.RequestContext(tag=request) return util.maybeDeferred(inevow.IResource(u).renderHTTP, ctx).addCallback( lambda r: (r, request.redirected_to))
def _cb(xxx_todo_changeme1, ctx): (resource, segments) = xxx_todo_changeme1 assert not segments res = inevow.IResource(resource) return res.renderHTTP(ctx)