diff options
author | Gorka Eguileor <geguileo@redhat.com> | 2020-02-12 13:05:21 +0100 |
---|---|---|
committer | Sergey Shepelev <temotor@gmail.com> | 2023-02-02 05:00:37 +0300 |
commit | f724f4ac0a6ed01c6a882412963b088e6c588a54 (patch) | |
tree | 695c039965a05508e6f41bcbaba9ef990f563fc0 | |
parent | 0e46f0410205ac7a60f9a5d3be401677eab97e03 (diff) | |
download | eventlet-f724f4ac0a6ed01c6a882412963b088e6c588a54.tar.gz |
dns: support host as bytes in getaddrinfo, resolve
Relevant traceback:
```
File "dns/resolver.py", line 858, in query
if qname.is_absolute():
AttributeError: 'bytes' object has no attribute 'is_absolute'
```
Fixes https://github.com/eventlet/eventlet/issues/599
Co-authored-by: Sergey Shepelev <temotor@gmail.com>
Co-authored-by: Tim Burke <tim.burke@gmail.com>
-rw-r--r-- | eventlet/support/greendns.py | 7 | ||||
-rw-r--r-- | tests/greendns_test.py | 24 |
2 files changed, 30 insertions, 1 deletions
diff --git a/eventlet/support/greendns.py b/eventlet/support/greendns.py index 658bb9c..f30c551 100644 --- a/eventlet/support/greendns.py +++ b/eventlet/support/greendns.py @@ -265,6 +265,9 @@ class HostsResolver(object): if isinstance(qname, six.string_types): name = qname qname = dns.name.from_text(qname) + elif isinstance(qname, six.binary_type): + name = qname.decode("ascii") + qname = dns.name.from_text(qname) else: name = str(qname) name = name.lower() @@ -364,7 +367,7 @@ class ResolverProxy(object): if qname is None: qname = '0.0.0.0' - if isinstance(qname, six.string_types): + if isinstance(qname, six.string_types) or isinstance(qname, six.binary_type): qname = dns.name.from_text(qname, None) def step(fun, *args, **kwargs): @@ -545,6 +548,8 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0): """ if isinstance(host, six.string_types): host = host.encode('idna').decode('ascii') + elif isinstance(host, six.binary_type): + host = host.decode("ascii") if host is not None and not is_ip_addr(host): qname, addrs = _getaddrinfo_lookup(host, family, flags) else: diff --git a/tests/greendns_test.py b/tests/greendns_test.py index feab2b6..a05e582 100644 --- a/tests/greendns_test.py +++ b/tests/greendns_test.py @@ -101,6 +101,10 @@ line2 # inline comment hr._v4 = {'v4.example.com': '1.2.3.4'} ans = hr.query('v4.example.com') assert ans[0].address == '1.2.3.4' + ans = hr.query(u'v4.example.com') + assert ans[0].address == '1.2.3.4' + ans = hr.query(b'v4.example.com') + assert ans[0].address == '1.2.3.4' def test_query_ans_types(self): # This assumes test_query_A above succeeds @@ -321,6 +325,10 @@ class TestProxyResolver(tests.LimitedTestCase): rp = greendns.ResolverProxy(hostsres) ans = rp.query('host.example.com') assert ans[0].address == '1.2.3.4' + ans = rp.query(u'host.example.com') + assert ans[0].address == '1.2.3.4' + ans = rp.query(b'host.example.com') + assert ans[0].address == '1.2.3.4' def test_hosts_noanswer(self): hostsres = self._make_mock_hostsresolver() @@ -337,6 +345,15 @@ class TestProxyResolver(tests.LimitedTestCase): rp._resolver = res ans = rp.query('host.example.com') assert ans[0].address == '5.6.7.8' + assert isinstance(res.args[0], dns.name.Name) + + ans = rp.query(u'host.example.com') + assert ans[0].address == '5.6.7.8' + assert isinstance(res.args[0], dns.name.Name) + + ans = rp.query(b'host.example.com') + assert ans[0].address == '5.6.7.8' + assert isinstance(res.args[0], dns.name.Name) def test_noanswer(self): res = self._make_mock_resolver() @@ -658,6 +675,13 @@ class TestGetaddrinfo(tests.LimitedTestCase): addr = [('1.2.3.4', 0)] * len(result) assert addr == [ai[-1] for ai in result] + def test_getaddrinfo_bytes(self): + greendns.resolve = _make_mock_resolve() + greendns.resolve.add('example.com', '1.2.3.4') + res = greendns.getaddrinfo(b'example.com', b'0') + addr = [('1.2.3.4', 0)] * len(res) + assert addr == [ai[-1] for ai in res] + def test_getaddrinfo_hosts_only_timeout(self): hostsres = _make_mock_base_resolver() hostsres.raises = greendns.dns.resolver.NoAnswer |