1
0
mirror of https://github.com/github/octodns.git synced 2024-05-11 05:55:00 +00:00

Merge pull request #939 from octodns/host-from-fqdn-idna

hostname_from_fqdn work with utf8 or idna, whichever it's passed
This commit is contained in:
Ross McFarland
2022-09-23 18:53:16 -07:00
committed by GitHub
2 changed files with 19 additions and 2 deletions

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:

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))