Skip to content

Commit

Permalink
Amendments to operations to the upport the ReQL API argument expected…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
tbolier committed Feb 13, 2018
1 parent 2f6a1ea commit 8686875
Show file tree
Hide file tree
Showing 77 changed files with 361 additions and 477 deletions.
26 changes: 16 additions & 10 deletions src/Connection/Connection.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand Down Expand Up @@ -155,7 +155,13 @@ public function continueQuery(int $token): ResponseInterface
*/
public function expr(string $string): ResponseInterface
{
return $this->run(new ExprMessage(QueryType::START, 'foo'));
$response = $this->run(new ExprMessage(QueryType::START, 'foo'));

if ($response instanceof ResponseInterface) {
return $response;
}

return new Response();
}

/**
Expand All @@ -179,7 +185,7 @@ public function run(MessageInterface $message, $raw = false)
$this->writeQuery($token, $message);

if ($this->noReply) {
return null;
return new Response();
}

$response = $this->receiveResponse($token, $message);
Expand Down Expand Up @@ -277,9 +283,9 @@ public function writeQuery(int $token, MessageInterface $message): int
}

$requestSize = pack('V', \strlen($request));
$binaryToken = pack('V', $token) . pack('V', 0);
$binaryToken = pack('V', $token).pack('V', 0);

return $this->stream->write($binaryToken . $requestSize . $request);
return $this->stream->write($binaryToken.$requestSize.$request);
}

/**
Expand Down Expand Up @@ -385,27 +391,27 @@ private function validateResponse(
int $token,
MessageInterface $message
): void {
if (!$response->getType()) {
if ($response->getType() === null) {
throw new ConnectionException('Response message has no type.');
}

if ($response->getType() === ResponseType::CLIENT_ERROR) {
throw new ConnectionException('Client error: ' . $response->getData()[0] . ' jsonQuery: ' . json_encode($message));
throw new ConnectionException('Client error: '.$response->getData()[0].' jsonQuery: '.json_encode($message));
}

if ($responseToken !== $token) {
throw new ConnectionException(
'Received wrong token. Response does not match the request. '
. 'Expected ' . $token . ', received ' . $responseToken
. 'Expected '.$token.', received '.$responseToken
);
}

if ($response->getType() === ResponseType::COMPILE_ERROR) {
throw new ConnectionException('Compile error: ' . $response->getData()[0] . ', jsonQuery: ' . json_encode($message));
throw new ConnectionException('Compile error: '.$response->getData()[0].', jsonQuery: '.json_encode($message));
}

if ($response->getType() === ResponseType::RUNTIME_ERROR) {
throw new ConnectionException('Runtime error: ' . $response->getData()[0] . ', jsonQuery: ' . json_encode($message));
throw new ConnectionException('Runtime error: '.$response->getData()[0].', jsonQuery: '.json_encode($message));
}
}
}
4 changes: 2 additions & 2 deletions src/Connection/ConnectionCursorInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand All @@ -10,7 +10,7 @@ interface ConnectionCursorInterface extends ConnectionQueryInterface
{
/**
* @param MessageInterface $message
* @return ResponseInterface
* @return Iterable|ResponseInterface
* @throws ConnectionException
*/
public function rewindFromCursor(MessageInterface $message): ResponseInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/ConnectionException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Connection/ConnectionQueryInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Connection/Options.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Connection/OptionsInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Connection/Registry.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Connection/RegistryInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Connection/Socket/Exception.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection\Socket;

Expand Down
34 changes: 16 additions & 18 deletions src/Connection/Socket/Handshake.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

