Skip to content

Commit

Permalink
Refactored class UrlParser()
Browse files Browse the repository at this point in the history
  • Loading branch information
elceef committed Sep 16, 2023
1 parent 88d1767 commit 66a384c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions dnstwist.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,19 @@ def _normalize(self):

class UrlParser():
def __init__(self, url):
if not url:
raise TypeError('argument has to be non-empty string')
u = urllib.parse.urlparse(url if '://' in url else 'http://{}'.format(url))
self.scheme = u.scheme.lower()
if self.scheme not in ('http', 'https'):
raise ValueError('invalid scheme') from None
self.domain = u.hostname.lower()
self.domain = idna.encode(self.domain).decode()
try:
self.domain = idna.encode(self.domain).decode()
except Exception:
raise ValueError('invalid domain name') from None
if not self._validate_domain(self.domain):
raise ValueError('Invalid domain name') from None
self.scheme = u.scheme
if self.scheme not in ('http', 'https'):
raise ValueError('Invalid scheme') from None
raise ValueError('invalid domain name') from None
self.username = u.username
self.password = u.password
self.port = u.port
Expand All @@ -318,7 +323,7 @@ def __init__(self, url):
self.fragment = u.fragment

def _validate_domain(self, domain):
if len(domain) > 253:
if 1 > len(domain) > 253:
return False
if VALID_FQDN_REGEX.match(domain):
try:
Expand Down

0 comments on commit 66a384c

Please sign in to comment.