SimpleChatTC:SimpleProxy:UrlValidator module initial skeleton

Copy validate_url and build initial skeleton
This commit is contained in:
hanishkvc 2025-11-02 13:07:41 +05:30
parent d3a893cac9
commit c8407a1240
1 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,45 @@
# Handle URL validation
# by Humans for All
import urllib.parse
import re
from dataclasses import dataclass
gMe = {
}
@dataclass(frozen=True)
class UrlVResponse:
"""
Used to return result wrt urlreq helper below.
"""
callOk: bool
statusCode: int
statusMsg: str = ""
def validator_ok():
pass
def validate_url(url: str, tag: str):
"""
Implement a re based filter logic on the specified url.
"""
tag=f"VU:{tag}"
if (not gMe.get('--allowed.domains')):
return UrlVResponse(False, 400, f"DBUG:{tag}:MissingAllowedDomains")
urlParts = urllib.parse.urlparse(url)
print(f"DBUG:ValidateUrl:{urlParts}, {urlParts.hostname}")
urlHName = urlParts.hostname
if not urlHName:
return UrlVResponse(False, 400, f"WARN:{tag}:Missing hostname in Url")
bMatched = False
for filter in gMe['--allowed.domains']:
if re.match(filter, urlHName):
bMatched = True
if not bMatched:
return UrlVResponse(False, 400, f"WARN:{tag}:requested hostname not allowed")
return UrlVResponse(True, 200)