def resolve_href(self, req, resp, log): """Figure out the theme URL given a request and response. This calls the pyref, or does URI template substitution on an href attribute""" substitute = True if self.pyref: if not execute_pyref(req): log.error( self, "Security disallows executing pyref %s" % self.pyref) ## FIXME: this isn't very good; fatal exception?: href = self.href else: href = self.pyref(req, resp, log) substitute = False else: href = self.href if substitute: vars = NestedDict(req.environ, req.headers, dict(here=posixpath.dirname(self.source_location))) new_href = uri_template_substitute(href, vars) if new_href != href: log.debug( self, 'Rewrote theme href="%s" to "%s"' % (href, new_href)) href = new_href ## FIXME: is this join a good idea? if href: href = urlparse.urljoin(req.url, href) return href
def resolve_href(self, req, resp, log): """Figure out the theme URL given a request and response. This calls the pyref, or does URI template substitution on an href attribute""" substitute = True if self.pyref: if not execute_pyref(req): log.error( self, "Security disallows executing pyref %s" % self.pyref) ## FIXME: this isn't very good; fatal exception?: href = self.href else: href = self.pyref(req, resp, log) substitute = False else: href = self.href if substitute: vars = NestedDict(req.environ, req.headers, dict(here=posixpath.dirname(self.source_location))) new_href = uri_template_substitute(href, vars) if new_href != href: log.debug( self, 'Rewrote theme href="%s" to "%s"' % (href, new_href)) href = new_href ## FIXME: is this join a good idea? if href: href = urllib.parse.urljoin(req.url, href) return href
def parse_xml(cls, el, source_location): """Parse this document from an XML/etree element""" assert el.tag == 'proxy' match = ProxyMatch.parse_xml(el, source_location) dest = None request_modifications = [] response_modifications = [] strip_script_name = True keep_host = False editable = asbool(el.get('editable')) for child in el: if child.tag == 'dest': if dest is not None: raise DeliveranceSyntaxError( "You cannot have more than one <dest> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) dest = ProxyDest.parse_xml(child, source_location) elif child.tag == 'transform': if child.get('strip-script-name'): strip_script_name = asbool(child.get('strip-script-name')) if child.get('keep-host'): keep_host = asbool(child.get('keep-host')) ## FIXME: error on other attrs elif child.tag == 'request': request_modifications.append( ProxyRequestModification.parse_xml(child, source_location)) elif child.tag == 'response': response_modifications.append( ProxyResponseModification.parse_xml(child, source_location)) elif child.tag is Comment: continue else: raise DeliveranceSyntaxError( "Unknown tag in <proxy>: %s" % xml_tostring(child), element=child, source_location=source_location) if editable: if not dest: ## FIXME: should this always be a test? raise DeliveranceSyntaxError("You must have a <dest> tag", element=el, source_location=source_location) try: href = uri_template_substitute( dest.href, dict(here=posixpath.dirname(source_location))) except KeyError: raise DeliveranceSyntaxError( 'You can only use <proxy editable="1"> if you have a <dest href="..."> that only contains {here} (you have %s)' % (dest.href)) if not href.startswith('file:'): raise DeliveranceSyntaxError( 'You can only use <proxy editable="1"> if you have a <dest href="file:///..."> (you have %s)' % (dest)) classes = el.get('class', '').split() or None inst = cls(match, dest, request_modifications, response_modifications, strip_script_name=strip_script_name, keep_host=keep_host, source_location=source_location, classes=classes, editable=editable) match.proxy = inst return inst
def __call__(self, request, log): """Determine the destination given the request""" assert not self.next if self.pyref: if not execute_pyref(request): log.error(self, "Security disallows executing pyref %s" % self.pyref) else: return self.pyref(request, log) ## FIXME: is this nesting really needed? ## we could just use HTTP_header keys... vars = NestedDict(request.environ, request.headers, dict(here=posixpath.dirname(self.source_location))) return uri_template_substitute(self.href, vars)
def __call__(self, request, log): """Determine the destination given the request""" assert not self.__next__ if self.pyref: if not execute_pyref(request): log.error(self, "Security disallows executing pyref %s" % self.pyref) else: return self.pyref(request, log) ## FIXME: is this nesting really needed? ## we could just use HTTP_header keys... vars = NestedDict(request.environ, request.headers, dict(here=posixpath.dirname(self.source_location))) return uri_template_substitute(self.href, vars)
def edit_app(self, environ, start_response): try: if not self.editable: raise exc.HTTPForbidden('This proxy is not editable="1"') if not edit_local_files(environ): raise exc.HTTPForbidden("Editing is forbidden") try: dest_href = uri_template_substitute(self.dest.href, dict(here=posixpath.dirname(self.source_location))) except KeyError: raise exc.HTTPForbidden("Not a static location: %s" % self.dest.href) if not dest_href.startswith("file:/"): raise exc.HTTPForbidden("Not local: %s" % self.dest.href) filename = url_to_filename(dest_href) editor = Editor(base_dir=filename) return editor(environ, start_response) except exc.HTTPException, e: return e(environ, start_response)
def edit_app(self, environ, start_response): try: if not self.editable: raise exc.HTTPForbidden('This proxy is not editable="1"') if not edit_local_files(environ): raise exc.HTTPForbidden('Editing is forbidden') try: dest_href = uri_template_substitute( self.dest.href, dict(here=posixpath.dirname(self.source_location))) except KeyError: raise exc.HTTPForbidden('Not a static location: %s' % self.dest.href) if not dest_href.startswith('file:/'): raise exc.HTTPForbidden('Not local: %s' % self.dest.href) filename = url_to_filename(dest_href) editor = Editor(base_dir=filename) return editor(environ, start_response) except exc.HTTPException, e: return e(environ, start_response)
def parse_xml(cls, el, source_location): """Parse this document from an XML/etree element""" assert el.tag == 'proxy' match = ProxyMatch.parse_xml(el, source_location) dest = None wsgi = None request_modifications = [] response_modifications = [] strip_script_name = True keep_host = False editable = asbool(el.get('editable')) rewriting_links = None ## FIXME: this inline validation is a bit brittle because it is ## order-dependent, but validation errors generally aren't for child in el: if child.tag == 'dest': if dest is not None: raise DeliveranceSyntaxError( "You cannot have more than one <dest> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) if wsgi is not None: raise DeliveranceSyntaxError( "You cannot have both a <dest> tag and a <wsgi> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) dest = ProxyDest.parse_xml(child, source_location) elif child.tag == 'wsgi': if wsgi is not None: raise DeliveranceSyntaxError( "You cannot have more than one <wsgi> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) if dest is not None: raise DeliveranceSyntaxError( "You cannot have both a <dest> tag and a <wsgi> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) if rewriting_links is not None: raise DeliveranceSyntaxError( "You cannot use ``<response rewrite-links='1'>`` in a proxy with a ``<wsgi>`` tag", element=child, source_location=source_location) wsgi = ProxyWsgi.parse_xml(child, source_location) elif child.tag == 'transform': if child.get('strip-script-name'): strip_script_name = asbool(child.get('strip-script-name')) if child.get('keep-host'): keep_host = asbool(child.get('keep-host')) ## FIXME: error on other attrs elif child.tag == 'request': request_modifications.append( ProxyRequestModification.parse_xml(child, source_location)) elif child.tag == 'response': mod = ProxyResponseModification.parse_xml( child, source_location) if mod.rewrite_links == True: rewriting_links = mod if wsgi is not None: raise DeliveranceSyntaxError( "You cannot use ``<response rewrite-links='1'>`` in a proxy with a ``<wsgi>`` tag", element=child, source_location=source_location) response_modifications.append(mod) elif child.tag is Comment: continue else: raise DeliveranceSyntaxError("Unknown tag in <proxy>: %s" % xml_tostring(child), element=child, source_location=source_location) if editable: if not dest: ## FIXME: should this always be a test? raise DeliveranceSyntaxError("You must have a <dest> tag", element=el, source_location=source_location) try: href = uri_template_substitute( dest.href, dict(here=posixpath.dirname(source_location))) except KeyError: raise DeliveranceSyntaxError( 'You can only use <proxy editable="1"> if you have a <dest href="..."> that only contains {here} (you have %s)' % (dest.href)) if not href.startswith('file:'): raise DeliveranceSyntaxError( 'You can only use <proxy editable="1"> if you have a <dest href="file:///..."> (you have %s)' % (dest)) classes = el.get('class', '').split() or None inst = cls(match, dest, request_modifications, response_modifications, strip_script_name=strip_script_name, keep_host=keep_host, source_location=source_location, classes=classes, editable=editable, wsgi=wsgi) match.proxy = inst return inst
def parse_xml(cls, el, source_location): """Parse this document from an XML/etree element""" assert el.tag == 'proxy' match = ProxyMatch.parse_xml(el, source_location) dest = None wsgi = None request_modifications = [] response_modifications = [] strip_script_name = True keep_host = False editable = asbool(el.get('editable')) rewriting_links = None ## FIXME: this inline validation is a bit brittle because it is ## order-dependent, but validation errors generally aren't for child in el: if child.tag == 'dest': if dest is not None: raise DeliveranceSyntaxError( "You cannot have more than one <dest> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) if wsgi is not None: raise DeliveranceSyntaxError( "You cannot have both a <dest> tag and a <wsgi> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) dest = ProxyDest.parse_xml(child, source_location) elif child.tag == 'wsgi': if wsgi is not None: raise DeliveranceSyntaxError( "You cannot have more than one <wsgi> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) if dest is not None: raise DeliveranceSyntaxError( "You cannot have both a <dest> tag and a <wsgi> tag (second tag: %s)" % xml_tostring(child), element=child, source_location=source_location) if rewriting_links is not None: raise DeliveranceSyntaxError( "You cannot use ``<response rewrite-links='1'>`` in a proxy with a ``<wsgi>`` tag", element=child, source_location=source_location) wsgi = ProxyWsgi.parse_xml(child, source_location) elif child.tag == 'transform': if child.get('strip-script-name'): strip_script_name = asbool(child.get('strip-script-name')) if child.get('keep-host'): keep_host = asbool(child.get('keep-host')) ## FIXME: error on other attrs elif child.tag == 'request': request_modifications.append( ProxyRequestModification.parse_xml(child, source_location)) elif child.tag == 'response': mod = ProxyResponseModification.parse_xml(child, source_location) if mod.rewrite_links == True: rewriting_links = mod if wsgi is not None: raise DeliveranceSyntaxError( "You cannot use ``<response rewrite-links='1'>`` in a proxy with a ``<wsgi>`` tag", element=child, source_location=source_location) response_modifications.append(mod) elif child.tag is Comment: continue else: raise DeliveranceSyntaxError( "Unknown tag in <proxy>: %s" % xml_tostring(child), element=child, source_location=source_location) if editable: if not dest: ## FIXME: should this always be a test? raise DeliveranceSyntaxError("You must have a <dest> tag", element=el, source_location=source_location) try: href = uri_template_substitute( dest.href, dict(here=posixpath.dirname(source_location))) except KeyError: raise DeliveranceSyntaxError( 'You can only use <proxy editable="1"> if you have a <dest href="..."> that only contains {here} (you have %s)' % (dest.href)) if not href.startswith('file:'): raise DeliveranceSyntaxError( 'You can only use <proxy editable="1"> if you have a <dest href="file:///..."> (you have %s)' % (dest)) classes = el.get('class', '').split() or None inst = cls(match, dest, request_modifications, response_modifications, strip_script_name=strip_script_name, keep_host=keep_host, source_location=source_location, classes=classes, editable=editable, wsgi=wsgi) match.proxy = inst return inst