Skip to content

Commit

Permalink
Fix #2231: raise OSError if DNS lookup found no hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Sep 1, 2017
1 parent 8d7bbd0 commit a668f6e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
22 changes: 18 additions & 4 deletions aiohttp/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,22 @@ def __init__(self, loop=None, *args, **kwargs):

@asyncio.coroutine
def resolve(self, host, port=0, family=socket.AF_INET):
try:
resp = yield from self._resolver.gethostbyname(host, family)
except aiodns.error.DNSError as exc:
msg = exc.args[1] if len(exc.args) >= 1 else "DNS lookup failed"
raise OSError(msg) from exc
hosts = []
resp = yield from self._resolver.gethostbyname(host, family)

for address in resp.addresses:
hosts.append(
{'hostname': host,
'host': address, 'port': port,
'family': family, 'proto': 0,
'flags': socket.AI_NUMERICHOST})

if not hosts:
raise OSError("DNS lookup failed")

return hosts

@asyncio.coroutine
Expand All @@ -82,16 +89,23 @@ def resolve_with_query(self, host, port=0, family=socket.AF_INET):
else:
qtype = 'A'

hosts = []
resp = yield from self._resolver.query(host, qtype)
try:
resp = yield from self._resolver.query(host, qtype)
except aiodns.error.DNSError as exc:
msg = exc.args[1] if len(exc.args) >= 1 else "DNS lookup failed"
raise OSError(msg) from exc

hosts = []
for rr in resp:
hosts.append(
{'hostname': host,
'host': rr.host, 'port': port,
'family': family, 'proto': 0,
'flags': socket.AI_NUMERICHOST})

if not hosts:
raise OSError("DNS lookup failed")

return hosts

@asyncio.coroutine
Expand Down
4 changes: 2 additions & 2 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_async_resolver_negative_lookup(loop):
with patch('aiodns.DNSResolver') as mock:
mock().gethostbyname.side_effect = aiodns.error.DNSError()
resolver = AsyncResolver(loop=loop)
with pytest.raises(aiodns.error.DNSError):
with pytest.raises(OSError):
yield from resolver.resolve('doesnotexist.bla')


Expand All @@ -114,7 +114,7 @@ def test_async_resolver_query_negative_lookup(loop):
del mock().gethostbyname
mock().query.side_effect = aiodns.error.DNSError()
resolver = AsyncResolver(loop=loop)
with pytest.raises(aiodns.error.DNSError):
with pytest.raises(OSError):
yield from resolver.resolve('doesnotexist.bla')


Expand Down

0 comments on commit a668f6e

Please sign in to comment.