Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tolerate and correct IPv6 without brackets #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

azmeuk
Copy link

@azmeuk azmeuk commented Sep 24, 2022

This PR is based on #165 and fixes #124. If #124 is too controversial, I can rebase this PR on master, just tell me :)

Depending on the contexts, people does not always agree on brackets usage with IPv6. For instance, the ipaddress core module handles IPv6 without brackets as do some other low-level python modules. Another example: DNS AAAA records usually handles IPv6 without brackets.
However in a URL context, with a port number being optionally associated with an IP, the brackets become mandatory to distinguish the columns inside the IP from the column that separate the port number from the IP.

As suggested in #124, I think this would be nice if a convenience utility like furl would tolerate and correct any usage. This would allow users to not need to check and correct manually the IPv6 they get from other libraries that may generate IPv6 without brackets because this is relevant in their development context. For instance a DNS SRV record being read with aiodns.

This is what this PR allows. There is not much code here, everything lies here:

    def host(self, host):
        ...
        if (
            is_valid_ipv6(host)
            and callable_attr(host, 'startswith')
            and callable_attr(host, 'endswith')
            and not host.startswith("[")
            and not host.endswith("]")
        ):
            host = "[" + host + "]"
        ...

This allows this usage:

        f.set(host="::1")
        assert f.host == "[::1]"
        assert f.url == "http://[::1]/"

        f.set(host="[::]")
        assert f.host == "[::]"
        assert f.url == "http://[::]/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IPv6 addresses do not quite work as advertised
1 participant