From: Yasser Tahiri Date: Wed, 9 Feb 2022 12:12:09 +0000 (+0100) Subject: Refactor `httpx/_utils.py` (#1943) X-Git-Tag: 0.23.0~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4161062501a32ce258e9f990737d494bfe419aa;p=thirdparty%2Fhttpx.git Refactor `httpx/_utils.py` (#1943) * Removes unnecessary call to keys() when iterating over a dictionary * Simplify conditionals into a form like a switch statement * Merge else clause's nested if statement into elif * Update httpx/_utils.py * Update httpx/_utils.py Co-authored-by: Tom Christie --- diff --git a/httpx/_utils.py b/httpx/_utils.py index 4d791b08..3632783b 100644 --- a/httpx/_utils.py +++ b/httpx/_utils.py @@ -472,19 +472,18 @@ class URLPattern: self.port = url.port if not url.host or url.host == "*": self.host_regex: typing.Optional[typing.Pattern[str]] = None + elif url.host.startswith("*."): + # *.example.com should match "www.example.com", but not "example.com" + domain = re.escape(url.host[2:]) + self.host_regex = re.compile(f"^.+\\.{domain}$") + elif url.host.startswith("*"): + # *example.com should match "www.example.com" and "example.com" + domain = re.escape(url.host[1:]) + self.host_regex = re.compile(f"^(.+\\.)?{domain}$") else: - if url.host.startswith("*."): - # *.example.com should match "www.example.com", but not "example.com" - domain = re.escape(url.host[2:]) - self.host_regex = re.compile(f"^.+\\.{domain}$") - elif url.host.startswith("*"): - # *example.com should match "www.example.com" and "example.com" - domain = re.escape(url.host[1:]) - self.host_regex = re.compile(f"^(.+\\.)?{domain}$") - else: - # example.com should match "example.com" but not "www.example.com" - domain = re.escape(url.host) - self.host_regex = re.compile(f"^{domain}$") + # example.com should match "example.com" but not "www.example.com" + domain = re.escape(url.host) + self.host_regex = re.compile(f"^{domain}$") def matches(self, other: "URL") -> bool: if self.scheme and self.scheme != other.scheme: