|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Tarantool Client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Connection; |
|
15
|
|
|
|
|
16
|
|
|
use Tarantool\Client\Exception\UnexpectedResponse; |
|
17
|
|
|
|
|
18
|
|
|
final class Greeting |
|
19
|
|
|
{ |
|
20
|
|
|
public const SIZE_BYTES = 128; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $greeting; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string|null */ |
|
26
|
|
|
private $salt; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string|null */ |
|
29
|
|
|
private $serverVersion; |
|
30
|
|
|
|
|
31
|
|
|
/** @var bool */ |
|
32
|
|
|
private $unknown = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $greeting |
|
36
|
|
|
*/ |
|
37
|
33 |
|
private function __construct($greeting) |
|
38
|
|
|
{ |
|
39
|
33 |
|
$this->greeting = $greeting; |
|
40
|
33 |
|
} |
|
41
|
|
|
|
|
42
|
33 |
|
public static function parse(string $greeting) : self |
|
43
|
|
|
{ |
|
44
|
33 |
|
if (0 === \strpos($greeting, 'Tarantool')) { |
|
45
|
33 |
|
return new self($greeting); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
throw new UnexpectedResponse('Unable to recognize Tarantool server'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function unknown() : self |
|
52
|
|
|
{ |
|
53
|
|
|
$self = new self(''); |
|
54
|
|
|
$self->unknown = true; |
|
55
|
|
|
|
|
56
|
|
|
return $self; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
public function getSalt() : string |
|
60
|
|
|
{ |
|
61
|
3 |
|
if (null !== $this->salt) { |
|
62
|
|
|
return $this->salt; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
if ($this->unknown) { |
|
66
|
|
|
throw new \BadMethodCallException('Salt is unknown for persistent connections'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
if (false === $salt = \base64_decode(\substr($this->greeting, 64, 44), true)) { |
|
70
|
|
|
throw new UnexpectedResponse('Unable to decode salt'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
$salt = \substr($salt, 0, 20); |
|
74
|
|
|
|
|
75
|
3 |
|
if (isset($salt[19])) { |
|
76
|
3 |
|
return $this->salt = $salt; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
throw new UnexpectedResponse('Salt is too short'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
30 |
|
public function getServerVersion() : string |
|
83
|
|
|
{ |
|
84
|
30 |
|
if (null !== $this->serverVersion) { |
|
85
|
|
|
return $this->serverVersion; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
30 |
|
if ($this->unknown) { |
|
89
|
|
|
throw new \BadMethodCallException('Server version is unknown for persistent connections'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
30 |
|
return $this->serverVersion = \substr($this->greeting, 10, \strspn($this->greeting, '0123456789.', 10)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function equals(?self $greeting) : bool |
|
96
|
|
|
{ |
|
97
|
|
|
if (!$greeting || $greeting->unknown) { |
|
98
|
|
|
return $this->unknown; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($this->unknown) { |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $greeting->getSalt() === $this->getSalt(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|