Skip to content

Commit

Permalink
Refactor httpx/_utils.py (#1943)
Browse files Browse the repository at this point in the history
* 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 <tom@tomchristie.com>
  • Loading branch information
yezz123 and tomchristie committed Feb 9, 2022
1 parent 7d3a534 commit a416106
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions httpx/_utils.py
Expand Up @@ -472,19 +472,18 @@ def __init__(self, pattern: str) -> None:
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:
Expand Down

0 comments on commit a416106

Please sign in to comment.