/**
* @license http://www.apache.org/licenses/ Apache License 2.0
Expand Down Expand Up @@ -113,7 +113,6 @@ public function hello(StreamInterface $stream): void
$stream->close();
throw $e;
}

}

/**
Expand All @@ -123,9 +122,11 @@ public function hello(StreamInterface $stream): void
*/
private function nextMessage(string $response = null): ?string
{
if ($response === null) {
return $this->createHandshakeMessage();
}

switch ($this->state) {
case 0:
return $this->createHandshakeMessage($response);
case 1:
return $this->verifyProtocol($response);
case 2:
Expand All @@ -145,7 +146,7 @@ private function nextMessage(string $response = null): ?string
*/
private function pkbdf2Hmac(string $password, string $salt, int $iterations): string
{
$t = hash_hmac('sha256', $salt . "\x00\x00\x00\x01", $password, true);
$t = hash_hmac('sha256', $salt."\x00\x00\x00\x01", $password, true);
$u = $t;
for ($i = 0; $i < $iterations - 1; ++$i) {
$t = hash_hmac('sha256', $t, $password, true);
Expand All @@ -156,15 +157,12 @@ private function pkbdf2Hmac(string $password, string $salt, int $iterations): st
}

/**
* @param null|string $response
* @return string
*/
private function createHandshakeMessage(?string $response): string
private function createHandshakeMessage(): string
{
$response === null or die('Illegal handshake state');

$this->myR = base64_encode(openssl_random_pseudo_bytes(18));
$this->clientFirstMessage = 'n=' . $this->username . ',r=' . $this->myR;
$this->clientFirstMessage = 'n='.$this->username.',r='.$this->myR;

$binaryVersion = pack('V', $this->version);

Expand All @@ -176,7 +174,7 @@ private function createHandshakeMessage(?string $response): string
[
'protocol_version' => $this->protocolVersion,
'authentication_method' => 'SCRAM-SHA-256',
'authentication' => 'n,,' . $this->clientFirstMessage,
'authentication' => 'n,,'.$this->clientFirstMessage,
]
)
. \chr(0);
Expand All @@ -199,7 +197,7 @@ private function verifyProtocol(?string $response): string

$json = json_decode($response, true);
if ($json['success'] === false) {
throw new Exception('Handshake failed: ' . $json["error"]);
throw new Exception('Handshake failed: '.$json["error"]);
}
if ($this->protocolVersion > $json['max_protocol_version']
|| $this->protocolVersion < $json['min_protocol_version']) {
Expand All @@ -220,7 +218,7 @@ private function createAuthenticationMessage($response): string
{
$json = json_decode($response, true);
if ($json['success'] === false) {
throw new Exception('Handshake failed: ' . $json['error']);
throw new Exception('Handshake failed: '.$json['error']);
}
$serverFirstMessage = $json['authentication'];
$authentication = [];
Expand All @@ -233,15 +231,15 @@ private function createAuthenticationMessage($response): string
throw new Exception('Invalid nonce from server.');
}
$salt = base64_decode($authentication['s']);
$iterations = (int)$authentication['i'];
$iterations = (int) $authentication['i'];

$clientFinalMessageWithoutProof = 'c=biws,r=' . $serverR;
$clientFinalMessageWithoutProof = 'c=biws,r='.$serverR;
$saltedPassword = $this->pkbdf2Hmac($this->password, $salt, $iterations);
$clientKey = hash_hmac('sha256', 'Client Key', $saltedPassword, true);
$storedKey = hash('sha256', $clientKey, true);

$authMessage =
$this->clientFirstMessage . ',' . $serverFirstMessage . ',' . $clientFinalMessageWithoutProof;
$this->clientFirstMessage.','.$serverFirstMessage.','.$clientFinalMessageWithoutProof;

$clientSignature = hash_hmac('sha256', $authMessage, $storedKey, true);

Expand All @@ -256,7 +254,7 @@ private function createAuthenticationMessage($response): string
return
json_encode(
[
'authentication' => $clientFinalMessageWithoutProof . ',p=' . base64_encode($clientProof),
'authentication' => $clientFinalMessageWithoutProof.',p='.base64_encode($clientProof),
]
)
. \chr(0);
Expand All @@ -271,7 +269,7 @@ private function verifyAuthentication(string $response): string
{
$json = json_decode($response, true);
if ($json['success'] === false) {
throw new Exception('Handshake failed: ' . $json['error']);
throw new Exception('Handshake failed: '.$json['error']);
}
$authentication = [];
foreach (explode(',', $json['authentication']) as $var) {
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/Socket/HandshakeInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection\Socket;

Expand Down
14 changes: 11 additions & 3 deletions src/Connection/Socket/Socket.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Connection\Socket;

use Psr\Http\Message\StreamInterface;
use TBolier\RethinkQL\Connection\OptionsInterface;
use TBolier\RethinkQL\Connection\Socket\Exception;

class Socket implements StreamInterface
{
Expand All @@ -25,11 +26,12 @@ class Socket implements StreamInterface

/**
* @param OptionsInterface $options
* @throws Exception
*/
public function __construct(OptionsInterface $options)
{
$this->openStream(
($options->isSsl() ? 'ssl' : 'tcp') . '://' . $options->getHostname() . ':' . $options->getPort(),
($options->isSsl() ? 'ssl' : 'tcp').'://'.$options->getHostname().':'.$options->getPort(),
$options->getTimeout(),
$options->getTimeoutStream()
);
Expand All @@ -39,17 +41,23 @@ public function __construct(OptionsInterface $options)
* @param string $remote_socket
* @param float $timeout
* @param int $timeoutStream
* @throws Exception
*/
private function openStream(string $remote_socket, float $timeout, int $timeoutStream): void
{
$this->stream = stream_socket_client(
$stream = stream_socket_client(
$remote_socket,
$errno,
$errstr,
$timeout,
STREAM_CLIENT_CONNECT
);

if (!$stream) {
throw new Exception('Failed to create a socket stream.');
}

$this->stream = $stream;
stream_set_timeout($this->stream, $timeoutStream);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Message/ExprMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function toArray(): array
return [
$this->queryType,
$this->query,
(object)$this->getOptions(),
(object) $this->getOptions(),
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Message;

Expand Down Expand Up @@ -90,7 +90,7 @@ public function toArray(): array
return [
$this->queryType,
$this->query,
(object)$this->getOptions()
(object) $this->getOptions()
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Message/MessageInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Message;

Expand Down
2 changes: 1 addition & 1 deletion src/Query/AbstractQuery.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query;

Expand Down
4 changes: 2 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query;

Expand Down Expand Up @@ -40,7 +40,7 @@ public function __construct(RethinkInterface $rethink, MessageInterface $message

/**
* @param string $name
* @return Table
* @return TableInterface
*/
public function table(string $name): TableInterface
{
Expand Down
4 changes: 2 additions & 2 deletions src/Query/BuilderInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query;

Expand All @@ -8,7 +8,7 @@ interface BuilderInterface
/**
* @param string $name
*
* @return Table
* @return TableInterface
*/
public function table(string $name): TableInterface;

Expand Down
Loading

0 comments on commit 8686875

Please sign in to comment.