Skip to content

Commit 41229e7

Browse files
committed
Do not cache truncated response messages as per RFC
See https://tools.ietf.org/html/rfc1035#section-7.4
1 parent 3bf372f commit 41229e7

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/Query/CachingExecutor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ function ($message) use ($nameserver, $query, $id, $cache, $executor, &$pending)
4343
// perform DNS lookup if not already cached
4444
return $pending = $executor->query($nameserver, $query)->then(
4545
function (Message $message) use ($cache, $id) {
46-
// DNS response message received => store in cache and return
47-
$cache->set($id, $message, CachingExecutor::TTL);
46+
// DNS response message received => store in cache when not truncated and return
47+
if (!$message->header->isTruncated()) {
48+
$cache->set($id, $message, CachingExecutor::TTL);
49+
}
4850

4951
return $message;
5052
}

tests/Query/CachingExecutorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ public function testQueryWillReturnResolvedPromiseWhenCacheReturnsMissAndFallbac
8282
$promise->then($this->expectCallableOnceWith($message), $this->expectCallableNever());
8383
}
8484

85+
public function testQueryWillReturnResolvedPromiseWhenCacheReturnsMissAndFallbackExecutorResolvesWithTruncatedResponseButShouldNotSaveTruncatedMessageToCache()
86+
{
87+
$message = new Message();
88+
$message->header->set('tc', 1);
89+
$fallback = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
90+
$fallback->expects($this->once())->method('query')->willReturn(\React\Promise\resolve($message));
91+
92+
$cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock();
93+
$cache->expects($this->once())->method('get')->with('reactphp.org:1:1')->willReturn(\React\Promise\resolve(null));
94+
$cache->expects($this->never())->method('set');
95+
96+
$executor = new CachingExecutor($fallback, $cache);
97+
98+
$query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
99+
100+
$promise = $executor->query('8.8.8.8', $query);
101+
102+
$promise->then($this->expectCallableOnceWith($message), $this->expectCallableNever());
103+
}
104+
85105
public function testQueryWillReturnRejectedPromiseWhenCacheReturnsMissAndFallbackExecutorRejects()
86106
{
87107
$query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);

0 commit comments

Comments
 (0)