Skip to content

Commit

Permalink
New Message::createResponseWithAnswersForQuery() unifies caching
Browse files Browse the repository at this point in the history
16. November 2016 um 20:33:42
  • Loading branch information
clue committed Feb 13, 2017
1 parent dc2e558 commit 5fef227
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
28 changes: 28 additions & 0 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Dns\Model;

use React\Dns\Query\Query;
use React\Dns\Model\Record;

class Message
{
Expand Down Expand Up @@ -44,6 +45,33 @@ public static function createRequestForQuery(Query $query)
return $request;
}

/**
* Creates a new response message for the given query with the given answer records
*
* @param Query $query
* @param Record[] $answers
* @return self
*/
public static function createResponseWithAnswersForQuery(Query $query, array $answers)
{
$response = new Message();
$response->header->set('id', self::generateId());
$response->header->set('qr', 1);
$response->header->set('opcode', Message::OPCODE_QUERY);
$response->header->set('rd', 1);
$response->header->set('rcode', Message::RCODE_OK);

$response->questions[] = (array) $query;

foreach ($answers as $record) {
$response->answers[] = $record;
}

$response->prepare();

return $response;
}

private static function generateId()
{
return mt_rand(0, 0xffff);
Expand Down
30 changes: 9 additions & 21 deletions src/Query/CachedExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace React\Dns\Query;

use React\Dns\Model\Message;
use React\Dns\Model\Record;

class CachedExecutor implements ExecutorInterface
{
Expand All @@ -18,15 +17,14 @@ public function __construct(ExecutorInterface $executor, RecordCache $cache)

public function query($nameserver, Query $query)
{
$that = $this;
$executor = $this->executor;
$cache = $this->cache;

return $this->cache
->lookup($query)
->then(
function ($cachedRecords) use ($that, $query) {
return $that->buildResponse($query, $cachedRecords);
function ($cachedRecords) use ($query) {
return Message::createResponseWithAnswersForQuery($query, $cachedRecords);
},
function () use ($executor, $cache, $nameserver, $query) {
return $executor
Expand All @@ -39,27 +37,17 @@ function () use ($executor, $cache, $nameserver, $query) {
);
}

/**
* @deprecated unused, exists for BC only
*/
public function buildResponse(Query $query, array $cachedRecords)
{
$response = new Message();

$response->header->set('id', $this->generateId());
$response->header->set('qr', 1);
$response->header->set('opcode', Message::OPCODE_QUERY);
$response->header->set('rd', 1);
$response->header->set('rcode', Message::RCODE_OK);

$response->questions[] = new Record($query->name, $query->type, $query->class);

foreach ($cachedRecords as $record) {
$response->answers[] = $record;
}

$response->prepare();

return $response;
return Message::createResponseWithAnswersForQuery($query, $cachedRecords);
}

/**
* @deprecated unused, exists for BC only
*/
protected function generateId()
{
return mt_rand(0, 0xffff);
Expand Down
11 changes: 11 additions & 0 deletions tests/Model/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public function testCreateRequestDesiresRecusion()
$this->assertTrue($request->header->isQuery());
$this->assertSame(1, $request->header->get('rd'));
}

public function testCreateResponseWithNoAnswers()
{
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);
$answers = array();
$request = Message::createResponseWithAnswersForQuery($query, $answers);

$this->assertFalse($request->header->isQuery());
$this->assertTrue($request->header->isResponse());
$this->assertEquals(0, $request->header->get('anCount'));
}
}

0 comments on commit 5fef227

Please sign in to comment.