Browse Source

hostname_from_fqdn work with utf8 or idna, whichevr it's passed

pull/939/head
Ross McFarland 3 years ago
parent
commit
3b102b4516
No known key found for this signature in database GPG Key ID: 943B179E15D3B22A
2 changed files with 19 additions and 2 deletions
  1. +9
    -2
      octodns/zone.py
  2. +10
    -0
      tests/test_octodns_zone.py

+ 9
- 2
octodns/zone.py View File

@ -41,7 +41,8 @@ class Zone(object):
self._root_ns = None
# optional leading . to match empty hostname
# optional trailing . b/c some sources don't have it on their fqdn
self._name_re = re.compile(fr'\.?{name}?$')
self._utf8_name_re = re.compile(fr'\.?{idna_decode(name)}?$')
self._idna_name_re = re.compile(fr'\.?{self.name}?$')
# Copy-on-write semantics support, when `not None` this property will
# point to a location with records for this `Zone`. Once `hydrated`
@ -63,7 +64,13 @@ class Zone(object):
return self._root_ns
def hostname_from_fqdn(self, fqdn):
return self._name_re.sub('', fqdn)
try:
fqdn.encode('ascii')
# it's non-idna or idna encoded
return self._idna_name_re.sub('', idna_encode(fqdn))
except UnicodeEncodeError:
# it has utf8 chars
return self._utf8_name_re.sub('', fqdn)
def add_record(self, record, replace=False, lenient=False):
if self._origin:


+ 10
- 0
tests/test_octodns_zone.py View File

@ -47,10 +47,16 @@ class TestZone(TestCase):
('foo.bar', 'foo.bar.unit.tests'),
('foo.unit.tests', 'foo.unit.tests.unit.tests.'),
('foo.unit.tests', 'foo.unit.tests.unit.tests'),
# if we pass utf8 we get utf8
('déjà', 'déjà.unit.tests'),
('déjà.foo', 'déjà.foo.unit.tests'),
('bar.déjà', 'bar.déjà.unit.tests'),
('bar.déjà.foo', 'bar.déjà.foo.unit.tests'),
# if we pass idna we get idna
('xn--dj-kia8a', 'xn--dj-kia8a.unit.tests'),
('xn--dj-kia8a.foo', 'xn--dj-kia8a.foo.unit.tests'),
('bar.xn--dj-kia8a', 'bar.xn--dj-kia8a.unit.tests'),
('bar.xn--dj-kia8a.foo', 'bar.xn--dj-kia8a.foo.unit.tests'),
):
self.assertEqual(hostname, zone.hostname_from_fqdn(fqdn))
@ -68,6 +74,10 @@ class TestZone(TestCase):
('déjà.foo', 'déjà.foo.grüßen.de'),
('bar.déjà', 'bar.déjà.grüßen.de'),
('bar.déjà.foo', 'bar.déjà.foo.grüßen.de'),
('xn--dj-kia8a', 'xn--dj-kia8a.xn--gren-wna7o.de'),
('xn--dj-kia8a.foo', 'xn--dj-kia8a.foo.xn--gren-wna7o.de'),
('bar.xn--dj-kia8a', 'bar.xn--dj-kia8a.xn--gren-wna7o.de'),
('bar.xn--dj-kia8a.foo', 'bar.xn--dj-kia8a.foo.xn--gren-wna7o.de'),
):
self.assertEqual(hostname, zone.hostname_from_fqdn(fqdn))


Loading…
Cancel
Save