Skip to content

Commit

Permalink
Fix empty resolution result on too many redirects
Browse files Browse the repository at this point in the history
Fixes #87.
  • Loading branch information
psafarov authored and kelunik committed Jul 8, 2019
1 parent 0f59e8a commit 2e7921a
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions lib/Rfc1035StubResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,32 @@ public function resolve(string $name, int $typeRestriction = null): Promise
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),
]);

$records = \array_merge(...$records);

break; // Break redirect loop, otherwise we query the same records 5 times
} catch (MultiReasonException $e) {
$errors = [];

foreach ($e->getReasons() as $reason) {
if ($reason instanceof NoRecordException) {
throw $reason;
}

$errors[] = $reason->getMessage();
return yield $this->query($name, $typeRestriction);
}

try {
list(, $records) = yield Promise\some([
$this->query($name, Record::A),
$this->query($name, Record::AAAA),
]);

return \array_merge(...$records);
} catch (MultiReasonException $e) {
$errors = [];

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 {
Expand All @@ -197,7 +195,7 @@ public function resolve(string $name, int $typeRestriction = null): Promise
}
}

return $records;
throw new DnsException("Giving up resolution of '{$name}', too many redirects");
});
}

Expand Down

0 comments on commit 2e7921a

Please sign in to comment.