Skip to content

Commit e3b6a1c

Browse files
authored
Update cookiejar.py
1 parent c5a1be2 commit e3b6a1c

1 file changed

Lines changed: 22 additions & 25 deletions

File tree

Lib/http/cookiejar.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -535,26 +535,33 @@ def parse_ns_headers(ns_headers):
535535
# only kept for backwards compatibilty.
536536
IPV4_RE = re.compile(r"\.\d+$", re.ASCII)
537537

538+
def _is_ipv4_hostname(text):
539+
from ipaddress import IPv4Address
540+
try:
541+
IPv4Address(text)
542+
except ValueError:
543+
return False
544+
return True
545+
546+
def _is_ipv6_hostname(text):
547+
if text.startswith('[') and text.endswith(']'):
548+
from ipaddress import IPv6Address
549+
try:
550+
IPv6Address(text[1:-1])
551+
except ValueError:
552+
return False
553+
return True
554+
return False
555+
538556
def is_ip_like_hostname(text):
539557
"""Return True if text is a valid hostname in the form of IP address.
540558
541559
A valid IP-like hostname is either an IPv4 address or
542560
an IPv6 enclosed in brackets (for instance, "[::1]").
543561
"""
544-
from ipaddress import IPv4Address, IPv6Address
545-
# check for IPv4 address
546-
try:
547-
IPv4Address(text)
548-
except ValueError:
549-
# check for IPv6 address in []
550-
if text.startswith('[') and text.endswith(']'):
551-
try:
552-
IPv6Address(text[1:-1])
553-
except ValueError:
554-
return False
555-
else:
556-
return False # not a IPv6 address in []
557-
return True
562+
if _is_ipv4_hostname(text) or _is_ipv6_hostname(text):
563+
return True
564+
return False
558565
def is_HDN(text):
559566
"""Return True if text is a host domain name."""
560567
# XXX
@@ -659,17 +666,7 @@ def eff_request_host(request):
659666
660667
"""
661668
erhn = req_host = request_host(request)
662-
if req_host.startswith('[') and req_host.endswith(']'):
663-
from ipaddress import IPv6Address
664-
try:
665-
IPv6Address(req_host[1:-1])
666-
except ValueError:
667-
is_ipV6 = False
668-
else:
669-
is_ipV6 = True
670-
else:
671-
is_ipV6 = False
672-
if "." not in req_host and not is_ipV6:
669+
if "." not in req_host and not _is_ipv6_hostname(req_host):
673670
# Avoid adding .local at the end of an IPv6 address.
674671
# See RFC 2965 [1] for the rationale of ".local".
675672
# [1]: https://www.rfc-editor.org/rfc/rfc2965

0 commit comments

Comments
 (0)