Ejemplo n.º 1
0
    def _parse_rfc_forwarded(self):
        """Parse RFC 7239 "Forwarded" header.

        Returns:
            list: addresses derived from "for" parameters.
        """

        addr = []

        for forwarded in self.env["HTTP_FORWARDED"].split(","):
            for param in forwarded.split(";"):
                # PERF(kgriffs): Partition() is faster than split().
                key, _, val = param.strip().partition("=")
                if not val:
                    # NOTE(kgriffs): The '=' separator was not found or
                    # it was, but the value was missing.
                    continue

                if key.lower() != "for":
                    # We only want "for" params
                    continue

                host, _ = parse_host(unquote_string(val))
                addr.append(host)

        return addr
Ejemplo n.º 2
0
    def _parse_rfc_forwarded(self):
        """Parse RFC 7239 "Forwarded" header.

        Returns:
            list: addresses derived from "for" parameters.
        """

        addr = []

        for forwarded in self.env['HTTP_FORWARDED'].split(','):
            for param in forwarded.split(';'):
                # PERF(kgriffs): Partition() is faster than split().
                key, _, val = param.strip().partition('=')
                if not val:
                    # NOTE(kgriffs): The '=' separator was not found or
                    # it was, but the value was missing.
                    continue

                if key.lower() != 'for':
                    # We only want "for" params
                    continue

                host, _ = parse_host(unquote_string(val))
                addr.append(host)

        return addr
Ejemplo n.º 3
0
    def port(self):
        try:
            host_header = self._asgi_headers[b'host'].decode('latin1')
            default_port = 443 if self._secure_scheme else 80
            __, port = parse_host(host_header, default_port=default_port)
        except KeyError:
            __, port = self._asgi_server

        return port
Ejemplo n.º 4
0
    def port(self):
        try:
            host_header = self._asgi_headers['host']
            default_port = 80 if self.scheme == 'http' else 443
            __, port = parse_host(host_header, default_port=default_port)
        except KeyError:
            __, port = self._asgi_server

        return port
Ejemplo n.º 5
0
    def host(self):
        try:
            # NOTE(kgriffs): Prefer the host header; the web server
            # isn't supposed to mess with it, so it should be what
            # the client actually sent.
            host_header = self._asgi_headers[b'host'].decode('latin1')
            host, __ = parse_host(host_header)
        except KeyError:
            host, __ = self._asgi_server

        return host
Ejemplo n.º 6
0
    def host(self):
        try:
            # NOTE(kgriffs): Prefer the host header; the web server
            # isn't supposed to mess with it, so it should be what
            # the client actually sent.
            host_header = self.env['HTTP_HOST']
            host, port = parse_host(host_header)
        except KeyError:
            # PERF(kgriffs): According to PEP-3333, this header
            # will always be present.
            host = self.env['SERVER_NAME']

        return host
Ejemplo n.º 7
0
    def host(self):
        try:
            # NOTE(kgriffs): Prefer the host header; the web server
            # isn't supposed to mess with it, so it should be what
            # the client actually sent.
            host_header = self.env["HTTP_HOST"]
            host, port = parse_host(host_header)
        except KeyError:
            # PERF(kgriffs): According to PEP-3333, this header
            # will always be present.
            host = self.env["SERVER_NAME"]

        return host
