Skip to content

Commit b940bdc

Browse files
committed
Reject connection attempt when no AAAA or A records are resolved
1 parent 28fac70 commit b940bdc

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/HappyEyeBallsConnectionBuilder.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function connect()
7575

7676
$that->mixIpsIntoConnectQueue($ips);
7777

78+
if ($that->hasBeenResolved() && $that->ipsCount === 0) {
79+
$reject(new \RuntimeException($that->error()));
80+
}
81+
7882
// start next connection attempt if not already awaiting next
7983
if ($that->nextAttemptTimer === null && $that->connectQueue) {
8084
$that->check($resolve, $reject);
@@ -123,7 +127,19 @@ public function connect()
123127
public function resolve($type, $reject)
124128
{
125129
$that = $this;
126-
return $that->resolver->resolveAll($that->host, $type)->then(null, function (\Exception $e) use ($type, $reject, $that) {
130+
return $that->resolver->resolveAll($that->host, $type)->then(function ($ips) use ($that, $type) {
131+
if (\count($ips) === 0) {
132+
if ($type === Message::TYPE_A) {
133+
$that->lastError4 = 'no records found';
134+
$that->lastErrorFamily = 4;
135+
} else {
136+
$that->lastError6 = 'no records found';
137+
$that->lastErrorFamily = 6;
138+
}
139+
}
140+
141+
return $ips;
142+
}, function (\Exception $e) use ($type, $reject, $that) {
127143
unset($that->resolverPromises[$type]);
128144
$that->resolved[$type] = true;
129145

tests/HappyEyeBallsConnectorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
270270
$this->loop->run();
271271
}
272272

273+
public function testConnectionFailsWhenNoAAAOrARecordsAreResolved()
274+
{
275+
$this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
276+
array('google.com', Message::TYPE_AAAA),
277+
array('google.com', Message::TYPE_A)
278+
)->willReturnOnConsecutiveCalls(
279+
$this->returnValue(Promise\resolve(array())),
280+
$this->returnValue(Promise\resolve(array()))
281+
);
282+
$this->tcp->expects($this->never())->method('connect');
283+
284+
$this->setExpectedException('RuntimeException', 'Connection to scheme://google.com:80/?hostname=google.com failed during DNS lookup: no records found');
285+
Block\await($this->connector->connect('scheme://google.com:80/?hostname=google.com'), $this->loop, 3);
286+
}
287+
273288
/**
274289
* @internal
275290
*/

0 commit comments

Comments
 (0)