def buildResourceTree(resources): if IResource.providedBy(resources): return resources logger.debug("build resorce tree...") if isinstance(resources, collections.Mapping): res_list = [] _flatten_url_dict(res_list, resources, "") else: res_list = list(resources) res_list.sort() roots = [v for k, v in res_list if not k] assert len(roots) <= 1 logger.debug("resource tree is %r", res_list) if roots: root, = roots else: root = _EmptyResource() res_list = sorted((k, v) for k, v in res_list if k) for fpath, res in res_list: logger.debug("path %r, resource %r", fpath, res) if fpath.startswith("/"): fpath = fpath[1:] else: raise AssertionError("expected leading '/'") _put_subresource(root, fpath.split("/"), res) return root
def test_wrapResourceWeb(self): if not getTwistedWeb(): raise SkipTest("This test requires both twisted.web.") from twisted.web.resource import IResource, Resource root = Resource() wrapped = wrapResource(root, [self.checker]) self.assertTrue(IResource.providedBy(wrapped))
def test_adaptation(self): """ Test that the Collection class can be adapted to an IResource. """ collection = base.Collection() self.assertTrue(ICollection.providedBy(collection)) self.assertTrue(IResource.providedBy(IResource(collection)))
def process(r): if IResource.providedBy(r): return request.render(getChildForRequest(r, request)) if IRenderable.providedBy(r): return flattenString(request, r).addCallback(process) return r
def process(r): if IResource.providedBy(r): request.render(getChildForRequest(r, request)) return _StandInResource if IRenderable.providedBy(r): return flattenString(request, r).addCallback(process) return r
def static_resource(hierarchy): root = Resource() for k, v in hierarchy.iteritems(): if IResource.providedBy(v): root.putChild(k, v) elif isinstance(v, dict): root.putChild(k, static_resource(v)) else: raise NotImplementedError(v) return root
def _flatten_url_dict(acc, d, prefix): if IResource.providedBy(d) or isinstance(d, basestring): acc.append((prefix, d)) else: for k, v in dict(d).items(): if k is None: pp = prefix else: pp = "%s/%s" % (prefix, k) _flatten_url_dict(acc, v, pp)
def process(r): if IResource.providedBy(r): return request.render(getChildForRequest(r, request)) if IRenderable.providedBy(r): return flattenString(request, r).addCallback(process) if isinstance(r, unicode): r = r.encode('utf-8') if r is not None: request.write(r) request.finish()
def process(r): """ Recursively go through r and any child Resources until something returns an IRenderable, then render it and let the result of that bubble back up. """ if IResource.providedBy(r): request.render(getChildForRequest(r, request)) return _StandInResource if IRenderable.providedBy(r): return renderElement(request, r) return r
def _getRealResource(self, resource): proxied = False for b in resource.__class__.__bases__: if b.__name__ == '(Proxy for twisted.web.resource.IResource)': proxied = True if not proxied: return resource # find the real resource (or the next proxy) for x in vars(resource).values(): if IResource.providedBy(x): return self._getRealResource(x) raise Exception("Unable to find real resource")
def test_service_with_region_internal(self): """ Validate that an external service does not provide an internal service resource. """ iapi = make_example_internal_api(self) helper = APIMockHelper(self, [iapi]) core = helper.core service_id = None for a_service_id in core._uuid_to_api_internal: if core._uuid_to_api_internal[a_service_id] == iapi: service_id = a_service_id resource = core.service_with_region(u"ORD", service_id, u"http://some/random/prefix") self.assertTrue(IResource.providedBy(resource))
def process(r): if IResource.providedBy(r): while (request.postpath and request.postpath != request._klein_postpath_): request.prepath.append(request.postpath.pop(0)) return request.render(getChildForRequest(r, request)) if IRenderable.providedBy(r): return flattenString(request, r).addCallback(process) if isinstance(r, unicode): r = r.encode('utf-8') if r is not None: request.write(r) request.finish()
def process(r: object) -> Any: """ Recursively go through r and any child Resources until something returns an IRenderable, then render it and let the result of that bubble back up. """ if isinstance(r, Response): r = r._applyToRequest(request) if IResource.providedBy(r): request.render(getChildForRequest(r, request)) return StandInResource if IRenderable.providedBy(r): renderElement(request, r) return StandInResource return r
def create_factory(authnz, git_configuration, git_viewer=None): if git_viewer is None: git_viewer = NoResource() elif not IResource.providedBy(git_viewer): raise ValueError("git_viewer should implement IResource") credentialFactories = [BasicCredentialFactory('Git Repositories')] gitportal = Portal(GitHTTPRealm(authnz, git_configuration, credentialFactories, git_viewer)) if hasattr(authnz, 'check_password'): log.msg("Registering PasswordChecker") gitportal.registerChecker(PasswordChecker(authnz.check_password)) gitportal.registerChecker(AllowAnonymousAccess()) resource = HTTPAuthSessionWrapper(gitportal, credentialFactories) site = Site(resource) return site
def create_factory(authnz, git_configuration, git_viewer=None): if git_viewer is None: git_viewer = NoResource() elif not IResource.providedBy(git_viewer): raise ValueError("git_viewer should implement IResource") credentialFactories = [BasicCredentialFactory('Git Repositories')] gitportal = Portal( GitHTTPRealm(authnz, git_configuration, credentialFactories, git_viewer)) if hasattr(authnz, 'check_password'): log.msg("Registering PasswordChecker") gitportal.registerChecker(PasswordChecker(authnz.check_password)) gitportal.registerChecker(AllowAnonymousAccess()) resource = HTTPAuthSessionWrapper(gitportal, credentialFactories) site = Site(resource) return site
def test_service_with_region_internal(self): """ Validate that an external service does not provide an internal service resource. """ iapi = make_example_internal_api(self) helper = APIMockHelper(self, [iapi]) core = helper.core service_id = None for a_service_id in core._uuid_to_api_internal: if core._uuid_to_api_internal[a_service_id] == iapi: service_id = a_service_id resource = core.service_with_region( u"ORD", service_id, u"http://some/random/prefix" ) self.assertTrue( IResource.providedBy(resource) )
def _listener_http(self, config, listener_config): port = listener_config["port"] bind_addresses = listener_config["bind_addresses"] tls = listener_config.get("tls", False) site_tag = listener_config.get("tag", port) resources = {} for res in listener_config["resources"]: for name in res["names"]: if name == "openid" and "federation" in res["names"]: # Skip loading openid resource if federation is defined # since federation resource will include openid continue resources.update( self._configure_named_resource(name, res.get("compress", False)) ) additional_resources = listener_config.get("additional_resources", {}) logger.debug("Configuring additional resources: %r", additional_resources) module_api = ModuleApi(self, self.get_auth_handler()) for path, resmodule in additional_resources.items(): handler_cls, config = load_module(resmodule) handler = handler_cls(config, module_api) if IResource.providedBy(handler): resource = handler elif hasattr(handler, "handle_request"): resource = AdditionalResource(self, handler.handle_request) else: raise ConfigError( "additional_resource %s does not implement a known interface" % (resmodule["module"],) ) resources[path] = resource # try to find something useful to redirect '/' to if WEB_CLIENT_PREFIX in resources: root_resource = RootOptionsRedirectResource(WEB_CLIENT_PREFIX) elif STATIC_PREFIX in resources: root_resource = RootOptionsRedirectResource(STATIC_PREFIX) else: root_resource = OptionsResource() root_resource = create_resource_tree(resources, root_resource) if tls: ports = listen_ssl( bind_addresses, port, SynapseSite( "synapse.access.https.%s" % (site_tag,), site_tag, listener_config, root_resource, self.version_string, ), self.tls_server_context_factory, reactor=self.get_reactor(), ) logger.info("Synapse now listening on TCP port %d (TLS)", port) else: ports = listen_tcp( bind_addresses, port, SynapseSite( "synapse.access.http.%s" % (site_tag,), site_tag, listener_config, root_resource, self.version_string, ), reactor=self.get_reactor(), ) logger.info("Synapse now listening on TCP port %d", port) return ports
def test_wrapResourceWeb(self): from twisted.web.resource import IResource, Resource root = Resource() wrapped = wrapResource(root, [self.checker]) self.assertTrue(IResource.providedBy(wrapped))
def _finish(result, render, request): """ Try to finish rendering the response to a request. This implements extra convenience functionality not provided by Twisted Web. Various resources in Tahoe-LAFS made use of this functionality when it was provided by Nevow. Rather than making that application code do the more tedious thing itself, we duplicate the functionality here. :param result: Something returned by a render method which we can turn into a response. :param render: The original render method which produced the result. :param request: The request being responded to. :return: ``None`` """ if isinstance(result, Failure): if result.check(CancelledError): return Message.log( message_type=u"allmydata:web:common-render:failure", message=result.getErrorMessage(), ) _finish( _renderHTTP_exception(request, result), render, request, ) elif IResource.providedBy(result): # If result is also using @render_exception then we don't want to # double-apply the logic. This leads to an attempt to double-finish # the request. If it isn't using @render_exception then you should # fix it so it is. Message.log( message_type=u"allmydata:web:common-render:resource", resource=fullyQualifiedName(type(result)), ) result.render(request) elif isinstance(result, str): Message.log(message_type=u"allmydata:web:common-render:unicode", ) request.write(result.encode("utf-8")) request.finish() elif isinstance(result, bytes): Message.log(message_type=u"allmydata:web:common-render:bytes", ) request.write(result) request.finish() elif isinstance(result, DecodedURL): Message.log(message_type=u"allmydata:web:common-render:DecodedURL", ) _finish(redirectTo(result.to_text().encode("utf-8"), request), render, request) elif result is None: Message.log(message_type=u"allmydata:web:common-render:None", ) request.finish() elif result == NOT_DONE_YET: Message.log(message_type=u"allmydata:web:common-render:NOT_DONE_YET", ) pass else: Message.log(message_type=u"allmydata:web:common-render:unknown", ) log.err( "Request for {!r} handled by {!r} returned unusable {!r}".format( request.uri, fullyQualifiedName(render), result, )) request.setResponseCode(http.INTERNAL_SERVER_ERROR) _finish(b"Internal Server Error", render, request)
def _listener_http(self, config: HomeServerConfig, listener_config: ListenerConfig): port = listener_config.port bind_addresses = listener_config.bind_addresses tls = listener_config.tls site_tag = listener_config.http_options.tag if site_tag is None: site_tag = str(port) # We always include a health resource. resources = {"/health": HealthResource()} for res in listener_config.http_options.resources: for name in res.names: if name == "openid" and "federation" in res.names: # Skip loading openid resource if federation is defined # since federation resource will include openid continue resources.update( self._configure_named_resource(name, res.compress)) additional_resources = listener_config.http_options.additional_resources logger.debug("Configuring additional resources: %r", additional_resources) module_api = self.get_module_api() for path, resmodule in additional_resources.items(): handler_cls, config = load_module( resmodule, ("listeners", site_tag, "additional_resources", "<%s>" % (path, )), ) handler = handler_cls(config, module_api) if IResource.providedBy(handler): resource = handler elif hasattr(handler, "handle_request"): resource = AdditionalResource(self, handler.handle_request) else: raise ConfigError( "additional_resource %s does not implement a known interface" % (resmodule["module"], )) resources[path] = resource # try to find something useful to redirect '/' to if WEB_CLIENT_PREFIX in resources: root_resource = RootOptionsRedirectResource(WEB_CLIENT_PREFIX) elif STATIC_PREFIX in resources: root_resource = RootOptionsRedirectResource(STATIC_PREFIX) else: root_resource = OptionsResource() root_resource = create_resource_tree(resources, root_resource) if tls: ports = listen_ssl( bind_addresses, port, SynapseSite( "synapse.access.https.%s" % (site_tag, ), site_tag, listener_config, root_resource, self.version_string, ), self.tls_server_context_factory, reactor=self.get_reactor(), ) logger.info("Synapse now listening on TCP port %d (TLS)", port) else: ports = listen_tcp( bind_addresses, port, SynapseSite( "synapse.access.http.%s" % (site_tag, ), site_tag, listener_config, root_resource, self.version_string, ), reactor=self.get_reactor(), ) logger.info("Synapse now listening on TCP port %d", port) return ports