Ejemplo n.º 1
0
 def parse_match_xml(cls, el, source_location):
     """
     Parses out the match-related arguments
     """
     path = cls._parse_attr(el, 'path', default='path')
     domain = cls._parse_attr(el, 'domain', default='wildcard-insensitive')
     request_header = cls._parse_attr(el, 'request-header', default='exact', 
                                      header=True)
     response_header = cls._parse_attr(el, 'response-header', default='exact', 
                                       header=True)
     response_status = cls._parse_attr(el, 'response-status', default='exact')
     environ = cls._parse_attr(el, 'environ', default='exact', header=True)
     pyref = PyReference.parse_xml(
         el, source_location=source_location,
         default_function='match_request',
         default_objs=dict(AbortTheme=AbortTheme))
     return dict(
         path=path,
         domain=domain,
         request_header=request_header,
         response_header=response_header,
         response_status=response_status,
         environ=environ,
         pyref=pyref,
         source_location=source_location)
Ejemplo n.º 2
0
 def parse_match_xml(cls, el, source_location):
     """
     Parses out the match-related arguments
     """
     path = cls._parse_attr(el, 'path', default='path')
     domain = cls._parse_attr(el, 'domain', default='wildcard-insensitive')
     request_header = cls._parse_attr(el,
                                      'request-header',
                                      default='exact',
                                      header=True)
     response_header = cls._parse_attr(el,
                                       'response-header',
                                       default='exact',
                                       header=True)
     response_status = cls._parse_attr(el,
                                       'response-status',
                                       default='exact')
     environ = cls._parse_attr(el, 'environ', default='exact', header=True)
     pyref = PyReference.parse_xml(el,
                                   source_location=source_location,
                                   default_function='match_request',
                                   default_objs=dict(AbortTheme=AbortTheme))
     return dict(path=path,
                 domain=domain,
                 request_header=request_header,
                 response_header=response_header,
                 response_status=response_status,
                 environ=environ,
                 pyref=pyref,
                 source_location=source_location)
Ejemplo n.º 3
0
 def parse_xml(cls, el, source_location):
     """Parse an instance from an etree XML element"""
     assert el.tag == 'theme'
     href = el.get('href')
     pyref = PyReference.parse_xml(el, source_location, default_function='get_theme',
                                   default_objs=dict(AbortTheme=AbortTheme))
     if not pyref and not href:
         ## FIXME: also warn when pyref and href?
         raise DeliveranceSyntaxError(
             'You must provide at least one of href, pymodule, or the '
             'pyfile attribute', element=el)
     return cls(href=href, pyref=pyref,
                source_location=source_location)
Ejemplo n.º 4
0
 def parse_xml(cls, el, source_location):
     """Parse an instance from an etree XML element"""
     assert el.tag == 'theme'
     href = el.get('href')
     pyref = PyReference.parse_xml(el, source_location, default_function='get_theme',
                                   default_objs=dict(AbortTheme=AbortTheme))
     if not pyref and not href:
         ## FIXME: also warn when pyref and href?
         raise DeliveranceSyntaxError(
             'You must provide at least one of href, pymodule, or the '
             'pyfile attribute', element=el)
     return cls(href=href, pyref=pyref,
                source_location=source_location)
Ejemplo n.º 5
0
 def parse_xml(cls, el, source_location):
     """Parse an instance from an etree XML element"""
     href = el.get('href')
     pyref = PyReference.parse_xml(
         el, source_location, 
         default_function='get_proxy_dest', default_objs=dict(AbortProxy=AbortProxy))
     next = asbool(el.get('next'))
     if next and (href or pyref):
         raise DeliveranceSyntaxError(
             'If you have a next="1" attribute you cannot also have an href '
             'or pyref attribute',
             element=el, source_location=source_location)
     return cls(href, pyref, next=next, source_location=source_location)
