summaryrefslogtreecommitdiff
path: root/django/http
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2023-07-27 11:27:46 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-08-02 13:07:41 +0200
commitee36c332b24247ba3790a355a13e682537258d6d (patch)
tree4a8ec266e1077c7c76600f9e7d97197ddadb0502 /django/http
parentc77fbda7ceaf00d09c322b6e0d0b0b82b4f32e98 (diff)
Simplified django.http.request.split_domain_port().
Use the capture groups from the regular expression that has already been matched to avoid resplitting and the need to special case for IPv6.
Diffstat (limited to 'django/http')
-rw-r--r--django/http/request.py20
1 files changed, 6 insertions, 14 deletions
diff --git a/django/http/request.py b/django/http/request.py
index be6823cacb..86ea68c20c 100644
--- a/django/http/request.py
+++ b/django/http/request.py
@@ -30,7 +30,7 @@ from django.utils.regex_helper import _lazy_re_compile
RAISE_ERROR = object()
host_validation_re = _lazy_re_compile(
- r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9\.:]+\])(:[0-9]+)?$"
+ r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9.:]+\])(?::([0-9]+))?$"
)
@@ -698,19 +698,11 @@ def split_domain_port(host):
Returned domain is lowercased. If the host is invalid, the domain will be
empty.
"""
- host = host.lower()
-
- if not host_validation_re.match(host):
- return "", ""
-
- if host[-1] == "]":
- # It's an IPv6 address without a port.
- return host, ""
- bits = host.rsplit(":", 1)
- domain, port = bits if len(bits) == 2 else (bits[0], "")
- # Remove a trailing dot (if present) from the domain.
- domain = domain.removesuffix(".")
- return domain, port
+ if match := host_validation_re.fullmatch(host.lower()):
+ domain, port = match.groups(default="")
+ # Remove a trailing dot (if present) from the domain.
+ return domain.removesuffix("."), port
+ return "", ""
def validate_host(host, allowed_hosts):