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:
@@ -41,7 +41,8 @@ class Zone(object):
|
|||||||
self._root_ns = None
|
self._root_ns = None
|
||||||
# optional leading . to match empty hostname
|
# optional leading . to match empty hostname
|
||||||
# optional trailing . b/c some sources don't have it on their fqdn
|
# 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
|
# Copy-on-write semantics support, when `not None` this property will
|
||||||
# point to a location with records for this `Zone`. Once `hydrated`
|
# point to a location with records for this `Zone`. Once `hydrated`
|
||||||
@@ -63,7 +64,13 @@ class Zone(object):
|
|||||||
return self._root_ns
|
return self._root_ns
|
||||||
|
|
||||||
def hostname_from_fqdn(self, fqdn):
|
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):
|
def add_record(self, record, replace=False, lenient=False):
|
||||||
if self._origin:
|
if self._origin:
|
||||||
|
@@ -47,10 +47,16 @@ class TestZone(TestCase):
|
|||||||
('foo.bar', 'foo.bar.unit.tests'),
|
('foo.bar', 'foo.bar.unit.tests'),
|
||||||
('foo.unit.tests', 'foo.unit.tests.unit.tests.'),
|
('foo.unit.tests', 'foo.unit.tests.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à', 'déjà.unit.tests'),
|
||||||
('déjà.foo', 'déjà.foo.unit.tests'),
|
('déjà.foo', 'déjà.foo.unit.tests'),
|
||||||
('bar.déjà', 'bar.déjà.unit.tests'),
|
('bar.déjà', 'bar.déjà.unit.tests'),
|
||||||
('bar.déjà.foo', 'bar.déjà.foo.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))
|
self.assertEqual(hostname, zone.hostname_from_fqdn(fqdn))
|
||||||
|
|
||||||
@@ -68,6 +74,10 @@ class TestZone(TestCase):
|
|||||||
('déjà.foo', 'déjà.foo.grüßen.de'),
|
('déjà.foo', 'déjà.foo.grüßen.de'),
|
||||||
('bar.déjà', 'bar.déjà.grüßen.de'),
|
('bar.déjà', 'bar.déjà.grüßen.de'),
|
||||||
('bar.déjà.foo', 'bar.déjà.foo.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))
|
self.assertEqual(hostname, zone.hostname_from_fqdn(fqdn))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user