Ejemplo n.º 6
0
 def parse_xml(cls, el, source_location):
     """Parse an instance from an etree XML element"""
     href = el.get('href')
     pyref = PyReference.parse_xml(
         el, source_location, 
         default_function='get_proxy_dest', default_objs=dict(AbortProxy=AbortProxy))
     next = asbool(el.get('next'))
     if next and (href or pyref):
         raise DeliveranceSyntaxError(
             'If you have a next="1" attribute you cannot also have an href '
             'or pyref attribute',
             element=el, source_location=source_location)
     return cls(href, pyref, next=next, source_location=source_location)
Ejemplo n.º 7
0
 def parse_xml(cls, el, source_location):
     """Parse an instance from an etree XML element"""
     assert el.tag == "request"
     pyref = PyReference.parse_xml(
         el, source_location, default_function="modify_proxy_request", default_objs=dict(AbortProxy=AbortProxy)
     )
     header = el.get("header")
     content = el.get("content")
     ## FIXME: the misspelling is annoying :(
     if (not header and content) or (not content and header):
         raise DeliveranceSyntaxError(
             "If you provide a header attribute you must provide a " "content attribute, and vice versa",
             element=el,
             source_location=source_location,
         )
     return cls(pyref, header, content, source_location=source_location)
Ejemplo n.º 8
0
 def parse_xml(cls, el, source_location):
     """Parse an instance from an etree XML element"""
     assert el.tag == 'request'
     pyref = PyReference.parse_xml(el,
                                   source_location,
                                   default_function='modify_proxy_request',
                                   default_objs=dict(AbortProxy=AbortProxy))
     header = el.get('header')
     content = el.get('content')
     ## FIXME: the misspelling is annoying :(
     if (not header and content) or (not content and header):
         raise DeliveranceSyntaxError(
             "If you provide a header attribute you must provide a "
             "content attribute, and vice versa",
             element=el,
             source_location=source_location)
     return cls(pyref, header, content, source_location=source_location)
Ejemplo n.º 9
0
 def parse_xml(cls, el, source_location):
     """Create an instance from a parsed element"""
     assert el.tag == 'response'
     pyref = PyReference.parse_xml(
         el, source_location,
         default_function='modify_proxy_response', 
         default_objs=dict(AbortProxy=AbortProxy))
     header = el.get('header')
     content = el.get('content')
     if (not header and content) or (not content and header):
         raise DeliveranceSyntaxError(
             "If you provide a header attribute you must provide a content "
             "attribute, and vice versa",
             element=el, source_location=source_location)
     rewrite_links = asbool(el.get('rewrite-links'))
     return cls(pyref=pyref, header=header, content=content, 
                rewrite_links=rewrite_links, source_location=source_location)
Ejemplo n.º 10
0
 def parse_xml(cls, el, source_location):
     """Create an instance from a parsed element"""
     assert el.tag == 'response'
     pyref = PyReference.parse_xml(
         el, source_location,
         default_function='modify_proxy_response', 
         default_objs=dict(AbortProxy=AbortProxy))
     header = el.get('header')
     content = el.get('content')
     if (not header and content) or (not content and header):
         raise DeliveranceSyntaxError(
             "If you provide a header attribute you must provide a content "
             "attribute, and vice versa",
             element=el, source_location=source_location)
     rewrite_links = asbool(el.get('rewrite-links'))
     return cls(pyref=pyref, header=header, content=content, 
                rewrite_links=rewrite_links, source_location=source_location)
