def get_resource(self, url, orig_req, log, retry_inner_if_not_200=False): """ Gets the resource at the given url, using the original request `orig_req` as the basis for constructing the subrequest. Returns a `webob.Response` object. We want to never retry_inner_if_not_200. """ retry_inner_if_not_200 = False return DeliveranceMiddleware.get_resource(self, url, orig_req, log, retry_inner_if_not_200)
def build_external_subrequest(self, url, orig_req, log): """ We need to carry through certain headers into external subrequests so that the downstream applications know what project context the request is within, what primary application the request is handled by, what domain the request is coming from; and login status must be retained """ subreq = DeliveranceMiddleware.build_external_subrequest( self, url, orig_req, log) for header in self._preserve_headers: value = orig_req.environ.get(header) if value is None: continue subreq.environ[header] = value return subreq
def build_external_subrequest(self, url, orig_req, log): """ We need to carry through certain headers into external subrequests so that the downstream applications know what project context the request is within, what primary application the request is handled by, what domain the request is coming from; and login status must be retained """ subreq = DeliveranceMiddleware.build_external_subrequest( self, url, orig_req, log) for header in self._preserve_headers: value = orig_req.environ.get(header) if value is None: continue subreq.environ[header] = value subreq.user_agent = "Deliverance" if subreq.host and subreq.host.endswith(":80"): subreq.host = subreq.host[:-3] return subreq
def create_deliverance_proxy(proxy_base_url, theme_html, rules_xml=None): '''Proxy to another url with re-theming using deliverance. Based on http://rufuspollock.org/code/deliverance :param proxy_base_url: base destination url we are proxying to. :param theme_html: string providing html theme to use for re-themeing. :param rules_xml: (optional) deliverance rules xml as a string. If not provided use `default_deliverance_rules`. For info on rulesets see deliverance docs. We require that ruleset support a single substitution string '%s' which is used to insert internal mountpoint for the them ('/_deliverance_theme.html'). ''' theme_url = '/_deliverance_theme.html' # use a urlmap so we can mount theme and urlset app = paste.urlmap.URLMap() # set up theme consistent with our rules file app[theme_url] = Response(theme_html) if rules_xml: rules = rules_xml else: rules = default_deliverance_rules rules = rules % theme_url app['/_deliverance_rules.xml'] = Response(rules, content_type="application/xml") class MyProxy(object): def __init__(self, proxy_base_url): self.proxy = paste.proxy.Proxy(proxy_base_url) def __call__(self, environ, start_response): req = Request(environ) res = req.get_response(self.proxy) res.decode_content() return res(environ, start_response) app['/'] = MyProxy(proxy_base_url) deliv = DeliveranceMiddleware( app, SubrequestRuleGetter('/_deliverance_rules.xml'), PrintingLogger, log_factory_kw=dict(print_level=logging.WARNING)) return deliv
def setup(): # Monkeypatch webtest TestRequest to inject our custom TestResponse # for now, hopefully subclass approach (demonstrated above to no effect) # will be merged to WebTest trunk (from bitbucket.org/ejucovy/webtest) TestRequest.ResponseClass = HtmlTestResponse global raw_app, rule_filename, deliv_filename, deliv_url app = URLMap() app['/theme.html'] = make_response(get_text("theme.html")) app['/blog/index.html'] = make_response(get_text("blog_index.html")) app['/about.html'] = make_response(get_text("about.html")) app['/magic'] = make_response(get_text("magic.html"), headerlist=[ ('Content-Type', "text/html"), ('X-No-Deliverate', "1")]) app['/magic2'] = make_response(get_text("magic2.html")) app['/foo'] = make_response(get_text("foo.html")) app['/empty'] = make_response("") app['/html_entities.html'] = make_response(get_text("html_entities.html")) app['/xhtml_doctype.html'] = make_response(get_text("xhtml_doctype.html")) app['/no_xhtml_doctype.html'] = make_response(get_text("no_xhtml_doctype.html")) app['/scriptcomments'] = make_response(get_text("scriptcomments.html")) app['/xhtml_scriptcomments'] = make_response(get_text("xhtml_scriptcomments.html")) app['/cdata.html'] = make_response(get_text("cdata.html")) app['/newfooter.html'] = make_response(get_text("newfooter.html")) app['/newfooter_sneaky_cdata.html'] = make_response(get_text("newfooter_sneaky_cdata.html")) app['/reddot.html'] = make_response(get_text("reddot.html")) app['/reddot2.html'] = make_response(get_text("reddot2.html")) app['/ellipse.html'] = make_response(get_text("ellipse.html")) app['/collapse_theme.html'] = make_response(get_text("collapse_theme.html")) app['/collapse_content.html'] = make_response(get_text("collapse_content.html")) app['/redirect_test/theme.html'] = make_redirect("/theme.html") app['/redirect_test/dynamic_topnav/logged_in.html'] = make_response( get_text("logged_in_topnav.html")) app['/redirect_test/dynamic_topnav'] = make_redirect( "/redirect_test/dynamic_topnav/logged_in.html") app['/redirect_test/dynamic_topnav'] = make_redirect( "/redirect_test/dynamic_topnav/logged_in.html") app['/redirect_test'] = make_redirect("/redirect_test/welcome.html") app['/redirect_test/welcome.html'] = make_response(get_text("welcome.html")) rule_xml = get_text("rule.xml") # Rule files can be read directly from the filesystem: rule_filename_pos, rule_filename = tempfile.mkstemp() f = open(rule_filename, 'w+') f.write(rule_xml) f.close() # Rule files can also be published and fetched with an HTTP subrequest: def read_rule_file(environ, start_response): f = open(rule_filename) content = f.read() f.close() return Response(content, content_type="application/xml")(environ, start_response) app['/mytheme/rules.xml'] = read_rule_file # We'll set up one DeliveranceMiddleware using the published rules, # and another using the rule file: deliv_filename = DeliveranceMiddleware( app, FileRuleGetter(rule_filename), PrintingLogger, log_factory_kw=dict(print_level=logging.WARNING)) deliv_url = DeliveranceMiddleware( app, SubrequestRuleGetter('http://localhost/mytheme/rules.xml'), PrintingLogger, log_factory_kw=dict(print_level=logging.WARNING)) raw_app = HtmlTestApp(app) deliv_filename = HtmlTestApp(deliv_filename) deliv_url = HtmlTestApp(deliv_url)
""" This sets up an example site """ import os import sys from paste.urlparser import StaticURLParser from paste.httpserver import serve from weberror.evalexception import EvalException from deliverance.middleware import DeliveranceMiddleware, FileRuleGetter from deliverance.security import SecurityContext base_path = os.path.join(os.path.dirname(__file__), 'example-files') app = StaticURLParser(base_path) rules_file = os.path.join(base_path, 'rules.xml') deliv_app = DeliveranceMiddleware(app, FileRuleGetter(rules_file)) full_app = SecurityContext.middleware(deliv_app, execute_pyref=True, display_logging=True, display_local_files=True, force_dev_auth=True) if __name__ == '__main__': try: port = sys.argv[1] except IndexError: port = '8080' host = '127.0.0.1' if ':' in port: host, port = port.split(':') print('See http://%s:%s/?deliv_log for the page with log messages' %
def notheme_request(self, req): if DeliveranceMiddleware.notheme_request(self, req): return True if req.headers.get("X-Requested-With") == "XMLHttpRequest": return True
def __init__(self, proxies, ruleset, source_location=None): self.proxies = proxies self.ruleset = ruleset self.source_location = source_location self.deliverator = DeliveranceMiddleware(self.proxy_app, self.rule_getter)