Ejemplo n.º 8
0
    def access_route(self):
        if self._cached_access_route is None:
            # PERF(kgriffs): 'client' is optional according to the ASGI spec
            #   but it will probably be present, hence the try...except.
            try:
                # NOTE(kgriffs): The ASGI spec states that this can be
                #   any iterable. So we need to read and cache it in
                #   case the iterable is forward-only. But that is
                #   effectively what we are doing since we only ever
                #   access this field when setting self._cached_access_route
                client, __ = self.scope['client']
            except KeyError:
                # NOTE(kgriffs): Default to localhost so that app logic does
                #   note have to special-case the handling of a missing
                #   client field in the connection scope. This should be
                #   a reasonable default, but we can change it later if
                #   that turns out not to be the case.
                client = '127.0.0.1'

            headers = self._asgi_headers

            if b'forwarded' in headers:
                self._cached_access_route = []
                for hop in self.forwarded:
                    if hop.src is not None:
                        host, __ = parse_host(hop.src)
                        self._cached_access_route.append(host)
            elif b'x-forwarded-for' in headers:
                addresses = headers[b'x-forwarded-for'].decode('latin1').split(
                    ',')
                self._cached_access_route = [ip.strip() for ip in addresses]
            elif b'x-real-ip' in headers:
                self._cached_access_route = [
                    headers[b'x-real-ip'].decode('latin1')
                ]

            if self._cached_access_route:
                if self._cached_access_route[-1] != client:
                    self._cached_access_route.append(client)
            else:
                self._cached_access_route = [client] if client else []

        return self._cached_access_route
Ejemplo n.º 9
0
    def _parse_rfc_forwarded(self):
        """Parse RFC 7239 "Forwarded" header.

        Returns:
            list: addresses derived from "for" parameters.
        """
        addr = []
        for forwarded in self.env['HTTP_FORWARDED'].split(','):
            for param in forwarded.split(';'):
                param = param.strip().split('=', 1)
                if len(param) == 1:
                    continue
                key, val = param
                if key.lower() != 'for':
                    # we only want for params
                    continue
                host, _ = parse_host(unquote_string(val))
                addr.append(host)
        return addr
Ejemplo n.º 10
0
    def test_parse_host(self):
        assert uri.parse_host('::1') == ('::1', None)
        assert uri.parse_host('2001:ODB8:AC10:FE01::') == (
            '2001:ODB8:AC10:FE01::',
            None,
        )
        assert uri.parse_host('2001:ODB8:AC10:FE01::', default_port=80) == (
            '2001:ODB8:AC10:FE01::',
            80,
        )

        ipv6_addr = '2001:4801:1221:101:1c10::f5:116'

        assert uri.parse_host(ipv6_addr) == (ipv6_addr, None)
        assert uri.parse_host('[' + ipv6_addr + ']') == (ipv6_addr, None)
        assert uri.parse_host('[' + ipv6_addr + ']:28080') == (ipv6_addr,
                                                               28080)
        assert uri.parse_host('[' + ipv6_addr + ']:8080') == (ipv6_addr, 8080)
        assert uri.parse_host('[' + ipv6_addr + ']:123') == (ipv6_addr, 123)
        assert uri.parse_host('[' + ipv6_addr + ']:42') == (ipv6_addr, 42)

        assert uri.parse_host('173.203.44.122') == ('173.203.44.122', None)
        assert uri.parse_host('173.203.44.122', default_port=80) == (
            '173.203.44.122',
            80,
        )
        assert uri.parse_host('173.203.44.122:27070') == ('173.203.44.122',
                                                          27070)
        assert uri.parse_host('173.203.44.122:123') == ('173.203.44.122', 123)
        assert uri.parse_host('173.203.44.122:42') == ('173.203.44.122', 42)

        assert uri.parse_host('example.com') == ('example.com', None)
        assert uri.parse_host('example.com',
                              default_port=443) == ('example.com', 443)
        assert uri.parse_host('falcon.example.com') == ('falcon.example.com',
                                                        None)
        assert uri.parse_host('falcon.example.com:9876') == (
            'falcon.example.com', 9876)
        assert uri.parse_host('falcon.example.com:42') == (
            'falcon.example.com', 42)
