Skip to content
Myr

Perform port normalization for http, https, ws, wss, and ftp schemes (#1603)

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

authored by Tom Christie · committed by GitHub · parent c927f3e · Browse files · 3 files+41 added−17 removed
This commit

State

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

Diff stats

3 files+41 added−17 removed

  • tests:+5 added−6 removed
  • other:+36 added−11 removed

3 files+41 added−17 removed

modified:httpx/_models.py+36 added-11 removed
98 unmodified lines
99 assert url.raw_host == b"xn--fiqs8s.icom.museum"100 url = httpx.URL("http://xn--fiqs8s.icom.museum")101 assert url.raw_host == b"xn--fiqs8s.icom.museum"102103 * `url.port` is either None or an integer. URLs that include the default port for104 "http", "https", "ws", "wss", and "ftp" schemes have their port normalized to `None`.105106 assert httpx.URL("http://example.com") == httpx.URL("http://example.com:80")107 assert httpx.URL("http://example.com").port is None108 assert httpx.URL("http://example.com:80").port is None109110 * `url.userinfo` is raw bytes, without URL escaping. Usually you'll want to work with111 `url.username` and `url.password` instead, which handle the URL escaping.112113 * `url.raw_path` is raw bytes of both the path and query, without URL escaping.
36 unmodified lines
150 raise TypeError(151 f"Invalid type for url. Expected str or httpx.URL, got {type(url)}: {url!r}"152 )153154 # Perform port normalization, following the WHATWG spec for default ports.155 #156 # See:157 # * https://tools.ietf.org/html/rfc3986#section-3.2.3158 # * https://url.spec.whatwg.org/#url-miscellaneous159 # * https://url.spec.whatwg.org/#scheme-state160 default_port = {161 "ftp": ":21",162 "http": ":80",163 "https": ":443",164 "ws": ":80",165 "wss": ":443",166 }.get(self._uri_reference.scheme, "")167 authority = self._uri_reference.authority or ""168 if default_port and authority.endswith(default_port):169 authority = authority[: -len(default_port)]170 self._uri_reference = self._uri_reference.copy_with(authority=authority)171172 if kwargs:173 self._uri_reference = self.copy_with(**kwargs)._uri_reference174175 @property
101 unmodified lines
277 @property278 def port(self) -> typing.Optional[int]:279 """280 The URL port as an integer.281282 Note that the URL class performs port normalization as per the WHATWG spec.283 Default ports for "http", "https", "ws", "wss", and "ftp" schemes are always284 treated as `None`.285286 For example:287288 assert httpx.URL("http://www.example.com") == httpx.URL("http://www.example.com:80")289 assert httpx.URL("http://www.example.com:80").port is None290 """291 port = self._uri_reference.port292 return int(port) if port else None293
2 unmodified lines
296 """297 Either `<host>` or `<host>:<port>` as bytes.298 Always normalized to lowercase, and IDNA encoded.299266 The port component is not included if it is the default for an267 "http://" or "https://" URL.268300 This property may be used for generating the value of a request301 "Host" header.271272 See: https://tools.ietf.org/html/rfc3986#section-3.2.3302 """303 host = self._uri_reference.host or ""304 port = self._uri_reference.port305 netloc = host.encode("ascii")
269 unmodified lines
575 def __hash__(self) -> int:576 return hash(str(self))577578 def __eq__(self, other: typing.Any) -> bool:550 return isinstance(other, (URL, str)) and str(self) == str(other)579 return isinstance(other, (URL, str)) and str(self) == str(URL(other))580581 def __str__(self) -> str:582 return self._uri_reference.unsplit()583
543 unmodified lines
1127 "Content-Length" in self.headers or "Transfer-Encoding" in self.headers1128 )11291130 if not has_host and self.url.host:1102 default_port = {"http": b":80", "https": b":443"}.get(self.url.scheme, b"")1103 host_header = self.url.netloc1104 if host_header.endswith(default_port):1105 host_header = host_header[: -len(default_port)]1106 auto_headers.append((b"Host", host_header))1131 auto_headers.append((b"Host", self.url.netloc))1132 if not has_content_length and self.method in ("POST", "PUT", "PATCH"):1133 auto_headers.append((b"Content-Length", b"0"))11341135 self.headers = Headers(auto_headers + self.headers.raw)
709 unmodified lines
modified:tests/client/test_proxies.py+1 added-2 removed
77 unmodified lines
78 ("http://example.com", {"all://": PROXY_URL}, PROXY_URL),79 ("http://example.com", {"all://": PROXY_URL, "http://example.com": None}, None),80 ("http://example.com", {"http://": PROXY_URL}, PROXY_URL),81 ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),82 ("http://example.com", {"all://example.com:80": PROXY_URL}, None),82 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),84 ("http://example.com", {"http://example.com:80": PROXY_URL}, None),83 ("http://example.com", {"http://example.com:80": PROXY_URL}, PROXY_URL),84 ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),85 ("http://example.com:8080", {"http://example.com": PROXY_URL}, PROXY_URL),86 (87 "http://example.com",
186 unmodified lines
modified:tests/models/test_url.py+4 added-4 removed
10 unmodified lines
11 "http://xn--fiqs8s.icom.museum:80/",12 "中国.icom.museum",13 b"xn--fiqs8s.icom.museum",14 "http",15 80,15 None,16 ),17 (18 "http://Königsgäßchen.de",19 "http://xn--knigsgchen-b4a3dun.de",
15 unmodified lines
35 "https://xn--nxasmm1c.com:443",36 "βόλος.com",37 b"xn--nxasmm1c.com",38 "https",39 443,39 None,40 ),41 (42 "http://ශ්‍රී.com:444",43 "http://xn--10cl1a0b660p.com:444",
329 unmodified lines
373 raw_url = (b"https", host, 443, b"/")374 url = httpx.URL(raw_url)375376 assert url.host == "::ffff:192.168.0.1"377 assert url.netloc == b"[::ffff:192.168.0.1]:443"378 assert str(url) == "https://[::ffff:192.168.0.1]:443/"377 assert url.netloc == b"[::ffff:192.168.0.1]"378 assert str(url) == "https://[::ffff:192.168.0.1]/"