Ejemplo n.º 11
0
    def parse_xml(cls, el, source_location, environ=None, traverse=False):
        """Parse an instance from an etree XML element"""

        middleware_factory = None
        middleware_factory_kwargs = None

        if traverse and el.tag != 'server-settings':
            try:
                el = el.xpath('//server-settings')[0]
            except IndexError:
                raise DeliveranceSyntaxError(
                    "There is no <server-settings> element", element=el)
        if environ is None:
            environ = os.environ
        assert el.tag == 'server-settings'
        server_host = 'localhost:8080'
        ## FIXME: should these defaults be passed in:
        execute_pyref = True
        display_local_files = True
        edit_local_files = True
        dev_allow_ips = []
        dev_deny_ips = []
        dev_htpasswd = None
        dev_expiration = 0
        dev_users = {}
        dev_secret_file = os.path.join(tempfile.gettempdir(), 'deliverance',
                                       'devauth.txt')
        for child in el:
            if child.tag is Comment:
                continue
            ## FIXME: should some of these be attributes?
            elif child.tag == 'server':
                server_host = cls.substitute(child.text, environ)
            elif child.tag == 'execute-pyref':
                execute_pyref = asbool(cls.substitute(child.text, environ))
            elif child.tag == 'dev-allow':
                dev_allow_ips.extend(
                    cls.substitute(child.text, environ).split())
            elif child.tag == 'dev-deny':
                dev_deny_ips.extend(
                    cls.substitute(child.text, environ).split())
            elif child.tag == 'dev-htpasswd':
                dev_htpasswd = os.path.join(
                    os.path.dirname(url_to_filename(source_location)),
                    cls.substitute(child.text, environ))
            elif child.tag == 'dev-expiration':
                dev_expiration = cls.substitute(child.text, environ)
                if dev_expiration:
                    dev_expiration = int(dev_expiration)
            elif child.tag == 'display-local-files':
                display_local_files = asbool(
                    cls.substitute(child.text, environ))
            elif child.tag == 'edit-local-files':
                edit_local_files = asbool(cls.substitute(child.text, environ))
            elif child.tag == 'dev-user':
                username = cls.substitute(child.get('username', ''), environ)
                ## FIXME: allow hashed password?
                password = cls.substitute(child.get('password', ''), environ)
                if not username or not password:
                    raise DeliveranceSyntaxError(
                        "<dev-user> must have both a username and password attribute",
                        element=child)
                if username in dev_users:
                    raise DeliveranceSyntaxError(
                        '<dev-user username="******"> appears more than once' %
                        username,
                        element=el)
                dev_users[username] = password
            elif child.tag == 'dev-secret-file':
                dev_secret_file = cls.substitute(child.text, environ)
            elif child.tag == 'middleware-factory':
                ref = PyReference.parse_xml(child, source_location)
                middleware_factory = ref.function
                middleware_factory_kwargs = ref.args or None
            else:
                raise DeliveranceSyntaxError(
                    'Unknown element in <server-settings>: <%s>' % child.tag,
                    element=child)
        if dev_users and dev_htpasswd:
            raise DeliveranceSyntaxError(
                "You can use <dev-htpasswd> or <dev-user>, but not both",
                element=el)
        if not dev_users and not dev_htpasswd:
            ## FIXME: not sure this is the best way to warn
            print(
                'Warning: no <dev-users> or <dev-htpasswd>; logging is inaccessible'
            )
        ## FIXME: add a default allow_ips of 127.0.0.1?
        return cls(server_host,
                   execute_pyref=execute_pyref,
                   display_local_files=display_local_files,
                   edit_local_files=edit_local_files,
                   dev_allow_ips=dev_allow_ips,
                   dev_deny_ips=dev_deny_ips,
                   dev_users=dev_users,
                   dev_htpasswd=dev_htpasswd,
                   dev_expiration=dev_expiration,
                   source_location=source_location,
                   dev_secret_file=dev_secret_file,
                   middleware_factory=middleware_factory,
                   middleware_factory_kwargs=middleware_factory_kwargs)
Ejemplo n.º 12
0
 def from_xml(cls, el, source_location):
     """Parses and instantiates the class from an element"""
     pyref = PyReference.parse_xml(el,
                                   source_location=source_location,
                                   default_function='transform')
     return cls(source_location, pyref)