Ejemplo n.º 11
0
    def test_parse_host(self):
        self.assertEqual(uri.parse_host('::1'), ('::1', None))
        self.assertEqual(uri.parse_host('2001:ODB8:AC10:FE01::'),
                         ('2001:ODB8:AC10:FE01::', None))
        self.assertEqual(
            uri.parse_host('2001:ODB8:AC10:FE01::', default_port=80),
            ('2001:ODB8:AC10:FE01::', 80))

        ipv6_addr = '2001:4801:1221:101:1c10::f5:116'

        self.assertEqual(uri.parse_host(ipv6_addr), (ipv6_addr, None))
        self.assertEqual(uri.parse_host('[' + ipv6_addr + ']'),
                         (ipv6_addr, None))
        self.assertEqual(uri.parse_host('[' + ipv6_addr + ']:28080'),
                         (ipv6_addr, 28080))
        self.assertEqual(uri.parse_host('[' + ipv6_addr + ']:8080'),
                         (ipv6_addr, 8080))
        self.assertEqual(uri.parse_host('[' + ipv6_addr + ']:123'),
                         (ipv6_addr, 123))
        self.assertEqual(uri.parse_host('[' + ipv6_addr + ']:42'),
                         (ipv6_addr, 42))

        self.assertEqual(uri.parse_host('173.203.44.122'),
                         ('173.203.44.122', None))
        self.assertEqual(uri.parse_host('173.203.44.122', default_port=80),
                         ('173.203.44.122', 80))
        self.assertEqual(uri.parse_host('173.203.44.122:27070'),
                         ('173.203.44.122', 27070))
        self.assertEqual(uri.parse_host('173.203.44.122:123'),
                         ('173.203.44.122', 123))
        self.assertEqual(uri.parse_host('173.203.44.122:42'),
                         ('173.203.44.122', 42))

        self.assertEqual(uri.parse_host('example.com'),
                         ('example.com', None))
        self.assertEqual(uri.parse_host('example.com', default_port=443),
                         ('example.com', 443))
        self.assertEqual(uri.parse_host('falcon.example.com'),
                         ('falcon.example.com', None))
        self.assertEqual(uri.parse_host('falcon.example.com:9876'),
                         ('falcon.example.com', 9876))
        self.assertEqual(uri.parse_host('falcon.example.com:42'),
                         ('falcon.example.com', 42))
Ejemplo n.º 12
0
    def test_parse_host(self):
        self.assertEqual(uri.parse_host("::1"), ("::1", None))
        self.assertEqual(uri.parse_host("2001:ODB8:AC10:FE01::"), ("2001:ODB8:AC10:FE01::", None))
        self.assertEqual(uri.parse_host("2001:ODB8:AC10:FE01::", default_port=80), ("2001:ODB8:AC10:FE01::", 80))

        ipv6_addr = "2001:4801:1221:101:1c10::f5:116"

        self.assertEqual(uri.parse_host(ipv6_addr), (ipv6_addr, None))
        self.assertEqual(uri.parse_host("[" + ipv6_addr + "]"), (ipv6_addr, None))
        self.assertEqual(uri.parse_host("[" + ipv6_addr + "]:28080"), (ipv6_addr, 28080))
        self.assertEqual(uri.parse_host("[" + ipv6_addr + "]:8080"), (ipv6_addr, 8080))
        self.assertEqual(uri.parse_host("[" + ipv6_addr + "]:123"), (ipv6_addr, 123))
        self.assertEqual(uri.parse_host("[" + ipv6_addr + "]:42"), (ipv6_addr, 42))

        self.assertEqual(uri.parse_host("173.203.44.122"), ("173.203.44.122", None))
        self.assertEqual(uri.parse_host("173.203.44.122", default_port=80), ("173.203.44.122", 80))
        self.assertEqual(uri.parse_host("173.203.44.122:27070"), ("173.203.44.122", 27070))
        self.assertEqual(uri.parse_host("173.203.44.122:123"), ("173.203.44.122", 123))
        self.assertEqual(uri.parse_host("173.203.44.122:42"), ("173.203.44.122", 42))

        self.assertEqual(uri.parse_host("example.com"), ("example.com", None))
        self.assertEqual(uri.parse_host("example.com", default_port=443), ("example.com", 443))
        self.assertEqual(uri.parse_host("falcon.example.com"), ("falcon.example.com", None))
        self.assertEqual(uri.parse_host("falcon.example.com:9876"), ("falcon.example.com", 9876))
        self.assertEqual(uri.parse_host("falcon.example.com:42"), ("falcon.example.com", 42))