From 2e7921a13ad0fa36cbccafb29b7d6e15dc2740af Mon Sep 17 00:00:00 2001 From: Patrick Safarov Date: Mon, 8 Jul 2019 22:49:23 +0200 Subject: [PATCH] Fix empty resolution result on too many redirects Fixes #87. --- lib/Rfc1035StubResolver.php | 44 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/lib/Rfc1035StubResolver.php b/lib/Rfc1035StubResolver.php index fc2d5a5..0433b75 100644 --- a/lib/Rfc1035StubResolver.php +++ b/lib/Rfc1035StubResolver.php @@ -153,34 +153,32 @@ final class Rfc1035StubResolver implements Resolver for ($redirects = 0; $redirects < 5; $redirects++) { try { if ($typeRestriction) { - $records = yield $this->query($name, $typeRestriction); - } else { - try { - list(, $records) = yield Promise\some([ - $this->query($name, Record::A), - $this->query($name, Record::AAAA), - ]); + return yield $this->query($name, $typeRestriction); + } - $records = \array_merge(...$records); + try { + list(, $records) = yield Promise\some([ + $this->query($name, Record::A), + $this->query($name, Record::AAAA), + ]); - break; // Break redirect loop, otherwise we query the same records 5 times - } catch (MultiReasonException $e) { - $errors = []; + return \array_merge(...$records); + } catch (MultiReasonException $e) { + $errors = []; - foreach ($e->getReasons() as $reason) { - if ($reason instanceof NoRecordException) { - throw $reason; - } - - $errors[] = $reason->getMessage(); + foreach ($e->getReasons() as $reason) { + if ($reason instanceof NoRecordException) { + throw $reason; } - throw new DnsException( - "All query attempts failed for {$name}: " . \implode(", ", $errors), - 0, - $e - ); + $errors[] = $reason->getMessage(); } + + throw new DnsException( + "All query attempts failed for {$name}: " . \implode(", ", $errors), + 0, + $e + ); } } catch (NoRecordException $e) { try { @@ -197,7 +195,7 @@ final class Rfc1035StubResolver implements Resolver } } - return $records; + throw new DnsException("Giving up resolution of '{$name}', too many redirects"); }); }