@@ -997,13 +997,18 @@ def _normalize_path(cls, path: str) -> str:
997997 def _encode_host (
998998 cls , host : str , human : bool = False , validate_host : bool = True
999999 ) -> str :
1000- if "%" in host :
1001- raw_ip , sep , zone = host .partition ("%" )
1002- else :
1003- raw_ip = host
1004- sep = zone = ""
1005-
1006- if raw_ip and raw_ip [- 1 ].isdigit () or ":" in raw_ip :
1000+ if host and host [- 1 ].isdigit () or ":" in host :
1001+ # If the host ends with a digit or contains a colon, its likely
1002+ # an IP address. So we check with _ip_compressed_version
1003+ # and fall-through if its not an IP address. This is a performance
1004+ # optimization to avoid parsing IP addresses as much as possible
1005+ # because it is orders of magnitude slower than almost any other
1006+ # operation this library does.
1007+ if "%" in host :
1008+ raw_ip , sep , zone = host .partition ("%" )
1009+ else :
1010+ raw_ip = host
1011+ sep = zone = ""
10071012 # Might be an IP address, check it
10081013 #
10091014 # IP Addresses can look like:
@@ -1016,10 +1021,6 @@ def _encode_host(
10161021 # Rare IP Address formats are not supported per:
10171022 # https://datatracker.ietf.org/doc/html/rfc3986#section-7.4
10181023 #
1019- # We try to avoid parsing IP addresses as much as possible
1020- # since its orders of magnitude slower than almost any other operation
1021- # this library does.
1022- #
10231024 # IP parsing is slow, so its wrapped in an LRU
10241025 try :
10251026 ip_compressed_version = _ip_compressed_version (raw_ip )
@@ -1029,11 +1030,9 @@ def _encode_host(
10291030 # These checks should not happen in the
10301031 # LRU to keep the cache size small
10311032 host , version = ip_compressed_version
1032- if sep :
1033- host += "%" + zone
10341033 if version == 6 :
1035- return f"[{ host } ]"
1036- return host
1034+ return f"[{ host } % { zone } ]" if sep else f"[ { host } ]"
1035+ return f" { host } % { zone } " if sep else host
10371036
10381037 host = host .lower ()
10391038 if human :
0 commit comments