Skip to content

Commit

Permalink
Allow hostnames with underscores when parsing URLs (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom authored Nov 24, 2020
1 parent 2c2722f commit fa05cfa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ target/

#PyCharm
.idea

#PyDev (Eclipse)
.project
.pydevproject
.settings
11 changes: 7 additions & 4 deletions apprise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@
r'(?P<email>(?P<userid>[a-z0-9$%=_~-]+'
r'(?:\.[a-z0-9$%+=_~-]+)'
r'*)@(?P<domain>('
r'(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+'
r'[a-z0-9](?:[a-z0-9-]*[a-z0-9]))|'
r'[a-z0-9][a-z0-9-]{5,})))'
r'(?:[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?\.)+'
r'[a-z0-9](?:[a-z0-9_-]*[a-z0-9]))|'
r'[a-z0-9][a-z0-9_-]{5,})))'
r'\s*>?', re.IGNORECASE)

# Regular expression used to extract a phone number
Expand Down Expand Up @@ -232,9 +232,12 @@ def is_hostname(hostname, ipv4=True, ipv6=True):
# - Hostnames can ony be comprised of alpha-numeric characters and the
# hyphen (-) character.
# - Hostnames can not start with the hyphen (-) character.
# - as a workaround for https://github.com/docker/compose/issues/229 to
# being able to address services in other stacks, we also allow
# underscores in hostnames
# - labels can not exceed 63 characters
allowed = re.compile(
r'(?!-)[a-z0-9][a-z0-9-]{1,62}(?<!-)$',
r'^[a-z0-9][a-z0-9_-]{1,62}(?<!-)$',
re.IGNORECASE,
)

Expand Down
11 changes: 10 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,14 +532,15 @@ def test_is_hostname():
assert utils.is_hostname('yahoo.ca.') == 'yahoo.ca'
assert utils.is_hostname('valid-dashes-in-host.ca') == \
'valid-dashes-in-host.ca'
assert utils.is_hostname('valid-underscores_in_host.ca') == \
'valid-underscores_in_host.ca'

# Invalid Hostnames
assert utils.is_hostname('-hostname.that.starts.with.a.dash') is False
assert utils.is_hostname('invalid-characters_#^.ca') is False
assert utils.is_hostname(' spaces ') is False
assert utils.is_hostname(' ') is False
assert utils.is_hostname('') is False
assert utils.is_hostname('valid-underscores_in_host.ca') is False

# Valid IPv4 Addresses
assert utils.is_hostname('127.0.0.1') == '127.0.0.1'
Expand Down Expand Up @@ -625,6 +626,14 @@ def test_is_email():
assert 'test' == results['user']
assert '' == results['label']

results = utils.is_email('test@my-valid_host.com')
assert '' == results['name']
assert 'test@my-valid_host.com' == results['email']
assert 'test@my-valid_host.com' == results['full_email']
assert 'my-valid_host.com' == results['domain']
assert 'test' == results['user']
assert '' == results['label']

results = utils.is_email('tag+test@gmail.com')
assert '' == results['name']
assert 'test@gmail.com' == results['email']
Expand Down

0 comments on commit fa05cfa

Please sign in to comment.