Ejemplo n.º 13
0
    def parse_xml(cls, el, source_location, environ=None, traverse=False):
        """Parse an instance from an etree XML element"""

        middleware_factory = None
        middleware_factory_kwargs = None

        if traverse and el.tag != 'server-settings':
            try:
                el = el.xpath('//server-settings')[0]
            except IndexError:
                raise DeliveranceSyntaxError(
                    "There is no <server-settings> element",
                    element=el)
        if environ is None:
            environ = os.environ
        assert el.tag == 'server-settings'
        server_host = 'localhost:8080'
        ## FIXME: should these defaults be passed in:
        execute_pyref = True
        display_local_files = True
        edit_local_files = True
        dev_allow_ips = []
        dev_deny_ips = []
        dev_htpasswd = None
        dev_expiration = 0
        dev_users = {}
        dev_secret_file = os.path.join(tempfile.gettempdir(), 'deliverance', 'devauth.txt')
        for child in el:
            if child.tag is Comment:
                continue
            ## FIXME: should some of these be attributes?
            elif child.tag == 'server':
                server_host = cls.substitute(child.text, environ)
            elif child.tag == 'execute-pyref':
                execute_pyref = asbool(cls.substitute(child.text, environ))
            elif child.tag == 'dev-allow':
                dev_allow_ips.extend(cls.substitute(child.text, environ).split())
            elif child.tag == 'dev-deny':
                dev_deny_ips.extend(cls.substitute(child.text, environ).split())
            elif child.tag == 'dev-htpasswd':
                dev_htpasswd = os.path.join(os.path.dirname(url_to_filename(source_location)), cls.substitute(child.text, environ))
            elif child.tag == 'dev-expiration':
                dev_expiration = cls.substitute(child.text, environ)
                if dev_expiration:
                    dev_expiration = int(dev_expiration)
            elif child.tag == 'display-local-files':
                display_local_files = asbool(cls.substitute(child.text, environ))
            elif child.tag == 'edit-local-files':
                edit_local_files = asbool(cls.substitute(child.text, environ))
            elif child.tag == 'dev-user':
                username = cls.substitute(child.get('username', ''), environ)
                ## FIXME: allow hashed password?
                password = cls.substitute(child.get('password', ''), environ)
                if not username or not password:
                    raise DeliveranceSyntaxError(
                        "<dev-user> must have both a username and password attribute",
                        element=child)
                if username in dev_users:
                    raise DeliveranceSyntaxError(
                        '<dev-user username="******"> appears more than once' % username,
                        element=el)
                dev_users[username] = password
            elif child.tag == 'dev-secret-file':
                dev_secret_file = cls.substitute(child.text, environ)
            elif child.tag == 'middleware-factory':
                ref = PyReference.parse_xml(child, source_location)
                middleware_factory = ref.function
                middleware_factory_kwargs = ref.args or None
            else:
                raise DeliveranceSyntaxError(
                    'Unknown element in <server-settings>: <%s>' % child.tag,
                    element=child)
        if dev_users and dev_htpasswd:
            raise DeliveranceSyntaxError(
                "You can use <dev-htpasswd> or <dev-user>, but not both",
                element=el)
        if not dev_users and not dev_htpasswd:
            ## FIXME: not sure this is the best way to warn
            print 'Warning: no <dev-users> or <dev-htpasswd>; logging is inaccessible'
        ## FIXME: add a default allow_ips of 127.0.0.1?
        return cls(server_host, execute_pyref=execute_pyref, 
                   display_local_files=display_local_files,
                   edit_local_files=edit_local_files,
                   dev_allow_ips=dev_allow_ips, dev_deny_ips=dev_deny_ips, 
                   dev_users=dev_users, dev_htpasswd=dev_htpasswd,
                   dev_expiration=dev_expiration,
                   source_location=source_location,
                   dev_secret_file=dev_secret_file,
                   middleware_factory=middleware_factory,
                   middleware_factory_kwargs=middleware_factory_kwargs)
Ejemplo n.º 14
0
 def from_xml(cls, el, source_location):
     """Parses and instantiates the class from an element"""
     pyref = PyReference.parse_xml(
         el, source_location=source_location,
         default_function='transform')
     return cls(source_location, pyref)