-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
31 lines (24 loc) · 837 Bytes
/
Copy pathparser.py
File metadata and controls
31 lines (24 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from __future__ import annotations
from urllib.parse import urlparse
# Normalize raw user text into a clean domain name.
def normalize_domain(raw_value: str) -> str:
value = raw_value.strip().lower()
if not value:
return ""
if "://" not in value:
value = f"https://{value}"
parsed = urlparse(value)
host = parsed.netloc or parsed.path
host = host.split("/")[0]
host = host.split(":")[0]
if host.startswith("www."):
host = host[4:]
return host.strip(".")
# Validate a normalized domain to avoid invalid hosts entries.
def is_valid_domain(domain: str) -> bool:
if not domain or " " in domain:
return False
if "." not in domain:
return False
allowed = set("abcdefghijklmnopqrstuvwxyz0123456789-.")
return all(ch in allowed for ch in domain)