Skip to content

Commit 8443007

Browse files
committed
SimpleChatTC:SimpleProxy:UrlValidator module initial skeleton
Copy validate_url and build initial skeleton
1 parent 6f7b6cc commit 8443007

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Handle URL validation
2+
# by Humans for All
3+
4+
import urllib.parse
5+
import re
6+
from dataclasses import dataclass
7+
8+
9+
gMe = {
10+
}
11+
12+
13+
@dataclass(frozen=True)
14+
class UrlVResponse:
15+
"""
16+
Used to return result wrt urlreq helper below.
17+
"""
18+
callOk: bool
19+
statusCode: int
20+
statusMsg: str = ""
21+
22+
23+
def validator_ok():
24+
pass
25+
26+
27+
def validate_url(url: str, tag: str):
28+
"""
29+
Implement a re based filter logic on the specified url.
30+
"""
31+
tag=f"VU:{tag}"
32+
if (not gMe.get('--allowed.domains')):
33+
return UrlVResponse(False, 400, f"DBUG:{tag}:MissingAllowedDomains")
34+
urlParts = urllib.parse.urlparse(url)
35+
print(f"DBUG:ValidateUrl:{urlParts}, {urlParts.hostname}")
36+
urlHName = urlParts.hostname
37+
if not urlHName:
38+
return UrlVResponse(False, 400, f"WARN:{tag}:Missing hostname in Url")
39+
bMatched = False
40+
for filter in gMe['--allowed.domains']:
41+
if re.match(filter, urlHName):
42+
bMatched = True
43+
if not bMatched:
44+
return UrlVResponse(False, 400, f"WARN:{tag}:requested hostname not allowed")
45+
return UrlVResponse(True, 200)

0 commit comments

Comments
 (0)