Skip to content
Myr

Correctly handle ipv6 addresses as a part of URL (#1349)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Correctly handle ipv6 addresses as a host * Fixed typo * Added an extra rfc reference * Update tests/models/test_url.py * Update tests/models/test_url.py

authored by cdeler · committed by GitHub · parent dbbcf43 · Browse files · 2 files+54 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+54 added−1 removed

  • tests:+34 added−0 removed
  • other:+20 added−1 removed

2 files+54 added−1 removed

modified:httpx/_models.py+20 added-1 removed
106 unmodified lines
107 if isinstance(url, tuple):108 raw_scheme, raw_host, port, raw_path = url109 scheme = raw_scheme.decode("ascii")110 host = raw_host.decode("ascii")111 if host and ":" in host and host[0] != "[":112 # it's an IPv6 address, so it should be enclosed in "[" and "]"113 # ref: https://tools.ietf.org/html/rfc2732#section-2114 # ref: https://tools.ietf.org/html/rfc3986#section-3.2.2115 host = f"[{host}]"116 port_str = "" if port is None else f":{port}"117 path = raw_path.decode("ascii")118 url = f"{scheme}://{host}{port_str}{path}"119
70 unmodified lines
190 assert url.host == "www.example.org"191192 url = httpx.URL("http://中国.icom.museum")193 assert url.host == "xn--fiqs8s.icom.museum"194195 url = httpx.URL("https://[::ffff:192.168.0.1]")196 assert url.host == "::ffff:192.168.0.1"197 """190 return self._uri_reference.host or ""198 host: str = self._uri_reference.host199200 if host and ":" in host and host[0] == "[":201 # it's an IPv6 address202 host = host.lstrip("[").rstrip("]")203204 return host or ""205206 @property207 def port(self) -> typing.Optional[int]:208 """209 The URL port as an integer.
139 unmodified lines
349 if "host" in kwargs or "port" in kwargs:350 # Consolidate host and port into netloc.351 host = kwargs.pop("host", self.host) or ""352 port = kwargs.pop("port", self.port)353354 if host and ":" in host and host[0] != "[":355 # it's an IPv6 address, so it should be hidden under bracket356 host = f"[{host}]"357358 kwargs["netloc"] = f"{host}:{port}" if port is not None else host359360 if "userinfo" in kwargs or "netloc" in kwargs:361 # Consolidate userinfo and netloc into authority.
1119 unmodified lines
modified:tests/models/test_url.py+34 added
285 unmodified lines
286 url = httpx.URL("https://www.example.com/path%20to%20somewhere")287 assert url.path == "/path to somewhere"288 assert url.query == b""289 assert url.raw_path == b"/path%20to%20somewhere"290291292def test_ipv6_url():293 url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")294295 assert url.host == "::ffff:192.168.0.1"296 assert url.netloc == "[::ffff:192.168.0.1]:5678"297298299@pytest.mark.parametrize(300 "url_str",301 [302 "http://127.0.0.1:1234",303 "http://example.com:1234",304 "http://[::ffff:127.0.0.1]:1234",305 ],306)307@pytest.mark.parametrize("new_host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])308def test_ipv6_url_copy_with_host(url_str, new_host):309 url = httpx.URL(url_str).copy_with(host=new_host)310311 assert url.host == "::ffff:192.168.0.1"312 assert url.netloc == "[::ffff:192.168.0.1]:1234"313 assert str(url) == "http://[::ffff:192.168.0.1]:1234"314315316@pytest.mark.parametrize("host", [b"[::ffff:192.168.0.1]", b"::ffff:192.168.0.1"])317def test_ipv6_url_from_raw_url(host):318 raw_url = (b"https", host, 443, b"/")319 url = httpx.URL(raw_url)320321 assert url.host == "::ffff:192.168.0.1"322 assert url.netloc == "[::ffff:192.168.0.1]:443"323 assert str(url) == "https://[::ffff:192.168.0.1]:443/"