/
WSClientTrait.php
241 lines (208 loc) · 7.64 KB
/
WSClientTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace WSSC\Components;
use WSSC\Contracts\CommonsContract;
use WSSC\Exceptions\BadOpcodeException;
use WSSC\Exceptions\BadUriException;
use WSSC\Exceptions\ConnectionException;
trait WSClientTrait
{
/**
* Validates whether server sent valid upgrade response
*
* @param ClientConfig $config
* @param string $pathWithQuery
* @param string $key
* @throws ConnectionException
*/
private function validateResponse(ClientConfig $config, string $pathWithQuery, string $key)
{
$response = stream_get_line($this->socket, self::DEFAULT_RESPONSE_HEADER, "\r\n\r\n");
if (!preg_match(self::SEC_WEBSOCKET_ACCEPT_PTTRN, $response, $matches)) {
$address = $config->getScheme() . '://' . $config->getHost() . ':' . $config->getPort() . $pathWithQuery;
throw new ConnectionException(
"Connection to '{$address}' failed: Server sent invalid upgrade response:\n"
. $response, CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE
);
}
$keyAccept = trim($matches[1]);
$expectedResonse = base64_encode(pack('H*', sha1($key . self::SERVER_KEY_ACCEPT)));
if ($keyAccept !== $expectedResonse) {
throw new ConnectionException('Server sent bad upgrade response.',
CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE);
}
}
/**
* Gets host uri based on protocol
*
* @param ClientConfig $config
* @return string
* @throws BadUriException
*/
private function getHostUri(ClientConfig $config): string
{
if (in_array($config->getScheme(), ['ws', 'wss'], true) === false) {
throw new BadUriException(
"Url should have scheme ws or wss, not '{$config->getScheme()}' from URI '$this->socketUrl' .",
CommonsContract::CLIENT_INCORRECT_SCHEME
);
}
return ($config->getScheme() === 'wss' ? 'ssl' : 'tcp') . '://' . $config->getHost();
}
/**
* @param string $data
* @return float|int
* @throws ConnectionException
*/
private function getPayloadLength(string $data)
{
$payloadLength = (int)ord($data[1]) & self::MASK_127; // Bits 1-7 in byte 1
if ($payloadLength > self::MASK_125) {
if ($payloadLength === self::MASK_126) {
$data = $this->read(2); // 126: Payload is a 16-bit unsigned int
} else {
$data = $this->read(8); // 127: Payload is a 64-bit unsigned int
}
$payloadLength = bindec(self::sprintB($data));
}
return $payloadLength;
}
/**
* @param string $data
* @param int $payloadLength
* @return string
* @throws ConnectionException
*/
private function getPayloadData(string $data, int $payloadLength): string
{
// Masking?
$mask = (bool)(ord($data[1]) >> 7); // Bit 0 in byte 1
$payload = '';
$maskingKey = '';
// Get masking key.
if ($mask) {
$maskingKey = $this->read(4);
}
// Get the actual payload, if any (might not be for e.g. close frames.
if ($payloadLength > 0) {
$data = $this->read($payloadLength);
if ($mask) {
// Unmask payload.
for ($i = 0; $i < $payloadLength; $i++) {
$payload .= ($data[$i] ^ $maskingKey[$i % 4]);
}
} else {
$payload = $data;
}
}
return $payload;
}
/**
* @return null|string
* @throws \WSSC\Exceptions\BadOpcodeException
* @throws \InvalidArgumentException
* @throws BadOpcodeException
* @throws BadUriException
* @throws ConnectionException
* @throws \Exception
*/
protected function receiveFragment()
{
// Just read the main fragment information first.
$data = $this->read(2);
// Is this the final fragment? // Bit 0 in byte 0
/// @todo Handle huge payloads with multiple fragments.
$final = (bool)(ord($data[0]) & 1 << 7);
// Parse opcode
$opcode_int = ord($data[0]) & 31; // Bits 4-7
$opcode_ints = array_flip(self::$opcodes);
if (!array_key_exists($opcode_int, $opcode_ints)) {
throw new ConnectionException("Bad opcode in websocket frame: $opcode_int",
CommonsContract::CLIENT_BAD_OPCODE);
}
$opcode = $opcode_ints[$opcode_int];
// record the opcode if we are not receiving a continutation fragment
if ($opcode !== 'continuation') {
$this->lastOpcode = $opcode;
}
$payloadLength = $this->getPayloadLength($data);
$payload = $this->getPayloadData($data, $payloadLength);
if ($opcode === CommonsContract::EVENT_TYPE_CLOSE) {
// Get the close status.
if ($payloadLength >= 2) {
$statusBin = $payload[0] . $payload[1];
$status = bindec(sprintf('%08b%08b', ord($payload[0]), ord($payload[1])));
$this->closeStatus = $status;
$payload = substr($payload, 2);
if (!$this->isClosing) {
$this->send($statusBin . 'Close acknowledged: ' . $status,
CommonsContract::EVENT_TYPE_CLOSE); // Respond.
}
}
if ($this->isClosing) {
$this->isClosing = false; // A close response, all done.
}
fclose($this->socket);
$this->isConnected = false;
}
if (!$final) {
$this->hugePayload .= $payload;
return NULL;
} // this is the last fragment, and we are processing a huge_payload
if ($this->hugePayload) {
$payload = $this->hugePayload .= $payload;
$this->hugePayload = NULL;
}
return $payload;
}
/**
* @param $final
* @param $payload
* @param $opcode
* @param $masked
* @throws ConnectionException
* @throws \Exception
*/
protected function sendFragment($final, $payload, $opcode, $masked)
{
// Binary string for header.
$frameHeadBin = '';
// Write FIN, final fragment bit.
$frameHeadBin .= (bool)$final ? '1' : '0';
// RSV 1, 2, & 3 false and unused.
$frameHeadBin .= '000';
// Opcode rest of the byte.
$frameHeadBin .= sprintf('%04b', self::$opcodes[$opcode]);
// Use masking?
$frameHeadBin .= $masked ? '1' : '0';
// 7 bits of payload length...
$payloadLen = strlen($payload);
if ($payloadLen > self::MAX_BYTES_READ) {
$frameHeadBin .= decbin(self::MASK_127);
$frameHeadBin .= sprintf('%064b', $payloadLen);
} else if ($payloadLen > self::MASK_125) {
$frameHeadBin .= decbin(self::MASK_126);
$frameHeadBin .= sprintf('%016b', $payloadLen);
} else {
$frameHeadBin .= sprintf('%07b', $payloadLen);
}
$frame = '';
// Write frame head to frame.
foreach (str_split($frameHeadBin, 8) as $binstr) {
$frame .= chr(bindec($binstr));
}
// Handle masking
if ($masked) {
// generate a random mask:
$mask = '';
for ($i = 0; $i < 4; $i++) {
$mask .= chr(random_int(0, 255));
}
$frame .= $mask;
}
// Append payload to frame:
for ($i = 0; $i < $payloadLen; $i++) {
$frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i];
}
$this->write($frame);
}
}